-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Support wrappingComponent & wrappingComponentProps #157
Changes from all commits
571d7a8
829fae4
7d7790d
720c15c
f5ee93a
2cdb301
9fc65e2
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Component } from 'preact'; | ||
|
||
export default class RootFinder extends Component { | ||
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 would be useful to have a brief comment explaining what 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. Updated |
||
render() { | ||
return this.props.children; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { ReactElement } from 'react'; | ||
import type { EnzymeAdapter, ShallowRendererProps } from 'enzyme'; | ||
import RootFinder from './RootFinder.js'; | ||
import { childElements } from './compat.js'; | ||
import { cloneElement } from 'preact'; | ||
|
||
/** Based on the equivalent function in `enzyme-adapter-utils` */ | ||
export default function wrapWithWrappingComponent( | ||
createElement: EnzymeAdapter['createElement'], | ||
node: ReactElement, | ||
options: ShallowRendererProps = {} | ||
) { | ||
const { wrappingComponent, wrappingComponentProps = {} } = options; | ||
|
||
if (!wrappingComponent) { | ||
return node; | ||
} | ||
|
||
let nodeWithValidChildren = node; | ||
|
||
if (typeof nodeWithValidChildren.props.children === 'string') { | ||
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. To summarize, this is normalizing VNodes like 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. 👍 |
||
// This prevents an error when `.dive()` is used: | ||
// `TypeError: ShallowWrapper::dive() can only be called on components`. | ||
// --------------------------------------------------------------------- | ||
// VNode before: `{ type: Widget, props: { children: 'test' }, ... }` | ||
// VNode after: `{ type: Widget, props: { children: ['test'] }, ... }` | ||
nodeWithValidChildren = cloneElement( | ||
nodeWithValidChildren, | ||
nodeWithValidChildren.props, | ||
childElements(nodeWithValidChildren) | ||
); | ||
} | ||
|
||
return createElement( | ||
wrappingComponent, | ||
wrappingComponentProps, | ||
createElement(RootFinder, null, nodeWithValidChildren) | ||
); | ||
} |
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 check my understanding,
RootFinder
is essentially a marker to allow location of the wrapped component in the rendered tree?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.
If I understand the Enzyme source code correctly, it's purpose is to properly pass down context, see here https://github.com/enzymejs/enzyme/blob/57baba5ceaccec7a3ada40d7559f5bf71289cbe7/packages/enzyme/src/ShallowWrapper.js#L356 and here https://github.com/enzymejs/enzyme/blob/57baba5ceaccec7a3ada40d7559f5bf71289cbe7/packages/enzyme/src/ShallowWrapper.js#L322. I tried what happens if I don't insert RootFinder into
wrapWithWrappingComponent
and it didn't work.