| 1 |
import { createApi } from '@reduxjs/toolkit/query' |
| 2 |
import { delay } from 'msw' |
| 3 |
import { actionsReducer, setupApiStore } from '../../tests/utils/helpers' |
| 4 |
|
| 5 |
const baseQuery = (args?: any) => ({ data: args }) |
| 6 |
const api = createApi({ |
| 7 |
baseQuery, |
| 8 |
tagTypes: ['Banana', 'Bread'], |
| 9 |
endpoints: (build) => ({ |
| 10 |
getBanana: build.query<unknown, number>({ |
| 11 |
query(id) { |
| 12 |
return { url: `banana/${id}` } |
| 13 |
}, |
| 14 |
providesTags: ['Banana'], |
| 15 |
}), |
| 16 |
getBananas: build.query<unknown, void>({ |
| 17 |
query() { |
| 18 |
return { url: 'bananas' } |
| 19 |
}, |
| 20 |
providesTags: ['Banana'], |
| 21 |
}), |
| 22 |
getBread: build.query<unknown, number>({ |
| 23 |
query(id) { |
| 24 |
return { url: `bread/${id}` } |
| 25 |
}, |
| 26 |
providesTags: ['Bread'], |
| 27 |
}), |
| 28 |
invalidateFruit: build.mutation({ |
| 29 |
query: (fruit?: 'Banana' | 'Bread' | null) => ({ url: `invalidate/fruit/${fruit || ''}` }), |
| 30 |
invalidatesTags(result, error, arg) { |
| 31 |
return [arg] |
| 32 |
} |
| 33 |
}) |
| 34 |
}), |
| 35 |
}) |
| 36 |
const { getBanana, getBread, invalidateFruit } = api.endpoints |
| 37 |
|
| 38 |
const storeRef = setupApiStore(api, { |
| 39 |
...actionsReducer, |
| 40 |
}) |
| 41 |
|
| 42 |
it('invalidates the specified tags', async () => { |
| 43 |
await storeRef.store.dispatch(getBanana.initiate(1)) |
| 44 |
expect(storeRef.store.getState().actions).toMatchSequence( |
| 45 |
api.internalActions.middlewareRegistered.match, |
| 46 |
getBanana.matchPending, |
| 47 |
getBanana.matchFulfilled, |
| 48 |
) |
| 49 |
|
| 50 |
await storeRef.store.dispatch(api.util.invalidateTags(['Banana', 'Bread'])) |
| 51 |
|
| 52 |
// Slight pause to let the middleware run and such |
| 53 |
await delay(20) |
| 54 |
|
| 55 |
const firstSequence = [ |
| 56 |
api.internalActions.middlewareRegistered.match, |
| 57 |
getBanana.matchPending, |
| 58 |
getBanana.matchFulfilled, |
| 59 |
api.util.invalidateTags.match, |
| 60 |
getBanana.matchPending, |
| 61 |
getBanana.matchFulfilled, |
| 62 |
] |
| 63 |
expect(storeRef.store.getState().actions).toMatchSequence(...firstSequence) |
| 64 |
|
| 65 |
await storeRef.store.dispatch(getBread.initiate(1)) |
| 66 |
await storeRef.store.dispatch(api.util.invalidateTags([{ type: 'Bread' }])) |
| 67 |
|
| 68 |
await delay(20) |
| 69 |
|
| 70 |
expect(storeRef.store.getState().actions).toMatchSequence( |
| 71 |
...firstSequence, |
| 72 |
getBread.matchPending, |
| 73 |
getBread.matchFulfilled, |
| 74 |
api.util.invalidateTags.match, |
| 75 |
getBread.matchPending, |
| 76 |
getBread.matchFulfilled, |
| 77 |
) |
| 78 |
}) |
| 79 |
|
| 80 |
it('invalidates tags correctly when null or undefined are provided as tags', async() =>{ |
| 81 |
await storeRef.store.dispatch(getBanana.initiate(1)) |
| 82 |
await storeRef.store.dispatch(api.util.invalidateTags([undefined, null, 'Banana'])) |
| 83 |
|
| 84 |
// Slight pause to let the middleware run and such |
| 85 |
await delay(20) |
| 86 |
|
| 87 |
const apiActions = [ |
| 88 |
api.internalActions.middlewareRegistered.match, |
| 89 |
getBanana.matchPending, |
| 90 |
getBanana.matchFulfilled, |
| 91 |
api.util.invalidateTags.match, |
| 92 |
getBanana.matchPending, |
| 93 |
getBanana.matchFulfilled, |
| 94 |
] |
| 95 |
|
| 96 |
expect(storeRef.store.getState().actions).toMatchSequence(...apiActions) |
| 97 |
}) |
| 98 |
|
| 99 |
|
| 100 |
it.each([ |
| 101 |
{ tags: [undefined, null, 'Bread'] as Parameters<typeof api.util.invalidateTags>['0'] }, |
| 102 |
{ tags: [undefined, null], }, { tags: [] }] |
| 103 |
)('does not invalidate with tags=$tags if no query matches', async ({ tags }) => { |
| 104 |
await storeRef.store.dispatch(getBanana.initiate(1)) |
| 105 |
await storeRef.store.dispatch(api.util.invalidateTags(tags)) |
| 106 |
|
| 107 |
// Slight pause to let the middleware run and such |
| 108 |
await delay(20) |
| 109 |
|
| 110 |
const apiActions = [ |
| 111 |
api.internalActions.middlewareRegistered.match, |
| 112 |
getBanana.matchPending, |
| 113 |
getBanana.matchFulfilled, |
| 114 |
api.util.invalidateTags.match, |
| 115 |
] |
| 116 |
|
| 117 |
expect(storeRef.store.getState().actions).toMatchSequence(...apiActions) |
| 118 |
}) |
| 119 |
|
| 120 |
it.each([{ mutationArg: 'Bread' as "Bread" | null | undefined }, { mutationArg: undefined }, { mutationArg: null }])('does not invalidate queries when a mutation with tags=[$mutationArg] runs and does not match anything', async ({ mutationArg }) => { |
| 121 |
await storeRef.store.dispatch(getBanana.initiate(1)) |
| 122 |
await storeRef.store.dispatch(invalidateFruit.initiate(mutationArg)) |
| 123 |
|
| 124 |
// Slight pause to let the middleware run and such |
| 125 |
await delay(20) |
| 126 |
|
| 127 |
const apiActions = [ |
| 128 |
api.internalActions.middlewareRegistered.match, |
| 129 |
getBanana.matchPending, |
| 130 |
getBanana.matchFulfilled, |
| 131 |
invalidateFruit.matchPending, |
| 132 |
invalidateFruit.matchFulfilled, |
| 133 |
] |
| 134 |
|
| 135 |
expect(storeRef.store.getState().actions).toMatchSequence(...apiActions) |
| 136 |
}) |