|
1 | 1 | import React from 'react' |
2 | 2 | import { render, waitFor } from '@testing-library/react' |
| 3 | +import { renderToString } from 'react-dom/server' |
3 | 4 |
|
4 | 5 | import { sleep, queryKey } from './utils' |
5 | | -import { QueryClient, QueryClientProvider, QueryCache, useQuery } from '../..' |
| 6 | +import { |
| 7 | + QueryClient, |
| 8 | + QueryClientProvider, |
| 9 | + QueryCache, |
| 10 | + useQuery, |
| 11 | + useQueryClient, |
| 12 | +} from '../..' |
6 | 13 |
|
7 | 14 | describe('QueryClientProvider', () => { |
8 | 15 | test('sets a specific cache for all queries to use', async () => { |
@@ -127,4 +134,77 @@ describe('QueryClientProvider', () => { |
127 | 134 | expect(queryCache.find(key)).toBeDefined() |
128 | 135 | expect(queryCache.find(key)?.options.cacheTime).toBe(Infinity) |
129 | 136 | }) |
| 137 | + |
| 138 | + describe('useQueryClient', () => { |
| 139 | + test('should throw an error if no query client has been set', () => { |
| 140 | + const consoleMock = jest |
| 141 | + .spyOn(console, 'error') |
| 142 | + .mockImplementation(() => undefined) |
| 143 | + |
| 144 | + function Page() { |
| 145 | + useQueryClient() |
| 146 | + return null |
| 147 | + } |
| 148 | + |
| 149 | + expect(() => render(<Page />)).toThrow( |
| 150 | + 'No QueryClient set, use QueryClientProvider to set one' |
| 151 | + ) |
| 152 | + |
| 153 | + consoleMock.mockRestore() |
| 154 | + }) |
| 155 | + |
| 156 | + test('should use window to get the context when contextSharing is true', () => { |
| 157 | + const queryCache = new QueryCache() |
| 158 | + const queryClient = new QueryClient({ queryCache }) |
| 159 | + |
| 160 | + let queryClientFromHook: QueryClient | undefined |
| 161 | + let queryClientFromWindow: QueryClient | undefined |
| 162 | + |
| 163 | + function Page() { |
| 164 | + queryClientFromHook = useQueryClient() |
| 165 | + queryClientFromWindow = React.useContext( |
| 166 | + window.ReactQueryClientContext as React.Context< |
| 167 | + QueryClient | undefined |
| 168 | + > |
| 169 | + ) |
| 170 | + return null |
| 171 | + } |
| 172 | + |
| 173 | + render( |
| 174 | + <QueryClientProvider client={queryClient} contextSharing={true}> |
| 175 | + <Page /> |
| 176 | + </QueryClientProvider> |
| 177 | + ) |
| 178 | + |
| 179 | + expect(queryClientFromHook).toEqual(queryClient) |
| 180 | + expect(queryClientFromWindow).toEqual(queryClient) |
| 181 | + }) |
| 182 | + |
| 183 | + test('should not use window to get the context when contextSharing is true and window does not exist', () => { |
| 184 | + const queryCache = new QueryCache() |
| 185 | + const queryClient = new QueryClient({ queryCache }) |
| 186 | + |
| 187 | + // Mock a non web browser environment |
| 188 | + const windowSpy = jest |
| 189 | + .spyOn(window, 'window', 'get') |
| 190 | + .mockImplementation(undefined) |
| 191 | + |
| 192 | + let queryClientFromHook: QueryClient | undefined |
| 193 | + |
| 194 | + function Page() { |
| 195 | + queryClientFromHook = useQueryClient() |
| 196 | + return null |
| 197 | + } |
| 198 | + |
| 199 | + renderToString( |
| 200 | + <QueryClientProvider client={queryClient} contextSharing={true}> |
| 201 | + <Page /> |
| 202 | + </QueryClientProvider> |
| 203 | + ) |
| 204 | + |
| 205 | + expect(queryClientFromHook).toEqual(queryClient) |
| 206 | + |
| 207 | + windowSpy.mockRestore() |
| 208 | + }) |
| 209 | + }) |
130 | 210 | }) |
0 commit comments