-
Notifications
You must be signed in to change notification settings - Fork 798
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
fix(testing): jest component disconnected callback #4269
Merged
tanner-reits
merged 6 commits into
main
from
treits/fix/jest-component-disconnected-callback
Apr 26, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f7ec4c
add method to remove DOM nodes when tearing down test
f6bb867
only remove children of `body`
c3a6f8a
test `removeDomNodes()`
6f84182
fix test cases
be84f27
Merge branch 'main' into treits/fix/jest-component-disconnected-callback
tanner-reits 8ffa837
Merge branch 'main' into treits/fix/jest-component-disconnected-callback
tanner-reits 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 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 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { MockHTMLElement, MockNode } from '../../../mock-doc/node'; | ||
import { removeDomNodes } from '../jest-setup-test-framework'; | ||
|
||
describe('jest setup test framework', () => { | ||
describe('removeDomNodes', () => { | ||
it('removes all children of the parent node', () => { | ||
const parentNode = new MockHTMLElement(null, 'div'); | ||
parentNode.appendChild(new MockHTMLElement(null, 'p')); | ||
|
||
expect(parentNode.childNodes.length).toEqual(1); | ||
|
||
removeDomNodes(parentNode); | ||
|
||
expect(parentNode.childNodes.length).toBe(0); | ||
}); | ||
|
||
it('does nothing if there is no parent node', () => { | ||
const parentNode: MockNode = undefined; | ||
|
||
removeDomNodes(parentNode); | ||
|
||
expect(parentNode).toBeUndefined(); | ||
}); | ||
|
||
it('does nothing if the parent node child array is empty', () => { | ||
const parentNode = new MockHTMLElement(null, 'div'); | ||
parentNode.childNodes = []; | ||
|
||
removeDomNodes(parentNode); | ||
|
||
expect(parentNode.childNodes).toStrictEqual([]); | ||
}); | ||
|
||
it('does nothing if the parent node child array is `null`', () => { | ||
const parentNode = new MockHTMLElement(null, 'div'); | ||
parentNode.childNodes = null; | ||
|
||
removeDomNodes(parentNode); | ||
|
||
expect(parentNode.childNodes).toBe(null); | ||
}); | ||
}); | ||
}); |
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.
I wonder if we could add a test for this either in
end-to-end
orkarma
- thoughts?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.
Chatted with @rwaskiewicz about this and came to the conclusion that there isn't a good, reliable way to test the teardown behavior since that logic executes in a global
afterEach()
callback. Hence, trying to spy on any component'sdisconnectedCallback
or public class member won't work since a component's teardown wouldn't happen until after the test has finished and any assertions have already run.