Skip to content

Commit e805dc9

Browse files
committed
added test suite for removeQueryData
1 parent dbf0574 commit e805dc9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/toolkit/src/query/tests/cacheCollection.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,43 @@ describe(`query: await cleanup, keepUnusedDataFor set`, () => {
156156
})
157157
})
158158

159+
describe('removeCacheData', () => {
160+
const { store, api } = storeForApi(
161+
createApi({
162+
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
163+
endpoints: (build) => ({
164+
query: build.query<unknown, string>({
165+
query: () => '/success',
166+
}),
167+
}),
168+
}),
169+
)
170+
171+
test('removeQueryData no impact if queryCacheKey different', async () => {
172+
const promise = store.dispatch(api.endpoints.query.initiate('arg'))
173+
await promise
174+
const initialResponse = api.endpoints.query.select('arg')(store.getState())
175+
expect(initialResponse.data).toBeDefined()
176+
177+
store.dispatch(api.util?.removeQueryData('query', 'argThatIsDifferent'))
178+
179+
const removedResult = api.endpoints.query.select('arg')(store.getState())
180+
expect(removedResult.data).toBeDefined()
181+
})
182+
183+
test('removeQueryData removes data of matching queryCacheKey', async () => {
184+
const promise = store.dispatch(api.endpoints.query.initiate('arg'))
185+
await promise
186+
const initialResponse = api.endpoints.query.select('arg')(store.getState())
187+
expect(initialResponse.data).toBeDefined()
188+
189+
store.dispatch(api.util?.removeQueryData('query', 'arg'))
190+
191+
const removedResult = api.endpoints.query.select('arg')(store.getState())
192+
expect(removedResult.data).toBeUndefined()
193+
})
194+
})
195+
159196
function storeForApi<
160197
A extends {
161198
reducerPath: 'api'

0 commit comments

Comments
 (0)