From a9def3987111bcb16e23910e173639f46c1cda41 Mon Sep 17 00:00:00 2001 From: Pier Roberto Lucisano Date: Sat, 19 Oct 2024 17:22:40 +0200 Subject: [PATCH] Support null or undefined when invalidateTags is a function --- .../toolkit/src/query/endpointDefinitions.ts | 4 ++- .../src/query/tests/buildMiddleware.test.tsx | 32 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/toolkit/src/query/endpointDefinitions.ts b/packages/toolkit/src/query/endpointDefinitions.ts index fb2a93a430..8f7955468c 100644 --- a/packages/toolkit/src/query/endpointDefinitions.ts +++ b/packages/toolkit/src/query/endpointDefinitions.ts @@ -29,6 +29,7 @@ import type { OmitFromUnion, UnwrapPromise, } from './tsHelpers' +import { isNotNullish } from './utils' const resultType = /* @__PURE__ */ Symbol() const baseQuery = /* @__PURE__ */ Symbol() @@ -224,7 +225,7 @@ export type GetResultDescriptionFn< error: ErrorType | undefined, arg: QueryArg, meta: MetaType, -) => ReadonlyArray> +) => ReadonlyArray | undefined | null> export type FullTagDescription = { type: TagType @@ -778,6 +779,7 @@ export function calculateProvidedBy( queryArg, meta as MetaType, ) + .filter(isNotNullish) .map(expandTagDescription) .map(assertTagTypes) } diff --git a/packages/toolkit/src/query/tests/buildMiddleware.test.tsx b/packages/toolkit/src/query/tests/buildMiddleware.test.tsx index 53e19c9be8..0a03396302 100644 --- a/packages/toolkit/src/query/tests/buildMiddleware.test.tsx +++ b/packages/toolkit/src/query/tests/buildMiddleware.test.tsx @@ -25,9 +25,15 @@ const api = createApi({ }, providesTags: ['Bread'], }), + invalidateFruit: build.mutation({ + query: (fruit?: 'Banana' | 'Bread' | null) => ({ url: `invalidate/fruit/${fruit || ''}` }), + invalidatesTags(result, error, arg) { + return [arg] + } + }) }), }) -const { getBanana, getBread } = api.endpoints +const { getBanana, getBread, invalidateFruit } = api.endpoints const storeRef = setupApiStore(api, { ...actionsReducer, @@ -91,7 +97,10 @@ it('invalidates tags correctly when null or undefined are provided as tags', asy }) -it.each([{ tags: [undefined, null, 'Bread'] as Parameters['0'] }, { tags: [undefined, null], }, { tags: [] }])('does not invalidate with tags=$tags if no query matches', async ({ tags }) => { +it.each([ + { tags: [undefined, null, 'Bread'] as Parameters['0'] }, + { tags: [undefined, null], }, { tags: [] }] +)('does not invalidate with tags=$tags if no query matches', async ({ tags }) => { await storeRef.store.dispatch(getBanana.initiate(1)) await storeRef.store.dispatch(api.util.invalidateTags(tags)) @@ -107,4 +116,21 @@ it.each([{ tags: [undefined, null, 'Bread'] as Parameters { + await storeRef.store.dispatch(getBanana.initiate(1)) + await storeRef.store.dispatch(invalidateFruit.initiate(mutationArg)) + + // Slight pause to let the middleware run and such + await delay(20) + + const apiActions = [ + api.internalActions.middlewareRegistered.match, + getBanana.matchPending, + getBanana.matchFulfilled, + invalidateFruit.matchPending, + invalidateFruit.matchFulfilled, + ] + + expect(storeRef.store.getState().actions).toMatchSequence(...apiActions) +}) \ No newline at end of file