react-testing | sample project from a talk on testing React | Frontend Framework library
kandi X-RAY | react-testing Summary
kandi X-RAY | react-testing Summary
(Update on 2020-04-22): This talk was given a long time ago. Tools and best practices have evolved since then. For more up-to-date information, have a look at:.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of react-testing
react-testing Key Features
react-testing Examples and Code Snippets
Community Discussions
Trending Discussions on react-testing
QUESTION
I'm using react testing library to test my component built with FluentUI.
Here is link: https://codesandbox.io/s/keen-borg-2tqmj?file=/src/App.spec.js
The code is basically a pasted snippet of the example code of Dialog
component from FluentUI docs site. The behavior that I'm testing is:
- User opens the dialog
- User clicks outside of the dialog
onDimiss
prop of the component should be fired.
It works when I'm playing with it manually however it seems that I failed to simulate a click outside of the component with testing library.
I tried using userEvent.click(document.body)
as mentioned in this post but got no luck
Does anyone has any idea how to make test work?
...ANSWER
Answered 2021-Jun-11 at 15:51It is not working because the Dialog component is not listening for the onClick
event on the body
, so what you need to do in this case is to find the actual element that is being clicked, observing the dom you'll find that the overlay is a div with some overlay classes on it.
QUESTION
I am testing an app with react-testing-library and I am using a mock service worker. All my tests pass until the last one which gives this error in the title. When testing only the part which gives error isolated (test.only), then it doesn't throw error. The error points to localhost:5000 which is my data server (my app is running on 3000)
This is my last test which only works when running alone:
...ANSWER
Answered 2021-May-20 at 13:26Just was a typo in one of the server url addresses in my mock service worker handlers. So when testing a part which gets requests to that url it threw an error sometimes, in case I place that function at the end, the tests finished before doing the get request to the wrong url. That's why it had that strange behaviour.
QUESTION
I'm new to Jest and react-testing-library, so hopefully the solution to this is straight forward.
What is the proper way to test the value returned through the 'onChange' prop?
...ANSWER
Answered 2021-May-20 at 05:10You could try something like:
QUESTION
when i try to build my project with yarn run build i get errors that are not exist in my code my code is clean it works fine in my local. I've been stuck for two weeks to resolve this problem please help me to solve this problem. this the errors that i get
node version: v10.15.3
webpack: 4.30.0 this is my package.json
...ANSWER
Answered 2021-May-09 at 20:03i added two folders that was missing 'transversal-administration', 'transversal-translation' in the past i have just only: ['app']. the loader in the past load just the app folder
QUESTION
I'm using Jest/Enzyme to test a React/TypeScript application and I'm having a hard time trying to write a test to assert if a button shows up after a certain period of time:
Here's a very simplified version of the component to be tested:
...ANSWER
Answered 2021-May-08 at 03:06When the component is mounted, useEffect
and setTimeout
will be executed, you need to use jest.useFakeTimers(implementation?: 'modern' | 'legacy') before mounting the component.
Use jest.runOnlyPendingTimers() to
Executes only the macro-tasks that are currently pending (i.e., only the tasks that have been queued by setTimeout() or setInterval() up to this point)
Or jest.advanceTimersByTime(msToRun).
Besides, we need to wrap the code rendering it and performing updates inside an act() call, So put jest.runOnlyPendingTimers()
in act()
.
Finally, we need to call wrapper.update()
to make sure the state reflect to the view.
E.g.
SomeComponent.tsx
:
QUESTION
Is there a way to fire a custom event with react-testing-library? I couldn't find such example in their docs.
...ANSWER
Answered 2021-May-06 at 12:14You can use fireEvent to dispatch a CustomEvent on document.body
HTML element. I added spy to console.log()
method to check if the onEvent
event handler is called or not.
E.g.
index.tsx
:
QUESTION
How to mock video duration from fire event loadedMetadata
with react-testing library?
in my React code, I'm retrieving the video duration with loadedMetaData event listener:
...ANSWER
Answered 2021-May-01 at 10:06you can modify the jsdom object
QUESTION
I have a material-ui navbar with a couple of material-ui icons on it. One of them is a Bookmarks icon. When clicking the icon it becomes blue (rgb(25, 118, 210)
) and when it is clicked again or somewhere in the page it should become white. This is achieved using a local variable.
ANSWER
Answered 2021-Apr-27 at 08:11I ended up asking this question in the official react-testing-library git hub repository.
and I got the following answer from one of the contributors.
You run your tests in JSDOM which has not full CSS support
That means that only partial css behavior can be tested.
He mentioned that:
Anything .
toHaveStyle
has a low confidence depending on how these styles are applied. You should use visual regression tests for these types of assertions.
You can find the issue in this link for more information.
QUESTION
I've stucked when learning how to tests react components with custom hooks. I think that the issue is connected with hook mocking but I have no idea what I'm doing wrong. I'm using jest and react-testing-library.
counter.js
...ANSWER
Answered 2021-Apr-21 at 22:07You don't need to mock the hook during the test. You want your test to resemble the actual behaviour as closely as possible.
In this case, just click on the button and check if the count value has been updated to the expected value in the DOM.
QUESTION
I'm trying to figure out how to test components that update state using useEffect to make an API call to get data. There are several things I think are important to know before I can talk anymore, and that is the files/packages I'm using.
First, I have a main component called App.tsx
, inside App.tsx
, inside of useEffect, I make a fetch call to an external API to fetch an array of songs by Queen. I also render out a component using
.map
to iterate over each song and .filter
to filter songs on UI based on text input. I'm using a custom hook. Here is the code I have for that component and its custom hook.
ANSWER
Answered 2021-Apr-08 at 17:16I was able to get a passing test with the updated DOM by changing the initial value from null to a blank Array.
I also changed the resulting testing code to the following.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install react-testing
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page