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.

Error clicking node in React Flow in jest test with @testing-library

See original GitHub issue

Describe the Bug

We have test runned with jest and react-testing-library. When test is run, everything works fine until node or any element inside node is clicked:

TypeError: Cannot read properties of null (reading 'document')

      at Ke (node_modules/reactflow/dist/umd/index.js:10:15269)
      at HTMLDivElement.E (node_modules/reactflow/dist/umd/index.js:10:74213)
      at HTMLDivElement.<anonymous> (node_modules/reactflow/dist/umd/index.js:10:7374)
      at HTMLDivElement.callTheUserObjectsOperation (node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30)
      at innerInvokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:338:25)
      at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:274:3)
      at HTMLDivElementImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:221:9)
      at HTMLDivElementImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:94:17)
      at HTMLDivElement.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:231:34)
      at node_modules/@testing-library/user-event/dist/cjs/event/dispatchEvent.js:47:43
      at node_modules/@testing-library/react/dist/pure.js:64:16
      at batchedUpdates$1 (node_modules/react-dom/cjs/react-dom.development.js:22380:12)
      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1042:14)
      at Object.eventWrapper (node_modules/@testing-library/react/dist/pure.js:63:28)
      at Object.wrapEvent (node_modules/@testing-library/user-event/dist/cjs/event/wrapEvent.js:8:28)
      at Object.dispatchEvent (node_modules/@testing-library/user-event/dist/cjs/event/dispatchEvent.js:47:22)
      at Object.dispatchUIEvent (node_modules/@testing-library/user-event/dist/cjs/event/dispatchEvent.js:24:26)
      at Mouse.down (node_modules/@testing-library/user-event/dist/cjs/system/pointer/mouse.js:70:34)
      at PointerHost.press (node_modules/@testing-library/user-event/dist/cjs/system/pointer/index.js:27:24)
      at pointerAction (node_modules/@testing-library/user-event/dist/cjs/pointer/index.js:59:43)
      at Object.pointer (node_modules/@testing-library/user-event/dist/cjs/pointer/index.js:35:15)
      at node_modules/@testing-library/react/dist/pure.js:57:16

I have tried to add nodesDraggable={false} to ReactFlow and className='nodrag' to Node, but that doesn’t help. We have added needed lines described in documentation to testUtils.ts. Nodes and elements inside nodes are rendered and pass test.

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

Unfortunately can’t share any actual code, other than test case:

const user = userEvent.setup();
const { getAllByText, getByTestId, getByText } = render(<WrappedReactFlow />);
await waitForElementToBeRemoved(getByTestId('loading-spinner'));
expect(getAllByText(/node title/i).length).toBe(1);
const node = getByText(/node title/i);
await user.click(node);

That’s pretty much is failing test case. Last line produces the error.

Wrapper includes mocks to redux store, API calls, history and GraphQL (moving to GQL, but still using some legacy for a while) and everything with those seem to work, as nodes are rendered.

Expected behavior

As a user I expect Node to be selected with no error and I can further test my app.

Screenshots or Videos

No response

Platform

Additional context

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
quantticommented, Nov 24, 2022

I can select node, but not with just mouse click. Could be something with how my custom node is built, as it is selectable by space after clicking it and it can be selected by tab. Anyway was able to get around it. Thanks again!

0reactions
moklickcommented, Nov 23, 2022

Why can’t you select a node?

Read more comments on GitHub >

github_iconTop Results From Across the Web

edges not rendered to JSDOM when testing with Jest ... - GitHub
Hi! I'm having some issue on making the nodes and edges to show up when I'm rendering the graph to JSDOM with Jest...
Read more >
Testing with react-testing-library and Jest | by Chidume Nnamdi
We can test for DOM events in our React components using react-testing-library. DOM events like: click; mouse events; key events; touch events ...
Read more >
"myText" error when using react-testing-library - Stack Overflow
I feel that my test is not complete for some reason. // Image.js Component // @flow import * as React from "react ......
Read more >
Testing Guide - React Flow
There are plenty of options to test a React application. If you want to test a React Flow application, we recommend to use...
Read more >
Testing & Error Handling (Jest, React Testing Library) - YouTube
Companies all over the world are using Next.js to build performant, scalable applications. In this video, we'll talk about...- Unit test vs ...
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