We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
The only way to test a component that relies on a useContext hook appears to be to use render or mount, not shallow.
useContext
render
mount
shallow
The following methods of attempting to provide context to a component that is shallow rendered all fail: See Code Sandbox with full details, including implementations of Child and TestContextHelper
it("receives context when context is provided by a context helper component", () => { const wrapper = shallow(<Child />, { wrappingComponent: TestContextHelper }); expect(wrapper.exists()); expect(wrapper.text().includes("hi there")).toBe(true); }); it("can be provided a context provider via wrappingComponent and wrappingComponentProps", () => { const wrapper = shallow(<Child />, { wrappingComponent: TestContext.Provider, wrappingComponentProps: { value: "hi there" } }); expect(wrapper.text().includes("hi there")).toBe(true); }); it("can be provided a context provider via wrappingComponent and setting props later", () => { const wrapper = shallow(<Child />, { wrappingComponent: TestContext.Provider }); const provider = wrapper.getWrappingComponent(); provider.setProps({ value: "hi there" }); expect(wrapper.text().includes("hi there")).toBe(true); }); it("can be wrapped by context and shallow rendered", () => { const wrapper = shallow( <TestContext.Provider value="hi there"> <Child /> </TestContext.Provider> ); expect(wrapper.text().includes("hi there")).toBe(true); }); });
I can use shallow on components that use useContext
https://codesandbox.io/s/priceless-driscoll-j45bv
Tested with both; codesandbox reflects enzyme-adapter-react-16.
The text was updated successfully, but these errors were encountered:
I swear I searched for duplicate issues before I posted this.. but I did not see this one. 😭 Sorry, maintainers!
Sorry, something went wrong.
No branches or pull requests
Current behavior
The only way to test a component that relies on a
useContext
hook appears to be to userender
ormount
, notshallow
.The following methods of attempting to provide context to a component that is shallow rendered all fail:
See Code Sandbox with full details, including implementations of Child and TestContextHelper
Expected behavior
I can use
shallow
on components that useuseContext
Your environment
https://codesandbox.io/s/priceless-driscoll-j45bv
API
Version
Adapter
Tested with both; codesandbox reflects enzyme-adapter-react-16.
The text was updated successfully, but these errors were encountered: