Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useSuspenseInfiniteQuery.test.tsx

127 lines2.9 KB

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})