Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import * as React from 'react'
3import { queryKey } from '@tanstack/query-test-utils'
4import {
5 QueryCache,
6 QueryClient,
7 skipToken,
8 useSuspenseInfiniteQuery,
9} from '..'
10import { renderWithClient } from './utils'
11β
12describe('useSuspenseInfiniteQuery', () => {
13 const queryCache = new QueryCache()
14 const queryClient = new QueryClient({ queryCache })
15β
16 it('should log an error when skipToken is passed as queryFn', () => {
17 const consoleErrorSpy = vi
18 .spyOn(console, 'error')
19 .mockImplementation(() => {})
20 const key = queryKey()
21β
22 function Page() {
23 useSuspenseInfiniteQuery({
24 queryKey: key,
25 initialPageParam: 1,
26 getNextPageParam: () => 1,
27 // @ts-expect-error
28 // eslint-disable-next-line react-hooks/purity
29 queryFn: Math.random() >= 0 ? skipToken : () => Promise.resolve(5),
30 })
31β
32 return null
33 }
34β
35 function App() {
36 return (
37 <React.Suspense fallback="Loading...">
38 <Page />
39 </React.Suspense>
40 )
41 }
42β
43 renderWithClient(queryClient, <App />)
44β
45 expect(consoleErrorSpy).toHaveBeenCalledWith(
46 'skipToken is not allowed for useSuspenseInfiniteQuery',
47 )
48 consoleErrorSpy.mockRestore()
49 })
50β
51 it('should log an error when skipToken is used in development environment', () => {
52 const envCopy = process.env.NODE_ENV
53 process.env.NODE_ENV = 'development'
54β
55 const consoleErrorSpy = vi
56 .spyOn(console, 'error')
57 .mockImplementation(() => undefined)
58 const key = queryKey()
59β
60 function Page() {
61 useSuspenseInfiniteQuery({
62 queryKey: key,
63 queryFn: skipToken as any,
64 initialPageParam: 1,
65 getNextPageParam: () => 1,
66 })
67β
68 return null
69 }
70β
71 renderWithClient(
72 queryClient,
73 <React.Suspense fallback="Loading...">
74 <Page />
75 </React.Suspense>,
76 )
77β
78 expect(consoleErrorSpy).toHaveBeenCalledWith(
79 'skipToken is not allowed for useSuspenseInfiniteQuery',
80 )
81β
82 consoleErrorSpy.mockRestore()
83 process.env.NODE_ENV = envCopy
84 })
85β
86 it('should not log an error when skipToken is used in production environment', () => {
87 const envCopy = process.env.NODE_ENV
88 process.env.NODE_ENV = 'production'
89β
90 const consoleErrorSpy = vi
91 .spyOn(console, 'error')
92 .mockImplementation(() => undefined)
93 const key = queryKey()
94β
95 function Page() {
96 useSuspenseInfiniteQuery({
97 queryKey: key,
98 queryFn: skipToken as any,
99 initialPageParam: 1,
100 getNextPageParam: () => 1,
101 })
102β
103 return null
104 }
105β
106 renderWithClient(
107 queryClient,
108 <React.Suspense fallback="Loading...">
109 <Page />
110 </React.Suspense>,
111 )
112β
113 expect(consoleErrorSpy).not.toHaveBeenCalled()
114β
115 consoleErrorSpy.mockRestore()
116 process.env.NODE_ENV = envCopy
117 })
118})