You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unable to find a childComponent by displayName when shallow rendering the parent while using wrapper.find("displayName") unless displayName includes ForwardRef(childComponent) || ForwardRef
I'm aware of some issues regarding forwardRef, and displayName so I've tried a few different approaches.
describe("<Button />", () => {
let component;
let props = {
className: "class"
}
beforeEach(() => {
component = shallow(<Button {...props} >{children}</Button>);
});
These both fail -->
it("should have a displayName, () => {
console.log(component.displayName) <-- undefined
console.log(component.debug()) <-- '{{ $$typeof: Object(Symbol(react.forward_ref)_16.czyhiszjh),
render: [Function: forwardRef] }}'
})
it("should set displayName", () => {
const button = mount(
<div>
<Button {...props}>{children}</Button>
</div>
);
expect(button.find("Button")).to.have.length(1);
});
Console output on failure:
✗ should set displayName
Invariant Violation: Objects are not valid as a React child (found: object with keys {$$typeof, render}). If you meant to render a collection of children, use an array instead.
in Button
in div (created by WrapperComponent)
in WrapperComponent
Output of wrapper.debug() In the parentComponent:
<div className="container">
<Button className="btn">
Text here
</Button)>
</div>
Output of wrapper in the childComponent:
ShallowWrapper()
Output of wrapper.debug() In the childComponent:
''
Output of wrapper.find('Button') In the childComponent:
ShallowWrapper()
Output of wrapper.find(Button) In the childComponent:
ShallowWrapper()
Expected behavior
Users should be able to use wrapper.find('displayName") to make assertions on a childComponent that uses React.forwardRef without having to set the displayName with ForwardRef(childComponent) || ForwardRef
Your environment
Mac OS High Sierra 10.13.6
Mac Book Pro
API
shallow
Version
library
version
enzyme
@3.6.0
react
@16.5.2
react-dom@
16.5.2
react-test-renderer
@16.5.2
Adapter
library
version
enzyme-adapter-react-16
@1.5.0
enzyme-adapter-utils
@1.8.0
The text was updated successfully, but these errors were encountered:
This has been resolved. The issue was needing a clean install after updating all dependencies.
Running rm -rf node_modules followed by npm i was the solution.
Appreciate your help.
Current behavior
Unable to find a
childComponent
bydisplayName
whenshallow
rendering the parent while usingwrapper.find("displayName")
unlessdisplayName
includesForwardRef(childComponent)
||ForwardRef
I'm aware of some issues regarding
forwardRef
, anddisplayName
so I've tried a few different approaches.Test:
Default approach: (Above test fails)
childComponent
only discoverable viafind
by "ForwardRef"Approach #1: (Above test fails)
childComponent
only discoverable viafind
by "ForwardRef(Button)"Output of wrapper.debug():
Approach #2:
(Above test passes with this approach in the
parentComponent
unit test above, but fails right out of the gate for theButton
unit tests)Failing Button Unit test:
Console output on failure:
Output of wrapper.debug() In the parentComponent:
Output of wrapper in the childComponent:
Output of wrapper.debug() In the childComponent:
Output of wrapper.find('Button') In the childComponent:
Output of wrapper.find(Button) In the childComponent:
Expected behavior
Users should be able to use
wrapper.find('displayName")
to make assertions on achildComponent
that usesReact.forwardRef
without having to set thedisplayName
withForwardRef(childComponent)
||ForwardRef
Your environment
Mac OS High Sierra 10.13.6
Mac Book Pro
API
shallow
Version
Adapter
The text was updated successfully, but these errors were encountered: