Dispatch success action for RTKQ endpoint in tests #4608
-
I'm struggling to write a unit test for a slice that listens to successful actions coming from a RTK-Query API slice. Instead of initializing a whole store with the two slices and then using the A working test I have for my reducer is this: it('should have 0 value for my value after initialization', () => {
const state = mySlice.reducer(initialState, { type: 'init' })
expect(state.myValue).toBe(0)
}) When I then try to set a similar test for a RTKQ success action, I don't know how to determine the right type for the action: it('should update my value when it has been persisted on server', () => {
const state = ebaSlice.reducer(
initialState,
myApi.endpoints.persistValue.???, // <- maybe an action creator, or a `type`
)
expect(state.myValue).toBe(1)
}) The slice is hooked to the API slice in the classic way: builder.addMatcher(myApi.endpoints.persistValue.matchFulfilled, (state, { payload }) => {
state.myValue = payload.value
}
}) Is there anything I can use to dispatch this successful action directly on the reducer? versions:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The type is going to be Generally the right answer here is to create a real store with the actual API slice and use the thunks as it would normally work. |
Beta Was this translation helpful? Give feedback.
The type is going to be
"myApi/execute/fulfilled"
, but it's made more specific with the endpoint name and request ID.Generally the right answer here is to create a real store with the actual API slice and use the thunks as it would normally work.