wrapper.update not working when mocking a custom hook
See original GitHub issueCurrent behavior
When mocking a custom hook, the component is not updated when calling wrapper.update()
.
Example:
component.jsx
import React from 'react';
import useCustomHook from './custom-hook';
export function MyComponent() {
const hookResult = useCustomHook();
return <span>{hookResult}</span>;
}
component.test.jsx
import { shallow } from 'enzyme';
import React from 'react';
import { MyComponent } from './component';
import useCustomHook from './custom-hook';
jest.mock('./custom-hook', () => jest.fn(() => 'bananas'));
it('should update the component', () => {
expect(wrapper.children().text()).toBe('bananas');
useCustomHook.mockReturnValueOnce('apples');
wrapper.update();
expect(wrapper.children().text()).toBe('apples');
})
Expected behavior
The above test passes.
I can get it to work when I replace the wrapper.update()
with a wrapper.setProps({})
(which is kinda weird).
So why is the component not re-rendering? According to the docs, wrapper.update()
is forcing a re-render. But I guess some optimization kicks in here and prevents the re-render.
Your environment
API
- shallow
- mount
- render
Version
library | version |
---|---|
enzyme | 3.10.0 |
react | 16.8.6 |
react-dom | 16.8.3 |
react-test-renderer | 16.8.6 |
adapter (below) |
Adapter
- enzyme-adapter-react-16
- enzyme-adapter-react-16.3
- enzyme-adapter-react-16.2
- enzyme-adapter-react-16.1
- enzyme-adapter-react-15
- enzyme-adapter-react-15.4
- enzyme-adapter-react-14
- enzyme-adapter-react-13
- enzyme-adapter-react-helper
- others ( )
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to mock a custom hook inside of a React component you ...
To mock your custom hook using jest. import * as useCustomHook from '../hooks/useCustomHooks' const spy = jest.spyOn(useCustomHook ...
Read more >Mocking React hooks when unit testing using Jest
Below I mock the base-fetch module which is responsible for making requests to the SWAPI endpoints and returning a JSON object. Instead of...
Read more >A Complete Guide to Testing React Hooks - Toptal
This article explores how to test React Hooks and outlines an eight-step testing plan you could employ to test your own projects.
Read more >Advanced Hooks - React Hooks Testing Library
Simple and complete React hooks testing utilities that encourage good testing practices. ... We can use the wrapper option for renderHook to do...
Read more >Testing Custom React Hooks with Jest -- newline - Fullstack.io
That way, your tests failing means the problem is with the UI and not somewhere else. How do I mock my hook's dependencies?#....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@marcus13371337 - This issue is nothing to do with hooks. The documentation for
wrapper.update
is simply incorrect.From your test cases:
This is expected. When you changed the mock return value from your hook, the component wasn’t re-rendered and the Enzyme tree was not updated.
This is also expected.
wrapper.update()
refreshes Enzyme’s view of the rendered component tree, but it doesn’t re-render the component. For that you needwrapper.setProps({})
.Thinking about this issue at a higher level, although there are good technical reasons for why Enzyme works the way it does (see the Enzyme v2 => v3 migration guide), this is clearly quite confusing for users. This could perhaps be addressed by providing visual documentation to convey the mental model better, but perhaps there are other changes that could be made to avoid the issue?
The docs say that
wrapper.mount()
unmounts and re-mounts the component, whereassetProps({})
re-renders (aka. “updates”) the component with new props, which won’t trigger lifecycle hooks related to mounting and unmounting.