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