|
3 | 3 | hookWaitFor,
|
4 | 4 | setupApiStore,
|
5 | 5 | } from '@internal/tests/utils/helpers'
|
| 6 | +import type { UnknownAction } from '@reduxjs/toolkit' |
6 | 7 | import { createSlice } from '@reduxjs/toolkit'
|
7 | 8 | import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
8 | 9 | import { act, renderHook } from '@testing-library/react'
|
@@ -239,3 +240,34 @@ test('inferred types', () => {
|
239 | 240 | },
|
240 | 241 | })
|
241 | 242 | })
|
| 243 | + |
| 244 | +describe('errors in reducers are not swallowed', () => { |
| 245 | + const faultyStoreFor = (matcher: (action: UnknownAction) => boolean) => |
| 246 | + setupApiStore(api, { |
| 247 | + ...actionsReducer, |
| 248 | + faultyReducer(state = null, action: UnknownAction) { |
| 249 | + if (matcher(action)) { |
| 250 | + throw new Error('reducer error') |
| 251 | + } |
| 252 | + return state |
| 253 | + }, |
| 254 | + }) |
| 255 | + test('pending action reducer errors should be thrown', async () => { |
| 256 | + const storeRef = faultyStoreFor(api.endpoints.querySuccess.matchPending) |
| 257 | + await expect( |
| 258 | + storeRef.store.dispatch(querySuccess2.initiate({} as any)), |
| 259 | + ).rejects.toThrow('reducer error') |
| 260 | + }) |
| 261 | + test('fulfilled action reducer errors should be thrown', async () => { |
| 262 | + const storeRef = faultyStoreFor(api.endpoints.querySuccess.matchFulfilled) |
| 263 | + await expect( |
| 264 | + storeRef.store.dispatch(querySuccess2.initiate({} as any)), |
| 265 | + ).rejects.toThrow('reducer error') |
| 266 | + }) |
| 267 | + test('rejected action reducer errors should be thrown', async () => { |
| 268 | + const storeRef = faultyStoreFor(api.endpoints.queryFail.matchRejected) |
| 269 | + await expect( |
| 270 | + storeRef.store.dispatch(queryFail.initiate({} as any)), |
| 271 | + ).rejects.toThrow('reducer error') |
| 272 | + }) |
| 273 | +}) |
0 commit comments