-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add utilities for mounting and un-mounting components #53
Merged
Merged
Changes from all commits
Commits
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
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 = []; | ||
} |
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.
This is convenient, as very frequently we need the element to actually be in the DOM and we don't care about the container, but there are cases in which we need to customize the container with styles and such. See this test for example.
Maybe we could allow an HTMLElement to be conditionally provided, either by allowing the
connected
option to beconnected?: boolean | HTMLElement
, or by providing a more complex union type where eitherconnected?: boolean
orcontainer?: HTMLElement
to be allowed.The later is probably more correct from a type point of view, but will require more checks.
We could also try to find a different option name where
boolean | HTMLElement
feels less awkward.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.
To begin with I would suggest that callers continue to use the underlying Enzyme API if they need to customize the container. When we better understand how custom containers are used then we could integrate it into this convenience API. There are some details I'm not sure about. For example, with this API the container is "owned" by this package and gets removed when
unmountAll
is called. I'm not sure if that is appropriate for custom containers.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.
Yeah, good point.
Let's land this. It will allow us to remove a lot of duplicated code already.