React Unit testing using RTL : Redux state is not getting updated even mock response has been received in reducer #4622
Unanswered
nandkishorhub
asked this question in
Help
Replies: 1 comment
-
Hi ,
Thank you so much for such detailed clarification 🙂 really appreciate it
.
I was able to resolved issue , I had to use waitFor as I wasn't waiting for
state to update so now it is working.
Thanks again for your help
…On Fri, May 3, 2024, 9:57 PM Sean Dinwiddie ***@***.***> wrote:
I see you're using MSW for mocking API responses and RTL for testing your
React application. You're also using Redux Toolkit for state management.
The issue you're facing is that the state is not being updated in your
component even though you're receiving the mocked response in your reducer.
Here are a few things to check:
1. *Make sure you're using the correct store instance*: In your
renderWithProviders function, you're creating a new store instance
using setupStore. Ensure that you're using the same store instance
throughout your test.
2. *Verify that the reducer is being called*: Put a console log or a
debugger in your reducer to verify that it's being called with the correct
action and payload.
3. *Check the action payload*: Make sure that the payload of the
action is correct and matches the expected shape of your state.
4. *Use waitFor from RTL*: Instead of using render directly, try using
waitFor from RTL to wait for the state to be updated. For example:
test("Rendering default movie/shows", async () => {
const { store } = renderWithProviders(<Home />);
await waitFor(() => expect(store.getState().filmData.movies).toEqual(expectedMovies));
// Assert that the component has rendered correctly});
5. *Check the component's props*: Verify that the component is
receiving the correct props from the store. You can do this by using the
debug function from RTL or by adding a console log in your component.
If none of these suggestions help, please provide more code or details
about your test setup, and I'll do my best to assist you.
—
Reply to this email directly, view it on GitHub
<#4622 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AZ5X4PNX7BWZAX4GEBY5CJLZAO3FHAVCNFSM6AAAAAA7WETIUKVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TGMBXGY2DA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Writing unit tests for my React (Typescript) application where I am using MSW service for Mocking response
For state management using Redux toolkit .
The issue is I am able to get mocked Response in my reducer case inside slice but it is not further updated state and hence I am not able to test component further .
Below is the Redux code , if you check any of the below async method eg: fetchMoviesAsync so I am able to get mock data inside it's case where I am able to console it .
Below is the my test file :
Here I am rendering component with customized render function as per official document .
But I am not able to get updated state data inside respective component .
Code for customized render function
Below is the code for my store.ts
It seems there is some issue regarding store settings for testing .
Beta Was this translation helpful? Give feedback.
All reactions