[Dropdown Menu] Problems when testing with Jest+RTL
See original GitHub issueHello!
We’re trying to test a wrapper we made around the Dropdown Menu component but we found out that it’s not rendering when the trigger is clicked. This only happens in the tests, the UI works like a charm.
We thought it could be related to the portal, so we used portalled={false}
and got the same results.
I tried to create a reproduction on codesandbox but now I’m getting a different error, which makes me think that maybe we have ResizeObserver mocked, I’ll have to take a look.
Failed to execute ‘observe’ on ‘ResizeObserver’: parameter 1 is not of type ‘Element’.
Here is the codesandbox reproducing the error.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
[DropdownMenu] `jest` / `react-testing-library` tests failing on ...
Actually, any test that involves some sort of pop up box appearing currently fails (so "@radix-ui/react-dropdown-menu": "0.1.6" and "@radix-ui/ ...
Read more >Test Drop Down With React Testing Library - Stack Overflow
When your data comes from an asynchronous fetch call, the DOM doesn't get updated synchronously, and you have to use one of the...
Read more >How to test a Semantic UI React Dropdown using Jest and ...
A Semantic UI React Dropdown using the options array above. Give it a default value of the first option in the array, Jenny...
Read more >Dropdown menu properties issue with sizing and color
When I use test the application on a mobile device, the dropdown menu takes up the entire screen and the "x" button to...
Read more >React Testing Library Common Scenarios - Rafael Quintanilha
For each scenario, I describe the problem and how you can write a test for it with Jest + React Testing Library.
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
Hey @rubenmoya,
Thanks for reaching out. I believe this is an issue with the test environment (jest/jsdom) not having
ResizeObserver
. That’s why you don’t see an issue in your app. The test environment is not a real browser/DOM so you don’t necessarily have all the APIs available.I believe what you’d have to do is mock these bits within your testing environment of choice.
We’re currently in alpha so we don’t have tests for all packages yet, but eventually you should be able to take a look at how we did it ourselves for the same purpose.
In the meantime, I’ve had a very quick go in our codebase with the same test you were trying to run, and I’ve had to mock a couple pieces for it to work:
I hope that helps, if so, feel free to close the issue if this is satisfactory.
For anyone looking to a typescript format of the same solution: