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