|
1 |
| -import { sleep, queryKey, mockConsoleError } from '../../reactjs/tests/utils' |
| 1 | +import { waitFor } from '@testing-library/react' |
| 2 | +import '@testing-library/jest-dom' |
| 3 | +import React from 'react' |
| 4 | + |
| 5 | +import { |
| 6 | + sleep, |
| 7 | + queryKey, |
| 8 | + mockConsoleError, |
| 9 | + renderWithClient, |
| 10 | +} from '../../reactjs/tests/utils' |
2 | 11 | import {
|
| 12 | + useQuery, |
3 | 13 | InfiniteQueryObserver,
|
4 | 14 | QueryCache,
|
5 | 15 | QueryClient,
|
@@ -206,6 +216,62 @@ describe('queryClient', () => {
|
206 | 216 |
|
207 | 217 | expect(queryCache.find(key)!.state.data).toBe(newData)
|
208 | 218 | })
|
| 219 | + |
| 220 | + test('should not call onSuccess callback of active observers', async () => { |
| 221 | + const key = queryKey() |
| 222 | + const onSuccess = jest.fn() |
| 223 | + |
| 224 | + function Page() { |
| 225 | + const state = useQuery(key, () => 'data', { onSuccess }) |
| 226 | + return ( |
| 227 | + <div> |
| 228 | + <div>data: {state.data}</div> |
| 229 | + <button onClick={() => queryClient.setQueryData(key, 'newData')}> |
| 230 | + setQueryData |
| 231 | + </button> |
| 232 | + </div> |
| 233 | + ) |
| 234 | + } |
| 235 | + |
| 236 | + const rendered = renderWithClient(queryClient, <Page />) |
| 237 | + |
| 238 | + await waitFor(() => rendered.getByText('data: data')) |
| 239 | + rendered.getByRole('button', { name: /setQueryData/i }).click() |
| 240 | + await waitFor(() => rendered.getByText('data: newData')) |
| 241 | + |
| 242 | + expect(onSuccess).toHaveBeenCalledTimes(1) |
| 243 | + expect(onSuccess).toHaveBeenCalledWith('data') |
| 244 | + }) |
| 245 | + |
| 246 | + test('should respect updatedAt', async () => { |
| 247 | + const key = queryKey() |
| 248 | + |
| 249 | + function Page() { |
| 250 | + const state = useQuery(key, () => 'data') |
| 251 | + return ( |
| 252 | + <div> |
| 253 | + <div>data: {state.data}</div> |
| 254 | + <div>dataUpdatedAt: {state.dataUpdatedAt}</div> |
| 255 | + <button |
| 256 | + onClick={() => |
| 257 | + queryClient.setQueryData(key, 'newData', { updatedAt: 100 }) |
| 258 | + } |
| 259 | + > |
| 260 | + setQueryData |
| 261 | + </button> |
| 262 | + </div> |
| 263 | + ) |
| 264 | + } |
| 265 | + |
| 266 | + const rendered = renderWithClient(queryClient, <Page />) |
| 267 | + |
| 268 | + await waitFor(() => rendered.getByText('data: data')) |
| 269 | + rendered.getByRole('button', { name: /setQueryData/i }).click() |
| 270 | + await waitFor(() => rendered.getByText('data: newData')) |
| 271 | + await waitFor(() => { |
| 272 | + expect(rendered.getByText('dataUpdatedAt: 100')).toBeInTheDocument() |
| 273 | + }) |
| 274 | + }) |
209 | 275 | })
|
210 | 276 |
|
211 | 277 | describe('setQueriesData', () => {
|
|
0 commit comments