From e268789f3dcf65404593a0f3927d354a0ad5b018 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Thu, 2 May 2024 17:38:26 +0100 Subject: [PATCH] add (currently failing) RTKQ test --- .../toolkit/src/query/tests/matchers.test.tsx | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/toolkit/src/query/tests/matchers.test.tsx b/packages/toolkit/src/query/tests/matchers.test.tsx index 5b26740e98..1c0b6c6432 100644 --- a/packages/toolkit/src/query/tests/matchers.test.tsx +++ b/packages/toolkit/src/query/tests/matchers.test.tsx @@ -3,6 +3,7 @@ import { hookWaitFor, setupApiStore, } from '@internal/tests/utils/helpers' +import type { UnknownAction } from '@reduxjs/toolkit' import { createSlice } from '@reduxjs/toolkit' import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' import { act, renderHook } from '@testing-library/react' @@ -239,3 +240,34 @@ test('inferred types', () => { }, }) }) + +describe('errors in reducers are not swallowed', () => { + const faultyStoreFor = (matcher: (action: UnknownAction) => boolean) => + setupApiStore(api, { + ...actionsReducer, + faultyReducer(state = null, action: UnknownAction) { + if (matcher(action)) { + throw new Error('reducer error') + } + return state + }, + }) + test('pending action reducer errors should be thrown', async () => { + const storeRef = faultyStoreFor(api.endpoints.querySuccess.matchPending) + await expect( + storeRef.store.dispatch(querySuccess2.initiate({} as any)), + ).rejects.toThrow('reducer error') + }) + test('fulfilled action reducer errors should be thrown', async () => { + const storeRef = faultyStoreFor(api.endpoints.querySuccess.matchFulfilled) + await expect( + storeRef.store.dispatch(querySuccess2.initiate({} as any)), + ).rejects.toThrow('reducer error') + }) + test('rejected action reducer errors should be thrown', async () => { + const storeRef = faultyStoreFor(api.endpoints.queryFail.matchRejected) + await expect( + storeRef.store.dispatch(queryFail.initiate({} as any)), + ).rejects.toThrow('reducer error') + }) +})