Debian Codegraph @

install/freeipa-webui/node_modules/@reduxjs/toolkit/src/query/tests/queryLifecycle.test.tsx

raw
1 import { server } from '@internal/query/tests/mocks/server'
2 import { setupApiStore } from '@internal/tests/utils/helpers'
3 import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
4 import { waitFor } from '@testing-library/react'
5 import { HttpResponse, http } from 'msw'
6 import { vi } from 'vitest'
7
8 const api = createApi({
9 baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
10 endpoints: () => ({}),
11 })
12 const storeRef = setupApiStore(api)
13
14 const onStart = vi.fn()
15 const onSuccess = vi.fn()
16 const onError = vi.fn()
17
18 beforeEach(() => {
19 onStart.mockClear()
20 onSuccess.mockClear()
21 onError.mockClear()
22 })
23
24 describe.each([['query'], ['mutation']] as const)(
25 'generic cases: %s',
26 (type) => {
27 test(`${type}: onStart only`, async () => {
28 const extended = api.injectEndpoints({
29 overrideExisting: true,
30 endpoints: (build) => ({
31 injected: build[type as 'mutation']<unknown, string>({
32 query: () => '/success',
33 onQueryStarted(arg) {
34 onStart(arg)
35 },
36 }),
37 }),
38 })
39 storeRef.store.dispatch(extended.endpoints.injected.initiate('arg'))
40 expect(onStart).toHaveBeenCalledWith('arg')
41 })
42
43 test(`${type}: onStart and onSuccess`, async () => {
44 const extended = api.injectEndpoints({
45 overrideExisting: true,
46 endpoints: (build) => ({
47 injected: build[type as 'mutation']<number, string>({
48 query: () => '/success',
49 async onQueryStarted(arg, { queryFulfilled }) {
50 onStart(arg)
51 // awaiting without catching like this would result in an `unhandledRejection` exception if there was an error
52 // unfortunately we cannot test for that in jest.
53 const result = await queryFulfilled
54 onSuccess(result)
55 },
56 }),
57 }),
58 })
59 storeRef.store.dispatch(extended.endpoints.injected.initiate('arg'))
60 expect(onStart).toHaveBeenCalledWith('arg')
61 await waitFor(() => {
62 expect(onSuccess).toHaveBeenCalledWith({
63 data: { value: 'success' },
64 meta: {
65 request: expect.any(Request),
66 response: expect.any(Object), // Response is not available in jest env
67 },
68 })
69 })
70 })
71
72 test(`${type}: onStart and onError`, async () => {
73 const extended = api.injectEndpoints({
74 overrideExisting: true,
75 endpoints: (build) => ({
76 injected: build[type as 'mutation']<unknown, string>({
77 query: () => '/error',
78 async onQueryStarted(arg, { queryFulfilled }) {
79 onStart(arg)
80 try {
81 const result = await queryFulfilled
82 onSuccess(result)
83 } catch (e) {
84 onError(e)
85 }
86 },
87 }),
88 }),
89 })
90 storeRef.store.dispatch(extended.endpoints.injected.initiate('arg'))
91 expect(onStart).toHaveBeenCalledWith('arg')
92 await waitFor(() => {
93 expect(onError).toHaveBeenCalledWith({
94 error: {
95 status: 500,
96 data: { value: 'error' },
97 },
98 isUnhandledError: false,
99 meta: {
100 request: expect.any(Request),
101 response: expect.any(Object), // Response is not available in jest env
102 },
103 })
104 })
105 expect(onSuccess).not.toHaveBeenCalled()
106 })
107 },
108 )
109
110 test('query: getCacheEntry (success)', async () => {
111 const snapshot = vi.fn()
112 const extended = api.injectEndpoints({
113 overrideExisting: true,
114 endpoints: (build) => ({
115 injected: build.query<unknown, string>({
116 query: () => '/success',
117 async onQueryStarted(
118 arg,
119 { dispatch, getState, getCacheEntry, queryFulfilled },
120 ) {
121 try {
122 snapshot(getCacheEntry())
123 const result = await queryFulfilled
124 onSuccess(result)
125 snapshot(getCacheEntry())
126 } catch (e) {
127 onError(e)
128 snapshot(getCacheEntry())
129 }
130 },
131 }),
132 }),
133 })
134 const promise = storeRef.store.dispatch(
135 extended.endpoints.injected.initiate('arg'),
136 )
137
138 await waitFor(() => {
139 expect(onSuccess).toHaveBeenCalled()
140 })
141
142 expect(snapshot).toHaveBeenCalledTimes(2)
143 expect(snapshot.mock.calls[0][0]).toMatchObject({
144 endpointName: 'injected',
145 isError: false,
146 isLoading: true,
147 isSuccess: false,
148 isUninitialized: false,
149 originalArgs: 'arg',
150 requestId: promise.requestId,
151 startedTimeStamp: expect.any(Number),
152 status: 'pending',
153 })
154 expect(snapshot.mock.calls[1][0]).toMatchObject({
155 data: {
156 value: 'success',
157 },
158 endpointName: 'injected',
159 fulfilledTimeStamp: expect.any(Number),
160 isError: false,
161 isLoading: false,
162 isSuccess: true,
163 isUninitialized: false,
164 originalArgs: 'arg',
165 requestId: promise.requestId,
166 startedTimeStamp: expect.any(Number),
167 status: 'fulfilled',
168 })
169 })
170
171 test('query: getCacheEntry (error)', async () => {
172 const snapshot = vi.fn()
173 const extended = api.injectEndpoints({
174 overrideExisting: true,
175 endpoints: (build) => ({
176 injected: build.query<unknown, string>({
177 query: () => '/error',
178 async onQueryStarted(
179 arg,
180 { dispatch, getState, getCacheEntry, queryFulfilled },
181 ) {
182 try {
183 snapshot(getCacheEntry())
184 const result = await queryFulfilled
185 onSuccess(result)
186 snapshot(getCacheEntry())
187 } catch (e) {
188 onError(e)
189 snapshot(getCacheEntry())
190 }
191 },
192 }),
193 }),
194 })
195 const promise = storeRef.store.dispatch(
196 extended.endpoints.injected.initiate('arg'),
197 )
198
199 await waitFor(() => {
200 expect(onError).toHaveBeenCalled()
201 })
202
203 expect(snapshot.mock.calls[0][0]).toMatchObject({
204 endpointName: 'injected',
205 isError: false,
206 isLoading: true,
207 isSuccess: false,
208 isUninitialized: false,
209 originalArgs: 'arg',
210 requestId: promise.requestId,
211 startedTimeStamp: expect.any(Number),
212 status: 'pending',
213 })
214 expect(snapshot.mock.calls[1][0]).toMatchObject({
215 error: {
216 data: { value: 'error' },
217 status: 500,
218 },
219 endpointName: 'injected',
220 isError: true,
221 isLoading: false,
222 isSuccess: false,
223 isUninitialized: false,
224 originalArgs: 'arg',
225 requestId: promise.requestId,
226 startedTimeStamp: expect.any(Number),
227 status: 'rejected',
228 })
229 })
230
231 test('mutation: getCacheEntry (success)', async () => {
232 const snapshot = vi.fn()
233 const extended = api.injectEndpoints({
234 overrideExisting: true,
235 endpoints: (build) => ({
236 injected: build.mutation<unknown, string>({
237 query: () => '/success',
238 async onQueryStarted(
239 arg,
240 { dispatch, getState, getCacheEntry, queryFulfilled },
241 ) {
242 try {
243 snapshot(getCacheEntry())
244 const result = await queryFulfilled
245 onSuccess(result)
246 snapshot(getCacheEntry())
247 } catch (e) {
248 onError(e)
249 snapshot(getCacheEntry())
250 }
251 },
252 }),
253 }),
254 })
255 const promise = storeRef.store.dispatch(
256 extended.endpoints.injected.initiate('arg'),
257 )
258
259 await waitFor(() => {
260 expect(onSuccess).toHaveBeenCalled()
261 })
262
263 expect(snapshot).toHaveBeenCalledTimes(2)
264 expect(snapshot.mock.calls[0][0]).toMatchObject({
265 endpointName: 'injected',
266 isError: false,
267 isLoading: true,
268 isSuccess: false,
269 isUninitialized: false,
270 startedTimeStamp: expect.any(Number),
271 status: 'pending',
272 })
273 expect(snapshot.mock.calls[1][0]).toMatchObject({
274 data: {
275 value: 'success',
276 },
277 endpointName: 'injected',
278 fulfilledTimeStamp: expect.any(Number),
279 isError: false,
280 isLoading: false,
281 isSuccess: true,
282 isUninitialized: false,
283 startedTimeStamp: expect.any(Number),
284 status: 'fulfilled',
285 })
286 })
287
288 test('mutation: getCacheEntry (error)', async () => {
289 const snapshot = vi.fn()
290 const extended = api.injectEndpoints({
291 overrideExisting: true,
292 endpoints: (build) => ({
293 injected: build.mutation<unknown, string>({
294 query: () => '/error',
295 async onQueryStarted(
296 arg,
297 { dispatch, getState, getCacheEntry, queryFulfilled },
298 ) {
299 try {
300 snapshot(getCacheEntry())
301 const result = await queryFulfilled
302 onSuccess(result)
303 snapshot(getCacheEntry())
304 } catch (e) {
305 onError(e)
306 snapshot(getCacheEntry())
307 }
308 },
309 }),
310 }),
311 })
312 const promise = storeRef.store.dispatch(
313 extended.endpoints.injected.initiate('arg'),
314 )
315
316 await waitFor(() => {
317 expect(onError).toHaveBeenCalled()
318 })
319
320 expect(snapshot.mock.calls[0][0]).toMatchObject({
321 endpointName: 'injected',
322 isError: false,
323 isLoading: true,
324 isSuccess: false,
325 isUninitialized: false,
326 startedTimeStamp: expect.any(Number),
327 status: 'pending',
328 })
329 expect(snapshot.mock.calls[1][0]).toMatchObject({
330 error: {
331 data: { value: 'error' },
332 status: 500,
333 },
334 endpointName: 'injected',
335 isError: true,
336 isLoading: false,
337 isSuccess: false,
338 isUninitialized: false,
339 startedTimeStamp: expect.any(Number),
340 status: 'rejected',
341 })
342 })
343
344 test('query: updateCachedData', async () => {
345 const trackCalls = vi.fn()
346
347 const extended = api.injectEndpoints({
348 overrideExisting: true,
349 endpoints: (build) => ({
350 injected: build.query<{ value: string }, string>({
351 query: () => '/success',
352 async onQueryStarted(
353 arg,
354 {
355 dispatch,
356 getState,
357 getCacheEntry,
358 updateCachedData,
359 queryFulfilled,
360 },
361 ) {
362 // calling `updateCachedData` when there is no data yet should not do anything
363 // but if there is a cache value it will be updated & overwritten by the next successful result
364 updateCachedData((draft) => {
365 draft.value += '.'
366 })
367
368 try {
369 const val = await queryFulfilled
370 onSuccess(getCacheEntry().data)
371 } catch (error) {
372 updateCachedData((draft) => {
373 draft.value += 'x'
374 })
375 onError(getCacheEntry().data)
376 }
377 },
378 }),
379 }),
380 })
381
382 // request 1: success
383 expect(onSuccess).not.toHaveBeenCalled()
384 storeRef.store.dispatch(extended.endpoints.injected.initiate('arg'))
385
386 await waitFor(() => {
387 expect(onSuccess).toHaveBeenCalled()
388 })
389 expect(onSuccess).toHaveBeenCalledWith({ value: 'success' })
390 onSuccess.mockClear()
391
392 // request 2: error
393 expect(onError).not.toHaveBeenCalled()
394 server.use(
395 http.get(
396 'https://example.com/success',
397 () => {
398 return HttpResponse.json({ value: 'failed' }, { status: 500 })
399 },
400 { once: true },
401 ),
402 )
403 storeRef.store.dispatch(
404 extended.endpoints.injected.initiate('arg', { forceRefetch: true }),
405 )
406
407 await waitFor(() => {
408 expect(onError).toHaveBeenCalled()
409 })
410 expect(onError).toHaveBeenCalledWith({ value: 'success.x' })
411
412 // request 3: success
413 expect(onSuccess).not.toHaveBeenCalled()
414
415 storeRef.store.dispatch(
416 extended.endpoints.injected.initiate('arg', { forceRefetch: true }),
417 )
418
419 await waitFor(() => {
420 expect(onSuccess).toHaveBeenCalled()
421 })
422 expect(onSuccess).toHaveBeenCalledWith({ value: 'success' })
423 onSuccess.mockClear()
424 })
425
426 test('query: will only start lifecycle if query is not skipped due to `condition`', async () => {
427 const extended = api.injectEndpoints({
428 overrideExisting: true,
429 endpoints: (build) => ({
430 injected: build.query<unknown, string>({
431 query: () => '/success',
432 onQueryStarted(arg) {
433 onStart(arg)
434 },
435 }),
436 }),
437 })
438 const promise = storeRef.store.dispatch(
439 extended.endpoints.injected.initiate('arg'),
440 )
441 expect(onStart).toHaveBeenCalledOnce()
442 storeRef.store.dispatch(extended.endpoints.injected.initiate('arg'))
443 expect(onStart).toHaveBeenCalledOnce()
444 await promise
445 storeRef.store.dispatch(
446 extended.endpoints.injected.initiate('arg', { forceRefetch: true }),
447 )
448 expect(onStart).toHaveBeenCalledTimes(2)
449 })