Skip to content

Commit e268789

Browse files
committed
add (currently failing) RTKQ test
1 parent 13be620 commit e268789

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

packages/toolkit/src/query/tests/matchers.test.tsx

+32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
hookWaitFor,
44
setupApiStore,
55
} from '@internal/tests/utils/helpers'
6+
import type { UnknownAction } from '@reduxjs/toolkit'
67
import { createSlice } from '@reduxjs/toolkit'
78
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
89
import { act, renderHook } from '@testing-library/react'
@@ -239,3 +240,34 @@ test('inferred types', () => {
239240
},
240241
})
241242
})
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

Comments
 (0)