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

useSuspenseInfiniteQuery.test.tsx

123 lines2.8 KB

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