Enzyme testing instruction out of date
See original GitHub issueDescribe the bug Writing test with enzyme as stated here will not work http://react-dnd.github.io/react-dnd/docs/testing#testing-with-enzyme
Reproduction
Write a test using enzyme, Try to retrieve the manager
var backend = root
.instance()
.getManager()
.getBackend()
It will fail because instance() will return null with React 16 (Stateless function component don’t have an instance)
Expected behavior The doc should provide the recommanded working way to achieve this.
Workaround I figured something like this, but does feel quite convoluted:
const mountComponent = (Component: any, testProps: TestProps) => {
const result: any = { wrapper: undefined, manager: undefined };
result.manager = new Promise<DragDropManager>(resolve => {
result.wrapper = mount(
<DndProvider backend={TestBackend}>
<DndContext.Consumer>
{dnd => {
resolve(dnd.dragDropManager);
}}
</DndContext.Consumer>
<Component
{...testProps}
/>
</DndProvider>,
);
});
return (result: {|
wrapper: ReactWrapper<DndProvider>,
manager: Promise<DragDropManager>,
|});
};
usage
let manager: DragDropManager;
let wrapper;
let monitor;
let backend;
beforeEach(async () => {
const result = render(MyComponent, {
/*my props*/
});
manager = await result.manager;
wrapper = result.wrapper;
monitor = manager.getMonitor();
backend = (manager.getBackend(): TestBackend);
});
- OS: MacOS
- Browser: Chrome
- Version : 75
- React: 16.8.6
- React-dnd : 9.3.9
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Unit Testing in React: Full Guide on Jest and Enzyme Testing
Discover how to start and proceed with the testing of React components with Enzyme and Jest if you do not have enough experience...
Read more >How do I set a mock date in Jest? - Stack Overflow
js to do most of my date logic in a helper file for my React components but I haven't been able to figure...
Read more >Better Testing with Enzyme - Fullstack React
Today, we'll look at an open-source library maintained by Airbnb called Enzyme that makes testing fun and easy.
Read more >Testing React applications using Enzyme and Jest manual ...
For example, if one day we decide to start using Redux instead of local state, such state test would become obsolete and more...
Read more >Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make ... or not an outdated snapshot is the correct behavior before...
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 FreeTop 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
Top GitHub Comments
How can I get getHandlerId method for my component if it is a stateless function component?
Found a workaround that might be slightly cleaner than @sandorfr’s above:
First, define this helper for yourself:
Then, you can do something like this in enzyme:
You have to wrap your component in a fragment and use a ref because of an enzyme bug.