Skip to content

Commit

Permalink
Merge pull request #4082 from riqts/OnCacheEntryAdded-work-with-fixed…
Browse files Browse the repository at this point in the history
…CacheKey

cacheLifecycle fix for fixedCacheKey in mutationThunk
  • Loading branch information
EskiMojo14 committed Jan 16, 2024
2 parents 022fb94 + 8e45462 commit 9b772d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({

function getCacheKey(action: any) {
if (isQueryThunk(action)) return action.meta.arg.queryCacheKey
if (isMutationThunk(action)) return action.meta.requestId
if (isMutationThunk(action)) {
return action.meta.arg.fixedCacheKey ?? action.meta.requestId
}
if (api.internalActions.removeQueryResult.match(action))
return action.payload.queryCacheKey
if (api.internalActions.removeMutationResult.match(action))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import {
waitFor,
act,
} from '@testing-library/react'
import { vi } from 'vitest'

describe('fixedCacheKey', () => {
const onNewCacheEntry = vi.fn()

const api = createApi({
async baseQuery(arg: string | Promise<string>) {
return { data: await arg }
Expand Down Expand Up @@ -354,4 +357,38 @@ describe('fixedCacheKey', () => {
expect(getByTestId(c1, 'status').textContent).toBe('fulfilled')
expect(getByTestId(c1, 'data').textContent).toBe('this should be visible')
})

test('using fixedCacheKey should create a new cache entry', async () => {
api.enhanceEndpoints({
endpoints: {
send: {
onCacheEntryAdded: (arg) => onNewCacheEntry(arg),
},
},
})

render(<Component name="C1" fixedCacheKey={'testKey'} />, {
wrapper: storeRef.wrapper,
})

let c1 = screen.getByTestId('C1')

expect(getByTestId(c1, 'status').textContent).toBe('uninitialized')
expect(getByTestId(c1, 'originalArgs').textContent).toBe('undefined')

await act(async () => {
getByTestId(c1, 'trigger').click()
await Promise.resolve()
})

expect(onNewCacheEntry).toHaveBeenCalledWith('C1')

api.enhanceEndpoints({
endpoints: {
send: {
onCacheEntryAdded: undefined,
},
},
})
})
})

0 comments on commit 9b772d9

Please sign in to comment.