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

useSuspenseInfiniteQuery.test.tsx

117 lines2.8 KB

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 queryFn: Math.random() >= 0 ? skipToken : () => Promise.resolve(5),
29 })
30​
31 return null
32 }
33​
34 function App() {
35 return (
36 <React.Suspense fallback="Loading...">
37 <Page />
38 </React.Suspense>
39 )
40 }
41​
42 renderWithClient(queryClient, <App />)
43​
44 expect(consoleErrorSpy).toHaveBeenCalledWith(
45 'skipToken is not allowed for useSuspenseInfiniteQuery',
46 )
47 consoleErrorSpy.mockRestore()
48 })
49​
50 it('should log an error when skipToken is used in development environment', () => {
51 const envCopy = process.env.NODE_ENV
52 process.env.NODE_ENV = 'development'
53​
54 const consoleErrorSpy = vi
55 .spyOn(console, 'error')
56 .mockImplementation(() => undefined)
57 const key = queryKey()
58​
59 function Page() {
60 useSuspenseInfiniteQuery({
61 queryKey: key,
62 queryFn: skipToken as any,
63 initialPageParam: 1,
64 getNextPageParam: () => 1,
65 })
66​
67 return null
68 }
69​
70 renderWithClient(
71 queryClient,
72 <React.Suspense fallback="Loading...">
73 <Page />
74 </React.Suspense>,
75 )
76​
77 expect(consoleErrorSpy).toHaveBeenCalledWith(
78 'skipToken is not allowed for useSuspenseInfiniteQuery',
79 )
80​
81 consoleErrorSpy.mockRestore()
82 process.env.NODE_ENV = envCopy
83 })
84​
85 it('should not log an error when skipToken is used in production environment', () => {
86 const envCopy = process.env.NODE_ENV
87 process.env.NODE_ENV = 'production'
88​
89 const consoleErrorSpy = vi
90 .spyOn(console, 'error')
91 .mockImplementation(() => undefined)
92 const key = queryKey()
93​
94 function Page() {
95 useSuspenseInfiniteQuery({
96 queryKey: key,
97 queryFn: skipToken as any,
98 initialPageParam: 1,
99 getNextPageParam: () => 1,
100 })
101​
102 return null
103 }
104​
105 renderWithClient(
106 queryClient,
107 <React.Suspense fallback="Loading...">
108 <Page />
109 </React.Suspense>,
110 )
111​
112 expect(consoleErrorSpy).not.toHaveBeenCalled()
113​
114 consoleErrorSpy.mockRestore()
115 process.env.NODE_ENV = envCopy
116 })
117})