-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utilities for mounting and un-mounting components
A common pattern in our tests is to render a component and add the wrapper and (optionally) its DOM container to a list of active wrappers/containers. At the end of the test a Mocha `afterEach` block is then used to ensure all components are unmounted. This commit adds `mount` and `unmountAll` utilities to this package to simplify this pattern. The `mount` function renders the component and adds its wrapper to the active set. `unmountAll` unmounts all active wrappers.
- Loading branch information
1 parent
ba7bdd2
commit 0b51eb0
Showing
4 changed files
with
87 additions
and
2 deletions.
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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export type { Scenario } from './accessibility.js'; | ||
export { checkAccessibility } from './accessibility.js'; | ||
export { mockImportedComponents } from './mock-imported-components.js'; | ||
export { mount, unmountAll } from './mount.js'; | ||
export type { MountOptions } from './mount.js'; | ||
export type { TestTimeout, TimeoutSpec } from './wait.js'; | ||
export { delay, waitFor, waitForElement } from './wait.js'; |
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,48 @@ | ||
import * as enzyme from 'enzyme'; | ||
import type { ReactWrapper } from 'enzyme'; | ||
import type { VNode } from 'preact'; | ||
|
||
let containers: HTMLElement[] = []; | ||
let wrappers: ReactWrapper[] = []; | ||
|
||
export type MountOptions = { | ||
/** | ||
* If true, the element will be rendered in a container element which is | ||
* attached to `document.body`. | ||
*/ | ||
connected?: boolean; | ||
}; | ||
|
||
/** | ||
* Render a Preact component using Enzyme and return a wrapper. | ||
* | ||
* The component can be unmounted by calling `wrapper.unmount()` or by calling | ||
* {@link unmountAll} at the end of the test. | ||
*/ | ||
export function mount(jsx: VNode, { connected = false }: MountOptions = {}) { | ||
let wrapper; | ||
if (connected) { | ||
const container = document.createElement('div'); | ||
container.setAttribute('data-enzyme-container', ''); | ||
containers.push(container); | ||
wrapper = enzyme.mount(jsx, { attachTo: container }); | ||
} else { | ||
wrapper = enzyme.mount(jsx); | ||
} | ||
|
||
wrappers.push(wrapper); | ||
|
||
return wrapper; | ||
} | ||
|
||
/** | ||
* Unmount all Preact components rendered using {@link mount} and remove their | ||
* parent container elements (if any) from the DOM. | ||
*/ | ||
export function unmountAll() { | ||
wrappers.forEach(w => w.unmount()); | ||
wrappers = []; | ||
|
||
containers.forEach(c => c.remove()); | ||
containers = []; | ||
} |