Skip to content

Commit 6ef71d0

Browse files
committed
add tests
1 parent e68b170 commit 6ef71d0

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ export interface QueryThunkArg
112112
type: 'query'
113113
originalArgs: unknown
114114
endpointName: string
115-
isPrefetch?: boolean
116115
}
117116

118117
export interface MutationThunkArg {
@@ -610,13 +609,12 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
610609
const force = hasTheForce(options) && options.force
611610
const maxAge = hasMaxAge(options) && options.ifOlderThan
612611

613-
const queryAction = (force: boolean = true) =>{
612+
const queryAction = (force: boolean = true) => {
614613
const options = { forceRefetch: force, isPrefetch: true }
615-
return (api.endpoints[endpointName] as ApiEndpointQuery<any, any>).initiate(
616-
arg,
617-
options
618-
)
619-
}
614+
return (
615+
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
616+
).initiate(arg, options)
617+
}
620618
const latestStateValue = (
621619
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
622620
).select(arg)(getState())

packages/toolkit/src/query/tests/buildThunks.test.tsx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { configureStore } from '@reduxjs/toolkit'
1+
import { configureStore, isAllOf } from '@reduxjs/toolkit'
22
import { createApi } from '@reduxjs/toolkit/query/react'
33
import { renderHook, waitFor } from '@testing-library/react'
4-
import { withProvider } from '../../tests/utils/helpers'
4+
import { actionsReducer, withProvider } from '../../tests/utils/helpers'
55
import type { BaseQueryApi } from '../baseQueryTypes'
66

77
test('handles a non-async baseQuery without error', async () => {
@@ -200,3 +200,52 @@ describe('re-triggering behavior on arg change', () => {
200200
}
201201
})
202202
})
203+
204+
describe('prefetch', () => {
205+
const baseQuery = () => ({ data: null })
206+
const api = createApi({
207+
baseQuery,
208+
endpoints: (build) => ({
209+
getUser: build.query<any, any>({
210+
query: (obj) => obj,
211+
}),
212+
}),
213+
})
214+
215+
const store = configureStore({
216+
reducer: { [api.reducerPath]: api.reducer, ...actionsReducer },
217+
middleware: (gDM) => gDM().concat(api.middleware),
218+
})
219+
it('should attach isPrefetch if prefetching', async () => {
220+
store.dispatch(api.util.prefetch('getUser', 1, {}))
221+
222+
await Promise.all(store.dispatch(api.util.getRunningQueriesThunk()))
223+
224+
const isPrefetch = (
225+
action: any,
226+
): action is { meta: { arg: { isPrefetch: true } } } =>
227+
action?.meta?.arg?.isPrefetch
228+
229+
expect(store.getState().actions).toMatchSequence(
230+
api.internalActions.middlewareRegistered.match,
231+
isAllOf(api.endpoints.getUser.matchPending, isPrefetch),
232+
isAllOf(api.endpoints.getUser.matchFulfilled, isPrefetch),
233+
)
234+
235+
// compare against a regular initiate call
236+
await store.dispatch(
237+
api.endpoints.getUser.initiate(1, { forceRefetch: true }),
238+
)
239+
240+
const isNotPrefetch = (action: any): action is unknown =>
241+
!isPrefetch(action)
242+
243+
expect(store.getState().actions).toMatchSequence(
244+
api.internalActions.middlewareRegistered.match,
245+
isAllOf(api.endpoints.getUser.matchPending, isPrefetch),
246+
isAllOf(api.endpoints.getUser.matchFulfilled, isPrefetch),
247+
isAllOf(api.endpoints.getUser.matchPending, isNotPrefetch),
248+
isAllOf(api.endpoints.getUser.matchFulfilled, isNotPrefetch),
249+
)
250+
})
251+
})

0 commit comments

Comments
 (0)