Skip to content
Advertisement

Testing react components with redux-toolkit inside

I have a component like this:

JavaScript

and am trying to test it.

JavaScript

The problem is, I somehow need to mock the store, to match my case. Any ideas how I should handle this?

I remember in my previous project I used a npm package to do this, but can’t find it now, and can’t remember how I did it, or what it was called

Advertisement

Answer

you can create a helper function for your tests to abstract the setup of the store/dependencies:

JavaScript

and use it in your tests:

JavaScript

have a look in the “Writing Tests” page from Redux, it is part of the “Recommended practices for testing apps using Redux”: https://redux.js.org/usage/writing-tests#connected-components

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement