-
Notifications
You must be signed in to change notification settings - Fork 1
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 prepareContainer option to mount function #59
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,13 @@ export type MountOptions = { | |
* attached to `document.body`. | ||
*/ | ||
connected?: boolean; | ||
|
||
/** | ||
* When `connected` is true, allows to customize the container to which the | ||
* wrapper is connected to. | ||
* Useful to add custom styles and such. | ||
*/ | ||
prepareContainer?: (container: HTMLElement) => void; | ||
}; | ||
|
||
/** | ||
|
@@ -19,13 +26,17 @@ export type MountOptions = { | |
* 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 = {}) { | ||
export function mount( | ||
jsx: VNode, | ||
{ connected = false, prepareContainer }: MountOptions = {}, | ||
) { | ||
let wrapper; | ||
if (connected) { | ||
const container = document.createElement('div'); | ||
container.setAttribute('data-enzyme-container', ''); | ||
containers.push(container); | ||
|
||
prepareContainer?.(container); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is possible we might encounter use cases in future where the callback needs the container to be connected (ie. in the body) when called. This would be necessary to resolved computed styles for example. I haven't seen any existing use cases where this is necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now I added the call before appending the container to the body, because that was the approach in all places where we were setting styles. But yeah, it's possible we need to do it the other way around in the future. |
||
document.body.append(container); | ||
|
||
wrapper = enzyme.mount(jsx, { attachTo: container }); | ||
|
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 would phrase this as "allows customizing the DOM container in which the component is mounted".