@tanstack/react-query 5.59.14 → 5.59.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +1 -1
- package/build/legacy/infiniteQueryOptions.js.map +1 -1
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +1 -1
- package/build/legacy/queryOptions.d.ts +1 -1
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/useBaseQuery.cjs +1 -3
- package/build/legacy/useBaseQuery.cjs.map +1 -1
- package/build/legacy/useBaseQuery.js +1 -3
- package/build/legacy/useBaseQuery.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +1 -1
- package/build/modern/infiniteQueryOptions.js.map +1 -1
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +1 -1
- package/build/modern/queryOptions.d.ts +1 -1
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/useBaseQuery.cjs +1 -3
- package/build/modern/useBaseQuery.cjs.map +1 -1
- package/build/modern/useBaseQuery.js +1 -3
- package/build/modern/useBaseQuery.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/useQuery.promise.test.tsx +60 -0
- package/src/__tests__/{suspense.test-d.tsx → useSuspenseInfiniteQuery.test-d.tsx} +0 -69
- package/src/__tests__/useSuspenseQueries.test.tsx +478 -2
- package/src/__tests__/useSuspenseQuery.test-d.tsx +71 -0
- package/src/__tests__/{suspense.test.tsx → useSuspenseQuery.test.tsx} +2 -479
- package/src/infiniteQueryOptions.ts +1 -1
- package/src/queryOptions.ts +1 -1
- package/src/useBaseQuery.ts +2 -4
|
@@ -7,9 +7,10 @@ import {
|
|
|
7
7
|
it,
|
|
8
8
|
vi,
|
|
9
9
|
} from 'vitest'
|
|
10
|
-
import { act, render } from '@testing-library/react'
|
|
10
|
+
import { act, fireEvent, render, waitFor } from '@testing-library/react'
|
|
11
11
|
import * as React from 'react'
|
|
12
|
-
import {
|
|
12
|
+
import { ErrorBoundary } from 'react-error-boundary'
|
|
13
|
+
import { useSuspenseQueries, useSuspenseQuery } from '..'
|
|
13
14
|
import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
|
|
14
15
|
import type { UseSuspenseQueryOptions } from '..'
|
|
15
16
|
|
|
@@ -183,3 +184,478 @@ describe('useSuspenseQueries', () => {
|
|
|
183
184
|
expect(spy).toHaveBeenCalled()
|
|
184
185
|
})
|
|
185
186
|
})
|
|
187
|
+
|
|
188
|
+
describe('useSuspenseQueries 2', () => {
|
|
189
|
+
it('should suspend all queries in parallel', async () => {
|
|
190
|
+
const key1 = queryKey()
|
|
191
|
+
const key2 = queryKey()
|
|
192
|
+
const results: Array<string> = []
|
|
193
|
+
|
|
194
|
+
function Fallback() {
|
|
195
|
+
results.push('loading')
|
|
196
|
+
return <div>loading</div>
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function Page() {
|
|
200
|
+
const result = useSuspenseQueries({
|
|
201
|
+
queries: [
|
|
202
|
+
{
|
|
203
|
+
queryKey: key1,
|
|
204
|
+
queryFn: async () => {
|
|
205
|
+
results.push('1')
|
|
206
|
+
await sleep(10)
|
|
207
|
+
return '1'
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
queryKey: key2,
|
|
212
|
+
queryFn: async () => {
|
|
213
|
+
results.push('2')
|
|
214
|
+
await sleep(20)
|
|
215
|
+
return '2'
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
})
|
|
220
|
+
return (
|
|
221
|
+
<div>
|
|
222
|
+
<h1>data: {result.map((item) => item.data ?? 'null').join(',')}</h1>
|
|
223
|
+
</div>
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const rendered = renderWithClient(
|
|
228
|
+
queryClient,
|
|
229
|
+
<React.Suspense fallback={<Fallback />}>
|
|
230
|
+
<Page />
|
|
231
|
+
</React.Suspense>,
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
await waitFor(() => rendered.getByText('loading'))
|
|
235
|
+
await waitFor(() => rendered.getByText('data: 1,2'))
|
|
236
|
+
|
|
237
|
+
expect(results).toEqual(['1', '2', 'loading'])
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
it("shouldn't unmount before all promises fetched", async () => {
|
|
241
|
+
const key1 = queryKey()
|
|
242
|
+
const key2 = queryKey()
|
|
243
|
+
const results: Array<string> = []
|
|
244
|
+
const refs: Array<number> = []
|
|
245
|
+
|
|
246
|
+
function Fallback() {
|
|
247
|
+
results.push('loading')
|
|
248
|
+
return <div>loading</div>
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function Page() {
|
|
252
|
+
const ref = React.useRef(Math.random())
|
|
253
|
+
const result = useSuspenseQueries({
|
|
254
|
+
queries: [
|
|
255
|
+
{
|
|
256
|
+
queryKey: key1,
|
|
257
|
+
queryFn: async () => {
|
|
258
|
+
refs.push(ref.current)
|
|
259
|
+
results.push('1')
|
|
260
|
+
await sleep(10)
|
|
261
|
+
return '1'
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
queryKey: key2,
|
|
266
|
+
queryFn: async () => {
|
|
267
|
+
refs.push(ref.current)
|
|
268
|
+
results.push('2')
|
|
269
|
+
await sleep(20)
|
|
270
|
+
return '2'
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
],
|
|
274
|
+
})
|
|
275
|
+
return (
|
|
276
|
+
<div>
|
|
277
|
+
<h1>data: {result.map((item) => item.data ?? 'null').join(',')}</h1>
|
|
278
|
+
</div>
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const rendered = renderWithClient(
|
|
283
|
+
queryClient,
|
|
284
|
+
<React.Suspense fallback={<Fallback />}>
|
|
285
|
+
<Page />
|
|
286
|
+
</React.Suspense>,
|
|
287
|
+
)
|
|
288
|
+
await waitFor(() => rendered.getByText('loading'))
|
|
289
|
+
expect(refs.length).toBe(2)
|
|
290
|
+
await waitFor(() => rendered.getByText('data: 1,2'))
|
|
291
|
+
expect(refs[0]).toBe(refs[1])
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
// this addresses the following issue:
|
|
295
|
+
// https://github.com/TanStack/query/issues/6344
|
|
296
|
+
it('should suspend on offline when query changes, and data should not be undefined', async () => {
|
|
297
|
+
function Page() {
|
|
298
|
+
const [id, setId] = React.useState(0)
|
|
299
|
+
|
|
300
|
+
const { data } = useSuspenseQuery({
|
|
301
|
+
queryKey: [id],
|
|
302
|
+
queryFn: () => Promise.resolve(`Data ${id}`),
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
// defensive guard here
|
|
306
|
+
if (data === undefined) {
|
|
307
|
+
throw new Error('data cannot be undefined')
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return (
|
|
311
|
+
<>
|
|
312
|
+
<div>{data}</div>
|
|
313
|
+
<button onClick={() => setId(id + 1)}>fetch</button>
|
|
314
|
+
</>
|
|
315
|
+
)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const rendered = renderWithClient(
|
|
319
|
+
queryClient,
|
|
320
|
+
<React.Suspense fallback={<div>loading</div>}>
|
|
321
|
+
<Page />
|
|
322
|
+
</React.Suspense>,
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
await waitFor(() => rendered.getByText('loading'))
|
|
326
|
+
await waitFor(() => rendered.getByText('Data 0'))
|
|
327
|
+
|
|
328
|
+
// go offline
|
|
329
|
+
document.dispatchEvent(new CustomEvent('offline'))
|
|
330
|
+
|
|
331
|
+
fireEvent.click(rendered.getByText('fetch'))
|
|
332
|
+
await waitFor(() => rendered.getByText('Data 0'))
|
|
333
|
+
|
|
334
|
+
// go back online
|
|
335
|
+
document.dispatchEvent(new CustomEvent('online'))
|
|
336
|
+
fireEvent.click(rendered.getByText('fetch'))
|
|
337
|
+
|
|
338
|
+
// query should resume
|
|
339
|
+
await waitFor(() => rendered.getByText('Data 1'))
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
it('should throw error when queryKey changes and new query fails', async () => {
|
|
343
|
+
const consoleMock = vi
|
|
344
|
+
.spyOn(console, 'error')
|
|
345
|
+
.mockImplementation(() => undefined)
|
|
346
|
+
const key = queryKey()
|
|
347
|
+
|
|
348
|
+
function Page() {
|
|
349
|
+
const [fail, setFail] = React.useState(false)
|
|
350
|
+
const { data } = useSuspenseQuery({
|
|
351
|
+
queryKey: [key, fail],
|
|
352
|
+
queryFn: async () => {
|
|
353
|
+
await sleep(10)
|
|
354
|
+
|
|
355
|
+
if (fail) {
|
|
356
|
+
throw new Error('Suspense Error Bingo')
|
|
357
|
+
} else {
|
|
358
|
+
return 'data'
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
retry: 0,
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
return (
|
|
365
|
+
<div>
|
|
366
|
+
<button onClick={() => setFail(true)}>trigger fail</button>
|
|
367
|
+
|
|
368
|
+
<div>rendered: {String(data)}</div>
|
|
369
|
+
</div>
|
|
370
|
+
)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const rendered = renderWithClient(
|
|
374
|
+
queryClient,
|
|
375
|
+
<ErrorBoundary fallbackRender={() => <div>error boundary</div>}>
|
|
376
|
+
<React.Suspense fallback={'Loading...'}>
|
|
377
|
+
<Page />
|
|
378
|
+
</React.Suspense>
|
|
379
|
+
</ErrorBoundary>,
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
await waitFor(() => rendered.getByText('Loading...'))
|
|
383
|
+
|
|
384
|
+
await waitFor(() => rendered.getByText('rendered: data'))
|
|
385
|
+
|
|
386
|
+
fireEvent.click(rendered.getByText('trigger fail'))
|
|
387
|
+
|
|
388
|
+
await waitFor(() => rendered.getByText('error boundary'))
|
|
389
|
+
|
|
390
|
+
expect(consoleMock.mock.calls[0]?.[1]).toStrictEqual(
|
|
391
|
+
new Error('Suspense Error Bingo'),
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
consoleMock.mockRestore()
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
it('should keep previous data when wrapped in a transition', async () => {
|
|
398
|
+
const key = queryKey()
|
|
399
|
+
|
|
400
|
+
function Page() {
|
|
401
|
+
const [count, setCount] = React.useState(0)
|
|
402
|
+
const [isPending, startTransition] = React.useTransition()
|
|
403
|
+
const { data } = useSuspenseQuery({
|
|
404
|
+
queryKey: [key, count],
|
|
405
|
+
queryFn: async () => {
|
|
406
|
+
await sleep(10)
|
|
407
|
+
return 'data' + count
|
|
408
|
+
},
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
return (
|
|
412
|
+
<div>
|
|
413
|
+
<button onClick={() => startTransition(() => setCount(count + 1))}>
|
|
414
|
+
inc
|
|
415
|
+
</button>
|
|
416
|
+
|
|
417
|
+
<div>{isPending ? 'Pending...' : String(data)}</div>
|
|
418
|
+
</div>
|
|
419
|
+
)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const rendered = renderWithClient(
|
|
423
|
+
queryClient,
|
|
424
|
+
<React.Suspense fallback={'Loading...'}>
|
|
425
|
+
<Page />
|
|
426
|
+
</React.Suspense>,
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
await waitFor(() => rendered.getByText('Loading...'))
|
|
430
|
+
|
|
431
|
+
await waitFor(() => rendered.getByText('data0'))
|
|
432
|
+
|
|
433
|
+
fireEvent.click(rendered.getByText('inc'))
|
|
434
|
+
|
|
435
|
+
await waitFor(() => rendered.getByText('Pending...'))
|
|
436
|
+
|
|
437
|
+
await waitFor(() => rendered.getByText('data1'))
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
it('should not request old data inside transitions (issue #6486)', async () => {
|
|
441
|
+
const key = queryKey()
|
|
442
|
+
let queryFnCount = 0
|
|
443
|
+
|
|
444
|
+
function App() {
|
|
445
|
+
const [count, setCount] = React.useState(0)
|
|
446
|
+
|
|
447
|
+
return (
|
|
448
|
+
<div>
|
|
449
|
+
<button
|
|
450
|
+
onClick={() => React.startTransition(() => setCount(count + 1))}
|
|
451
|
+
>
|
|
452
|
+
inc
|
|
453
|
+
</button>
|
|
454
|
+
<React.Suspense fallback={'Loading...'}>
|
|
455
|
+
<Page count={count} />
|
|
456
|
+
</React.Suspense>
|
|
457
|
+
</div>
|
|
458
|
+
)
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function Page({ count }: { count: number }) {
|
|
462
|
+
const { data } = useSuspenseQuery({
|
|
463
|
+
queryKey: [key, count],
|
|
464
|
+
queryFn: async () => {
|
|
465
|
+
queryFnCount++
|
|
466
|
+
await sleep(10)
|
|
467
|
+
return 'data' + count
|
|
468
|
+
},
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
return (
|
|
472
|
+
<div>
|
|
473
|
+
<div>{String(data)}</div>
|
|
474
|
+
</div>
|
|
475
|
+
)
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const rendered = renderWithClient(
|
|
479
|
+
queryClient,
|
|
480
|
+
|
|
481
|
+
<App />,
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
await waitFor(() => rendered.getByText('Loading...'))
|
|
485
|
+
|
|
486
|
+
await waitFor(() => rendered.getByText('data0'))
|
|
487
|
+
|
|
488
|
+
fireEvent.click(rendered.getByText('inc'))
|
|
489
|
+
|
|
490
|
+
await waitFor(() => rendered.getByText('data1'))
|
|
491
|
+
|
|
492
|
+
await sleep(20)
|
|
493
|
+
|
|
494
|
+
expect(queryFnCount).toBe(2)
|
|
495
|
+
})
|
|
496
|
+
|
|
497
|
+
it('should still suspense if queryClient has placeholderData config', async () => {
|
|
498
|
+
const key = queryKey()
|
|
499
|
+
const queryClientWithPlaceholder = createQueryClient({
|
|
500
|
+
defaultOptions: {
|
|
501
|
+
queries: {
|
|
502
|
+
placeholderData: (previousData: any) => previousData,
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
function Page() {
|
|
508
|
+
const [count, setCount] = React.useState(0)
|
|
509
|
+
const [isPending, startTransition] = React.useTransition()
|
|
510
|
+
const { data } = useSuspenseQuery({
|
|
511
|
+
queryKey: [key, count],
|
|
512
|
+
queryFn: async () => {
|
|
513
|
+
await sleep(10)
|
|
514
|
+
return 'data' + count
|
|
515
|
+
},
|
|
516
|
+
})
|
|
517
|
+
|
|
518
|
+
return (
|
|
519
|
+
<div>
|
|
520
|
+
<button onClick={() => startTransition(() => setCount(count + 1))}>
|
|
521
|
+
inc
|
|
522
|
+
</button>
|
|
523
|
+
|
|
524
|
+
<div>{isPending ? 'Pending...' : String(data)}</div>
|
|
525
|
+
</div>
|
|
526
|
+
)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const rendered = renderWithClient(
|
|
530
|
+
queryClientWithPlaceholder,
|
|
531
|
+
<React.Suspense fallback={'Loading...'}>
|
|
532
|
+
<Page />
|
|
533
|
+
</React.Suspense>,
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
await waitFor(() => rendered.getByText('Loading...'))
|
|
537
|
+
|
|
538
|
+
await waitFor(() => rendered.getByText('data0'))
|
|
539
|
+
|
|
540
|
+
fireEvent.click(rendered.getByText('inc'))
|
|
541
|
+
|
|
542
|
+
await waitFor(() => rendered.getByText('Pending...'))
|
|
543
|
+
|
|
544
|
+
await waitFor(() => rendered.getByText('data1'))
|
|
545
|
+
})
|
|
546
|
+
|
|
547
|
+
it('should show error boundary even with gcTime:0 (#7853)', async () => {
|
|
548
|
+
const consoleMock = vi
|
|
549
|
+
.spyOn(console, 'error')
|
|
550
|
+
.mockImplementation(() => undefined)
|
|
551
|
+
const key = queryKey()
|
|
552
|
+
let count = 0
|
|
553
|
+
|
|
554
|
+
function Page() {
|
|
555
|
+
useSuspenseQuery({
|
|
556
|
+
queryKey: key,
|
|
557
|
+
queryFn: async () => {
|
|
558
|
+
count++
|
|
559
|
+
console.log('queryFn')
|
|
560
|
+
throw new Error('Query failed')
|
|
561
|
+
},
|
|
562
|
+
gcTime: 0,
|
|
563
|
+
retry: false,
|
|
564
|
+
})
|
|
565
|
+
|
|
566
|
+
return null
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function App() {
|
|
570
|
+
return (
|
|
571
|
+
<React.Suspense fallback="loading">
|
|
572
|
+
<ErrorBoundary
|
|
573
|
+
fallbackRender={() => {
|
|
574
|
+
return <div>There was an error!</div>
|
|
575
|
+
}}
|
|
576
|
+
>
|
|
577
|
+
<Page />
|
|
578
|
+
</ErrorBoundary>
|
|
579
|
+
</React.Suspense>
|
|
580
|
+
)
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
const rendered = renderWithClient(queryClient, <App />)
|
|
584
|
+
|
|
585
|
+
await waitFor(() => rendered.getByText('There was an error!'))
|
|
586
|
+
expect(count).toBe(1)
|
|
587
|
+
consoleMock.mockRestore()
|
|
588
|
+
})
|
|
589
|
+
|
|
590
|
+
describe('gc (with fake timers)', () => {
|
|
591
|
+
beforeAll(() => {
|
|
592
|
+
vi.useFakeTimers()
|
|
593
|
+
})
|
|
594
|
+
|
|
595
|
+
afterAll(() => {
|
|
596
|
+
vi.useRealTimers()
|
|
597
|
+
})
|
|
598
|
+
|
|
599
|
+
it('should gc when unmounted while fetching with low gcTime (#8159)', async () => {
|
|
600
|
+
const key = queryKey()
|
|
601
|
+
|
|
602
|
+
function Page() {
|
|
603
|
+
return (
|
|
604
|
+
<React.Suspense fallback="loading">
|
|
605
|
+
<Component />
|
|
606
|
+
</React.Suspense>
|
|
607
|
+
)
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function Component() {
|
|
611
|
+
const { data } = useSuspenseQuery({
|
|
612
|
+
queryKey: key,
|
|
613
|
+
queryFn: async () => {
|
|
614
|
+
await sleep(3000)
|
|
615
|
+
return 'data'
|
|
616
|
+
},
|
|
617
|
+
gcTime: 1000,
|
|
618
|
+
})
|
|
619
|
+
|
|
620
|
+
return <div>{data}</div>
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function Page2() {
|
|
624
|
+
return <div>page2</div>
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
function App() {
|
|
628
|
+
const [show, setShow] = React.useState(true)
|
|
629
|
+
return (
|
|
630
|
+
<div>
|
|
631
|
+
{show ? <Page /> : <Page2 />}
|
|
632
|
+
<button onClick={() => setShow(false)}>hide</button>
|
|
633
|
+
</div>
|
|
634
|
+
)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const rendered = renderWithClient(queryClient, <App />)
|
|
638
|
+
|
|
639
|
+
await act(() => vi.advanceTimersByTimeAsync(200))
|
|
640
|
+
|
|
641
|
+
rendered.getByText('loading')
|
|
642
|
+
|
|
643
|
+
// unmount while still fetching
|
|
644
|
+
fireEvent.click(rendered.getByText('hide'))
|
|
645
|
+
|
|
646
|
+
await act(() => vi.advanceTimersByTimeAsync(800))
|
|
647
|
+
|
|
648
|
+
rendered.getByText('page2')
|
|
649
|
+
|
|
650
|
+
// wait for query to be resolved
|
|
651
|
+
await act(() => vi.advanceTimersByTimeAsync(2000))
|
|
652
|
+
|
|
653
|
+
expect(queryClient.getQueryData(key)).toBe('data')
|
|
654
|
+
|
|
655
|
+
// wait for gc
|
|
656
|
+
await act(() => vi.advanceTimersByTimeAsync(1000))
|
|
657
|
+
|
|
658
|
+
expect(queryClient.getQueryData(key)).toBe(undefined)
|
|
659
|
+
})
|
|
660
|
+
})
|
|
661
|
+
})
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from 'vitest'
|
|
2
|
+
import { skipToken } from '@tanstack/query-core'
|
|
3
|
+
import { useSuspenseQuery } from '../useSuspenseQuery'
|
|
4
|
+
|
|
5
|
+
describe('useSuspenseQuery', () => {
|
|
6
|
+
it('should always have data defined', () => {
|
|
7
|
+
const { data } = useSuspenseQuery({
|
|
8
|
+
queryKey: ['key'],
|
|
9
|
+
queryFn: () => Promise.resolve(5),
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
expectTypeOf(data).toEqualTypeOf<number>()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('should not have pending status', () => {
|
|
16
|
+
const { status } = useSuspenseQuery({
|
|
17
|
+
queryKey: ['key'],
|
|
18
|
+
queryFn: () => Promise.resolve(5),
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
expectTypeOf(status).toEqualTypeOf<'error' | 'success'>()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should not allow skipToken in queryFn', () => {
|
|
25
|
+
useSuspenseQuery({
|
|
26
|
+
queryKey: ['key'],
|
|
27
|
+
// @ts-expect-error
|
|
28
|
+
queryFn: skipToken,
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
useSuspenseQuery({
|
|
32
|
+
queryKey: ['key'],
|
|
33
|
+
// @ts-expect-error
|
|
34
|
+
queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should not allow placeholderData, enabled or throwOnError props', () => {
|
|
39
|
+
useSuspenseQuery({
|
|
40
|
+
queryKey: ['key'],
|
|
41
|
+
queryFn: () => Promise.resolve(5),
|
|
42
|
+
// @ts-expect-error TS2345
|
|
43
|
+
placeholderData: 5,
|
|
44
|
+
enabled: true,
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
useSuspenseQuery({
|
|
48
|
+
queryKey: ['key'],
|
|
49
|
+
queryFn: () => Promise.resolve(5),
|
|
50
|
+
// @ts-expect-error TS2345
|
|
51
|
+
enabled: true,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
useSuspenseQuery({
|
|
55
|
+
queryKey: ['key'],
|
|
56
|
+
queryFn: () => Promise.resolve(5),
|
|
57
|
+
// @ts-expect-error TS2345
|
|
58
|
+
throwOnError: true,
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('should not return isPlaceholderData', () => {
|
|
63
|
+
const query = useSuspenseQuery({
|
|
64
|
+
queryKey: ['key'],
|
|
65
|
+
queryFn: () => Promise.resolve(5),
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
// @ts-expect-error TS2339
|
|
69
|
+
query.isPlaceholderData
|
|
70
|
+
})
|
|
71
|
+
})
|