Testing createAsyncThunk with multiple dispatch calls
See original GitHub issueHello! Ive rewritten old thunks using createAsyncThunk helper.
So Im curious is it able to test like this
const requestSomething1 = createAsyncThunk('something1', (someArgs) => {
// simple await/return thunk like in example
})
const requestSomething2 = createAsyncThunk('something2', (someArgs, {dispatch}) => {
const someData = await api.get('url', someArgs)
await dispatch(requestSomething1(someData.someParam))
return someData
})
Since the helper generates requestId
on every action and pass it in the meta
I can test requestSomething1
like this:
it('some description', async () => {
// some preparations with redux-mock-store
const { meta: { requestId }} = await mockedStore.dispatch(requestSomething1(someArgs))
const actions = store.getActions()
const expectedActions = [
requestSomething1.pending(requestId , someArgs),
requestSomething1.fulfilled(mockedResponsePayload, requestId , someArgs)
]
expect(actions).toEqual(expectedActions)
})
So it impossible to do the same for requestSomething2 because I dont know the inner requestId.
const { meta: { requestId }} = await mockedStore.dispatch(requestSomething2(someArgs))
const actions = store.getActions()
const expectedActions = [
requestSomething2.pending(requestId , someArgs),
requestSomething1.pending(\* what is here? *\, someArgs),
requestSomething1.fulfilled(mockedResponsePayload1, \* what is here? *\, someArgs),
requestSomething2.fulfilled(mockedResponsePayload2, requestId , someArgs)
]
expect(actions).toEqual(expectedActions)
The first solution was to add the all requestIds into meta
in the body of requestSomething2
but I can return only data and have no access to meta
field. And we only have some customization of the payload with rejectWithValue
but its for reject only.
Another option is adding the ids into result like return {data, requestIds: {request1: id, request2: id .... }}
but I have felt its wrong in different cases
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8
Top Results From Across the Web
Testing createAsyncThunk Redux Toolkit Jest - Stack Overflow
Run your jest.mock calls to mock any API/hooks you may be using to ... createAsyncThunk( 'accounts/refreshSession', async (_, { dispatch }) ...
Read more >createAsyncThunk - Redux Toolkit
If you need to pass in multiple values, pass them together in an object when you dispatch the thunk, like dispatch(fetchUsers({status: 'active', ...
Read more >redux-toolkit: test slice and actions - Fabio Marcoccia - Medium
It's signficant to see how the store changes when an action has been dispatched. For example, we have these actions: export const fetchUser...
Read more >Unit Testing Redux Thunks with a Mock Dispatch Function
For this lesson we're testing a thunk created using Redux Toolkit's createAsyncThunk method. The test should continue work completely untouched even if we...
Read more >Chaining Asynchronous Dispatches in Redux Toolkit - YouTube
In this video I cover how we can correctly dispatch one action after another, where the second dispatch is informed by the state...
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
I realize this was closed, but this was one of the things I stumbled upon that solves the problem and this should help for anyone testing specific thunks.
Since
createAsyncThunk
returns a function for later execution, you can use this to your advantage. Instead of going through the hassle of testing an entire store’s interaction with your thunks, you can test the thunks themselves in isolation away from a store.Hope this helps 🙂. I tried my best to provide as complete of an example as humanly possible for easier reference.
The API
Getting an HTTP Client
Use the API clients
The Thunk
The Test
Updated the original code to make full usage more obvious