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

useSuspenseInfiniteQuery.test.tsx

124 lines2.8 KB

1import { createKey } 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 consoleErrorMock = vi
28 .spyOn(console, 'error')
29 .mockImplementation(() => {})
30 const key = createKey()
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(consoleErrorMock).toHaveBeenCalledWith(
55 'skipToken is not allowed for useSuspenseInfiniteQuery',
56 )
57 })
58​
59 it('should log an error when skipToken is used in development environment', () => {
60 const envCopy = process.env.NODE_ENV
61 process.env.NODE_ENV = 'development'
62​
63 const consoleErrorMock = vi
64 .spyOn(console, 'error')
65 .mockReturnValue(undefined)
66 const key = createKey()
67​
68 function Page() {
69 useSuspenseInfiniteQuery({
70 queryKey: key,
71 queryFn: skipToken as any,
72 initialPageParam: 1,
73 getNextPageParam: () => 1,
74 })
75​
76 return null
77 }
78​
79 renderWithClient(
80 queryClient,
81 <Suspense fallback="Loading...">
82 <Page />
83 </Suspense>,
84 )
85​
86 expect(consoleErrorMock).toHaveBeenCalledWith(
87 'skipToken is not allowed for useSuspenseInfiniteQuery',
88 )
89​
90 process.env.NODE_ENV = envCopy
91 })
92​
93 it('should not log an error when skipToken is used in production environment', () => {
94 const envCopy = process.env.NODE_ENV
95 process.env.NODE_ENV = 'production'
96​
97 const consoleErrorMock = vi
98 .spyOn(console, 'error')
99 .mockReturnValue(undefined)
100 const key = createKey()
101​
102 function Page() {
103 useSuspenseInfiniteQuery({
104 queryKey: key,
105 queryFn: skipToken as any,
106 initialPageParam: 1,
107 getNextPageParam: () => 1,
108 })
109​
110 return null
111 }
112​
113 renderWithClient(
114 queryClient,
115 <Suspense fallback="Loading...">
116 <Page />
117 </Suspense>,
118 )
119​
120 expect(consoleErrorMock).not.toHaveBeenCalled()
121​
122 process.env.NODE_ENV = envCopy
123 })
124})