Better support unit-testing of reducers that handle lifecycle actions
See original GitHub issueContext
Using createAsyncThunk
, I created a thunk fetchData
to fetch data from the network. I unit-test the (non-trivial) thunk logic and the lifecycle reducers seperately, that is, I have unit tests to
- Check if the thunk dispatches the right actions, and
- Check if the reducers update the store correctly for each of the three lifecycle actions.
Problem
To test the reducer logic for any of the lifecycle actions, say the fulfilled
action, I need to dispatch the action. I see three options to create the action, none of which seems ideal:
- One option is to call
fetchData.fulfilled
. However, the action creator’s parameters are not explained anywhere in the official docs, and the number of parameters was silently changed in release 1.5.0. This suggests that the action creator is not intended to be called from outside of RTK. - Instead, I could easily write my own action creator of course. However, I guess it would be better if RTK provided it as it would likely be beneficial for many RTK users. After all, unit-testing reducers is quite a common use case 😃
- A third option is to mock the data fetching to have the thunk dispatch the lifecycle actions. This is not optimal either because unit-testing reducers should be independent of thunks.
TLDR
I think it would be best if the three lifecycle action creators exposed by a thunk would be an official part of RTK’s API.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Best practices for unit testing with a React/Redux approach
Best practices for unit testing with a React/Redux approach · Actions & Async Action Creators · Reducers · Components · Connected Components ·...
Read more >Writing Tests | Redux
Reducers are pure functions that return the new state after applying the action to the previous state. In the majority of cases, the...
Read more >A nice way to test redux reducers and actions - part I
We'll explore some different options by testing a simple redux reducer. The reducer in question is responsible for managing a list of todos....
Read more >A better approach for testing your Redux code
Unit tests should interact with it as a whole. There is no point in testing reducers, action creators and selectors in isolation.
Read more >Redux ToolKit: is it possible to dispatch other actions from the ...
First of all: please note that reducers always need to be pure functions without side effects. So you can never dispatch anything there, ......
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
For the meantime, I chose option (2), that is, I wrote action creators to mock the lifecycle actions:
However, this requires redefining the action types (
PendingAction
,FulfilledAction
,RejectedAction
,RejectedWithValueAction
). Would you mind exporting these types to avoid the need for redefining them?Resolved in #1083 .