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

ssr.test.tsx

157 lines4.0 KB

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