-
Notifications
You must be signed in to change notification settings - Fork 1
Fix/detach #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shaohua
wants to merge
5
commits into
master
Choose a base branch
from
fix/detach
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix/detach #13
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,122 @@ | ||
describe('test-component', function() { | ||
|
||
before(function(done) { | ||
//WebComponentsReady event is | ||
//fired only once after custom elments polyfill finished its start up tasks | ||
//all subequent upgrades are done by MutationObserver hence no more events | ||
window.addEventListener('WebComponentsReady', function() { | ||
done(); | ||
}); | ||
}); | ||
|
||
beforeEach(function() { | ||
// this function is called in detachedCallback of the component | ||
window.detachedCallbackFromGadget = function(){}; | ||
}); | ||
|
||
afterEach(function(){ | ||
delete window.detachedCallbackFromGadget; | ||
}); | ||
|
||
it('renders NEW CONTENT instead of the original content', function(done){ | ||
window.addEventListener('WebComponentsReady', function(e) { | ||
var div = document.createElement('div'); | ||
div.innerHTML = '<test-component data-new-content="NEW CONTENT">Original content</test-component>'; | ||
document.body.appendChild(div); | ||
var div = document.createElement('div'); | ||
div.innerHTML = '<test-component data-new-content="NEW CONTENT">Original content</test-component>'; | ||
document.body.appendChild(div); | ||
|
||
setTimeout(function(){ | ||
chai.expect(div.children[0].innerHTML).to.eq('NEW CONTENT'); | ||
setTimeout(function(){ | ||
//need to wait for upgrade to finish | ||
//can set to zero for browsers with native mutation observer | ||
//need a longer time for browsers without native mutation observer | ||
var element = document.querySelector('test-component'); | ||
expect(element.innerHTML).to.eq('NEW CONTENT'); | ||
document.body.removeChild(div); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('fires detachedCallback when removing the custom element', function(done){ | ||
var element = document.createElement('test-component'); | ||
document.body.appendChild(element); | ||
|
||
window.detachedCallbackFromGadget = function() { | ||
done(); | ||
}; | ||
setTimeout(function(){ | ||
document.body.removeChild(element); | ||
}, 100); | ||
|
||
}); | ||
|
||
it('fires detachedCallback when removing the parent of the custom element', function(done){ | ||
var div = document.createElement('div'); | ||
div.innerHTML = '<test-component data-new-content="NEW CONTENT">Original content</test-component>'; | ||
document.body.appendChild(div); | ||
|
||
window.detachedCallbackFromGadget = function() { | ||
done(); | ||
}; | ||
setTimeout(function(){ | ||
document.body.removeChild(div); | ||
}, 100); | ||
|
||
}); | ||
|
||
|
||
it('fires detachedCallback when removing the parents of the custom element', function(done){ | ||
//create a custom element nested in eleven divs | ||
var i = 0, tempDiv; | ||
var walker = function(input){ | ||
tempDiv = document.createElement('div'); | ||
if(i >= 9) { | ||
tempDiv.innerHTML = '<test-component data-new-content="NEW CONTENT">Original content</test-component>'; | ||
input.appendChild(tempDiv); | ||
return; | ||
} else { | ||
input.appendChild(tempDiv); | ||
i++; | ||
walker(tempDiv); | ||
} | ||
}; | ||
|
||
var div = document.createElement('div'); | ||
walker(div); | ||
document.body.appendChild(div); | ||
|
||
window.detachedCallbackFromGadget = function() { | ||
done(); | ||
}; | ||
setTimeout(function(){ | ||
document.body.removeChild(div); | ||
}, 100); | ||
|
||
}); | ||
|
||
it('fires detachedCallback when removing and adding back the custom element', function(done){ | ||
var element = document.createElement('test-component'); | ||
document.body.appendChild(element); | ||
|
||
var i = 0; | ||
window.detachedCallbackFromGadget = function() { | ||
i++; //count number of invocations | ||
if(i >= 2) { | ||
// only done after the attachedCallback is fired twice or more | ||
done(); | ||
} | ||
}; | ||
|
||
setTimeout(function(){ //wait for upgrade | ||
document.body.removeChild(element); | ||
|
||
//wait for mutation observer to fire | ||
//if not setTimeout, then detachedCallback is not fired for the removeChild above | ||
setTimeout(function(){ | ||
document.body.appendChild(element); | ||
setTimeout(function(){ | ||
document.body.removeChild(element); | ||
}, 100); | ||
}, 100); | ||
}); | ||
|
||
}, 100); | ||
|
||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this some sort of magic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
detachedCallbackFromGadget is called from the other file where the component is defined