| 1 |
import { configureStore, isAllOf } from '@reduxjs/toolkit' |
| 2 |
import { createApi } from '@reduxjs/toolkit/query/react' |
| 3 |
import { renderHook, waitFor } from '@testing-library/react' |
| 4 |
import { actionsReducer, withProvider } from '../../tests/utils/helpers' |
| 5 |
import type { BaseQueryApi } from '../baseQueryTypes' |
| 6 |
|
| 7 |
test('handles a non-async baseQuery without error', async () => { |
| 8 |
const baseQuery = (args?: any) => ({ data: args }) |
| 9 |
const api = createApi({ |
| 10 |
baseQuery, |
| 11 |
endpoints: (build) => ({ |
| 12 |
getUser: build.query<unknown, number>({ |
| 13 |
query(id) { |
| 14 |
return { url: `user/${id}` } |
| 15 |
}, |
| 16 |
}), |
| 17 |
}), |
| 18 |
}) |
| 19 |
const { getUser } = api.endpoints |
| 20 |
const store = configureStore({ |
| 21 |
reducer: { |
| 22 |
[api.reducerPath]: api.reducer, |
| 23 |
}, |
| 24 |
middleware: (gDM) => gDM().concat(api.middleware), |
| 25 |
}) |
| 26 |
|
| 27 |
const promise = store.dispatch(getUser.initiate(1)) |
| 28 |
const { data } = await promise |
| 29 |
|
| 30 |
expect(data).toEqual({ |
| 31 |
url: 'user/1', |
| 32 |
}) |
| 33 |
|
| 34 |
const storeResult = getUser.select(1)(store.getState()) |
| 35 |
expect(storeResult).toEqual({ |
| 36 |
data: { |
| 37 |
url: 'user/1', |
| 38 |
}, |
| 39 |
endpointName: 'getUser', |
| 40 |
isError: false, |
| 41 |
isLoading: false, |
| 42 |
isSuccess: true, |
| 43 |
isUninitialized: false, |
| 44 |
originalArgs: 1, |
| 45 |
requestId: expect.any(String), |
| 46 |
status: 'fulfilled', |
| 47 |
startedTimeStamp: expect.any(Number), |
| 48 |
fulfilledTimeStamp: expect.any(Number), |
| 49 |
}) |
| 50 |
}) |
| 51 |
|
| 52 |
test('passes the extraArgument property to the baseQueryApi', async () => { |
| 53 |
const baseQuery = (_args: any, api: BaseQueryApi) => ({ data: api.extra }) |
| 54 |
const api = createApi({ |
| 55 |
baseQuery, |
| 56 |
endpoints: (build) => ({ |
| 57 |
getUser: build.query<unknown, void>({ |
| 58 |
query: () => '', |
| 59 |
}), |
| 60 |
}), |
| 61 |
}) |
| 62 |
const store = configureStore({ |
| 63 |
reducer: { |
| 64 |
[api.reducerPath]: api.reducer, |
| 65 |
}, |
| 66 |
middleware: (gDM) => |
| 67 |
gDM({ thunk: { extraArgument: 'cakes' } }).concat(api.middleware), |
| 68 |
}) |
| 69 |
const { getUser } = api.endpoints |
| 70 |
const { data } = await store.dispatch(getUser.initiate()) |
| 71 |
expect(data).toBe('cakes') |
| 72 |
}) |
| 73 |
|
| 74 |
describe('re-triggering behavior on arg change', () => { |
| 75 |
const api = createApi({ |
| 76 |
baseQuery: () => ({ data: null }), |
| 77 |
endpoints: (build) => ({ |
| 78 |
getUser: build.query<any, any>({ |
| 79 |
query: (obj) => obj, |
| 80 |
}), |
| 81 |
}), |
| 82 |
}) |
| 83 |
const { getUser } = api.endpoints |
| 84 |
const store = configureStore({ |
| 85 |
reducer: { [api.reducerPath]: api.reducer }, |
| 86 |
middleware: (gDM) => gDM().concat(api.middleware), |
| 87 |
}) |
| 88 |
|
| 89 |
const spy = vi.spyOn(getUser, 'initiate') |
| 90 |
beforeEach(() => void spy.mockClear()) |
| 91 |
|
| 92 |
test('re-trigger on literal value change', async () => { |
| 93 |
const { result, rerender } = renderHook( |
| 94 |
(props) => getUser.useQuery(props), |
| 95 |
{ |
| 96 |
wrapper: withProvider(store), |
| 97 |
initialProps: 5, |
| 98 |
}, |
| 99 |
) |
| 100 |
|
| 101 |
await waitFor(() => { |
| 102 |
expect(result.current.status).not.toBe('pending') |
| 103 |
}) |
| 104 |
|
| 105 |
expect(spy).toHaveBeenCalledOnce() |
| 106 |
|
| 107 |
for (let x = 1; x < 3; x++) { |
| 108 |
rerender(6) |
| 109 |
await waitFor(() => { |
| 110 |
expect(result.current.status).not.toBe('pending') |
| 111 |
}) |
| 112 |
expect(spy).toHaveBeenCalledTimes(2) |
| 113 |
} |
| 114 |
|
| 115 |
for (let x = 1; x < 3; x++) { |
| 116 |
rerender(7) |
| 117 |
await waitFor(() => { |
| 118 |
expect(result.current.status).not.toBe('pending') |
| 119 |
}) |
| 120 |
expect(spy).toHaveBeenCalledTimes(3) |
| 121 |
} |
| 122 |
}) |
| 123 |
|
| 124 |
test('only re-trigger on shallow-equal arg change', async () => { |
| 125 |
const { result, rerender } = renderHook( |
| 126 |
(props) => getUser.useQuery(props), |
| 127 |
{ |
| 128 |
wrapper: withProvider(store), |
| 129 |
initialProps: { name: 'Bob', likes: 'iceCream' }, |
| 130 |
}, |
| 131 |
) |
| 132 |
|
| 133 |
await waitFor(() => { |
| 134 |
expect(result.current.status).not.toBe('pending') |
| 135 |
}) |
| 136 |
expect(spy).toHaveBeenCalledOnce() |
| 137 |
|
| 138 |
for (let x = 1; x < 3; x++) { |
| 139 |
rerender({ name: 'Bob', likes: 'waffles' }) |
| 140 |
await waitFor(() => { |
| 141 |
expect(result.current.status).not.toBe('pending') |
| 142 |
}) |
| 143 |
expect(spy).toHaveBeenCalledTimes(2) |
| 144 |
} |
| 145 |
|
| 146 |
for (let x = 1; x < 3; x++) { |
| 147 |
rerender({ name: 'Alice', likes: 'waffles' }) |
| 148 |
await waitFor(() => { |
| 149 |
expect(result.current.status).not.toBe('pending') |
| 150 |
}) |
| 151 |
expect(spy).toHaveBeenCalledTimes(3) |
| 152 |
} |
| 153 |
}) |
| 154 |
|
| 155 |
test('re-triggers every time on deeper value changes', async () => { |
| 156 |
const name = 'Tim' |
| 157 |
|
| 158 |
const { result, rerender } = renderHook( |
| 159 |
(props) => getUser.useQuery(props), |
| 160 |
{ |
| 161 |
wrapper: withProvider(store), |
| 162 |
initialProps: { person: { name } }, |
| 163 |
}, |
| 164 |
) |
| 165 |
|
| 166 |
await waitFor(() => { |
| 167 |
expect(result.current.status).not.toBe('pending') |
| 168 |
}) |
| 169 |
expect(spy).toHaveBeenCalledOnce() |
| 170 |
|
| 171 |
for (let x = 1; x < 3; x++) { |
| 172 |
rerender({ person: { name: name + x } }) |
| 173 |
await waitFor(() => { |
| 174 |
expect(result.current.status).not.toBe('pending') |
| 175 |
}) |
| 176 |
expect(spy).toHaveBeenCalledTimes(x + 1) |
| 177 |
} |
| 178 |
}) |
| 179 |
|
| 180 |
test('do not re-trigger if the order of keys change while maintaining the same values', async () => { |
| 181 |
const { result, rerender } = renderHook( |
| 182 |
(props) => getUser.useQuery(props), |
| 183 |
{ |
| 184 |
wrapper: withProvider(store), |
| 185 |
initialProps: { name: 'Tim', likes: 'Bananas' }, |
| 186 |
}, |
| 187 |
) |
| 188 |
|
| 189 |
await waitFor(() => { |
| 190 |
expect(result.current.status).not.toBe('pending') |
| 191 |
}) |
| 192 |
expect(spy).toHaveBeenCalledOnce() |
| 193 |
|
| 194 |
for (let x = 1; x < 3; x++) { |
| 195 |
rerender({ likes: 'Bananas', name: 'Tim' }) |
| 196 |
await waitFor(() => { |
| 197 |
expect(result.current.status).not.toBe('pending') |
| 198 |
}) |
| 199 |
expect(spy).toHaveBeenCalledOnce() |
| 200 |
} |
| 201 |
}) |
| 202 |
}) |
| 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 |
}) |