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

ssr.test.tsx

167 lines3.9 KB

1import * as React from 'react'
2import { renderToString } from 'react-dom/server'
3import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
4import { queryKey } from '@tanstack/query-test-utils'
5import {
6 QueryCache,
7 QueryClient,
8 QueryClientProvider,
9 useInfiniteQuery,
10 useQuery,
11} from '..'
12import { setIsServer } from './utils'
13​
14describe('Server Side Rendering', () => {
15 setIsServer(true)
16​
17 let queryCache: QueryCache
18 let queryClient: QueryClient
19​
20 beforeEach(() => {
21 vi.useFakeTimers()
22 queryCache = new QueryCache()
23 queryClient = new QueryClient({ queryCache })
24 })
25​
26 afterEach(() => {
27 vi.useRealTimers()
28 })
29​
30 it('should not trigger fetch', () => {
31 const key = queryKey()
32 const queryFn = vi.fn().mockReturnValue('data')
33​
34 function Page() {
35 const query = useQuery({ queryKey: key, queryFn })
36​
37 const content = `status ${query.status}`
38​
39 return (
40 <div>
41 <div>{content}</div>
42 </div>
43 )
44 }
45​
46 const markup = renderToString(
47 <QueryClientProvider client={queryClient}>
48 <Page />
49 </QueryClientProvider>,
50 )
51​
52 expect(markup).toContain('status pending')
53 expect(queryFn).toHaveBeenCalledTimes(0)
54 queryCache.clear()
55 })
56​
57 it('should add prefetched data to cache', async () => {
58 const key = queryKey()
59 const fetchFn = () => Promise.resolve('data')
60 const data = await queryClient.fetchQuery({
61 queryKey: key,
62 queryFn: fetchFn,
63 })
64 expect(data).toBe('data')
65 expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')
66 queryCache.clear()
67 })
68​
69 it('should return existing data from the cache', async () => {
70 const key = queryKey()
71 const queryFn = vi.fn(async () => {
72 await vi.advanceTimersByTimeAsync(10)
73 return 'data'
74 })
75​
76 function Page() {
77 const query = useQuery({ queryKey: key, queryFn })
78​
79 const content = `status ${query.status}`
80​
81 return (
82 <div>
83 <div>{content}</div>
84 </div>
85 )
86 }
87​
88 await queryClient.prefetchQuery({ queryKey: key, queryFn })
89​
90 const markup = renderToString(
91 <QueryClientProvider client={queryClient}>
92 <Page />
93 </QueryClientProvider>,
94 )
95​
96 expect(markup).toContain('status success')
97 expect(queryFn).toHaveBeenCalledTimes(1)
98 queryCache.clear()
99 })
100​
101 it('should add initialData to the cache', () => {
102 const key = queryKey()
103​
104 function Page() {
105 const [page, setPage] = React.useState(1)
106 const { data } = useQuery({
107 queryKey: [key, page],
108 queryFn: () => Promise.resolve(page),
109 initialData: 1,
110 })
111​
112 return (
113 <div>
114 <h1 data-testid="title">{data}</h1>
115 <button onClick={() => setPage(page + 1)}>next</button>
116 </div>
117 )
118 }
119​
120 renderToString(
121 <QueryClientProvider client={queryClient}>
122 <Page />
123 </QueryClientProvider>,
124 )
125​
126 const keys = queryCache.getAll().map((query) => query.queryKey)
127​
128 expect(keys).toEqual([[key, 1]])
129 queryCache.clear()
130 })
131​
132 it('useInfiniteQuery should return the correct state', async () => {
133 const key = queryKey()
134 const queryFn = vi.fn(async () => {
135 await vi.advanceTimersByTimeAsync(5)
136 return 'page 1'
137 })
138​
139 function Page() {
140 const query = useInfiniteQuery({
141 queryKey: key,
142 queryFn,
143 getNextPageParam: () => undefined,
144 initialPageParam: 0,
145 })
146 return (
147 <ul>{query.data?.pages.map((page) => <li key={page}>{page}</li>)}</ul>
148 )
149 }
150​
151 await queryClient.prefetchInfiniteQuery({
152 queryKey: key,
153 queryFn,
154 initialPageParam: 0,
155 })
156​
157 const markup = renderToString(
158 <QueryClientProvider client={queryClient}>
159 <Page />
160 </QueryClientProvider>,
161 )
162​
163 expect(markup).toContain('page 1')
164 expect(queryFn).toHaveBeenCalledTimes(1)
165 queryCache.clear()
166 })
167})