-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(testing): jest component disconnected callback (#4269)
* add method to remove DOM nodes when tearing down test This commit adds a method to the Jest environment setup file that will handle removing each node from the mocked DOM. If we do not explicitly remove each node, then the `disconnectedCallback` will not execute for the Stencil component(s). In a testing context, this can be problematic if this lifecycle method contains cleanup code (for things like timeouts and other async functionality) that should execute after the test has ran. * only remove children of `body` * test `removeDomNodes()` * fix test cases
- Loading branch information
1 parent
8ca9058
commit 4ec3b69
Showing
2 changed files
with
80 additions
and
1 deletion.
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); | ||
}); | ||
}); | ||
}); |