Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cacheLifecycle fix for fixedCacheKey in mutationThunk #4082

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
},
},
})
})
})