Skip to content

Commit 111b466

Browse files
committed
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit into fix-codemods
2 parents dc5db44 + 9b772d9 commit 111b466

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

packages/toolkit/src/query/core/buildMiddleware/cacheLifecycle.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
253253

254254
function getCacheKey(action: any) {
255255
if (isQueryThunk(action)) return action.meta.arg.queryCacheKey
256-
if (isMutationThunk(action)) return action.meta.requestId
256+
if (isMutationThunk(action)) {
257+
return action.meta.arg.fixedCacheKey ?? action.meta.requestId
258+
}
257259
if (api.internalActions.removeQueryResult.match(action))
258260
return action.payload.queryCacheKey
259261
if (api.internalActions.removeMutationResult.match(action))

packages/toolkit/src/query/tests/useMutation-fixedCacheKey.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import {
88
waitFor,
99
act,
1010
} from '@testing-library/react'
11+
import { vi } from 'vitest'
1112

1213
describe('fixedCacheKey', () => {
14+
const onNewCacheEntry = vi.fn()
15+
1316
const api = createApi({
1417
async baseQuery(arg: string | Promise<string>) {
1518
return { data: await arg }
@@ -354,4 +357,38 @@ describe('fixedCacheKey', () => {
354357
expect(getByTestId(c1, 'status').textContent).toBe('fulfilled')
355358
expect(getByTestId(c1, 'data').textContent).toBe('this should be visible')
356359
})
360+
361+
test('using fixedCacheKey should create a new cache entry', async () => {
362+
api.enhanceEndpoints({
363+
endpoints: {
364+
send: {
365+
onCacheEntryAdded: (arg) => onNewCacheEntry(arg),
366+
},
367+
},
368+
})
369+
370+
render(<Component name="C1" fixedCacheKey={'testKey'} />, {
371+
wrapper: storeRef.wrapper,
372+
})
373+
374+
let c1 = screen.getByTestId('C1')
375+
376+
expect(getByTestId(c1, 'status').textContent).toBe('uninitialized')
377+
expect(getByTestId(c1, 'originalArgs').textContent).toBe('undefined')
378+
379+
await act(async () => {
380+
getByTestId(c1, 'trigger').click()
381+
await Promise.resolve()
382+
})
383+
384+
expect(onNewCacheEntry).toHaveBeenCalledWith('C1')
385+
386+
api.enhanceEndpoints({
387+
endpoints: {
388+
send: {
389+
onCacheEntryAdded: undefined,
390+
},
391+
},
392+
})
393+
})
357394
})

0 commit comments

Comments
 (0)