Skip to content
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

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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".

* Useful to add custom styles and such.
*/
prepareContainer?: (container: HTMLElement) => void;
};

/**
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 });
Expand Down