question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enzyme testing instruction out of date

See original GitHub issue

Describe 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:open
  • Created 4 years ago
  • Reactions:8
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
RuGrofcommented, Aug 14, 2019

How can I get getHandlerId method for my component if it is a stateless function component?

3reactions
jkilliancommented, Oct 2, 2019

Found a workaround that might be slightly cleaner than @sandorfr’s above:

First, define this helper for yourself:

  const wrapInTestContext = Component =>
    React.forwardRef((props, ref) => {
      const dragDropManager = useRef();
      useImperativeHandle(ref, () => ({ getManager: () => dragDropManager.current }));

      return (
        <DndProvider backend={TestBackend}>
          <DndContext.Consumer>
            {ctx => {
              dragDropManager.current = ctx.dragDropManager;
            }}
          </DndContext.Consumer>
          <Component {...props} />
        </DndProvider>
      );
    });

Then, you can do something like this in enzyme:

const MyCompWithDnd = wrapInTestContext(MyComp);

const ref = React.createRef();
const enzymeWrapper = mount(<><MyCompWithDnd ref={ref} /></>);

ref.current.getManager(); // now works!

You have to wrap your component in a fragment and use a ref because of an enzyme bug.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found