Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import * as React from 'react'
3import { act, fireEvent } from '@testing-library/react'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import { Suspense, startTransition, useDeferredValue } from 'react'
6import { QueryClient, useSuspenseQuery } from '..'
7import { renderWithClient } from './utils'
8β
9describe('react transitions', () => {
10 let queryClient: QueryClient
11β
12 beforeEach(() => {
13 vi.useFakeTimers()
14 queryClient = new QueryClient()
15 })
16β
17 afterEach(() => {
18 queryClient.clear()
19 vi.useRealTimers()
20 })
21β
22 it('should keep values of old key around with useDeferredValue', async () => {
23 const key = queryKey()
24β
25 function Page() {
26 const [count, setCount] = React.useState(0)
27 const deferredCount = useDeferredValue(count)
28 const query = useSuspenseQuery({
29 queryKey: [key, deferredCount],
30 queryFn: () => sleep(10).then(() => 'test' + deferredCount),
31 })
32β
33 return (
34 <div>
35 <button onClick={() => setCount((c) => c + 1)}>increment</button>
36 <div>data: {query.data}</div>
37 </div>
38 )
39 }
40β
41 const rendered = await renderWithClient(
42 queryClient,
43 <Suspense fallback="loading">
44 <Page />
45 </Suspense>,
46 )
47β
48 expect(rendered.getByText('loading')).toBeInTheDocument()
49β
50 await act(() => vi.advanceTimersByTimeAsync(10))
51β
52 expect(rendered.getByText('data: test0')).toBeInTheDocument()
53β
54 await act(() =>
55 fireEvent.click(rendered.getByRole('button', { name: 'increment' })),
56 )
57β
58 expect(rendered.getByText('data: test0')).toBeVisible()
59 expect(rendered.queryByText('loading')).not.toBeInTheDocument()
60β
61 await act(() => vi.advanceTimersByTimeAsync(10))
62β
63 expect(rendered.getByText('data: test1')).toBeInTheDocument()
64 })
65β
66 it('should keep values of old key around with startTransition', async () => {
67 const key = queryKey()
68β
69 function Page() {
70 const [count, setCount] = React.useState(0)
71 const query = useSuspenseQuery({
72 queryKey: [key, count],
73 queryFn: () => sleep(10).then(() => 'test' + count),
74 })
75β
76 return (
77 <div>
78 <button onClick={() => startTransition(() => setCount((c) => c + 1))}>
79 increment
80 </button>
81 <div>data: {query.data}</div>
82 </div>
83 )
84 }
85β
86 const rendered = await renderWithClient(
87 queryClient,
88 <Suspense fallback="loading">
89 <Page />
90 </Suspense>,
91 )
92β
93 expect(rendered.getByText('loading')).toBeInTheDocument()
94β
95 await act(() => vi.advanceTimersByTimeAsync(10))
96β
97 expect(rendered.getByText('data: test0')).toBeInTheDocument()
98β
99 await act(() =>
100 fireEvent.click(rendered.getByRole('button', { name: 'increment' })),
101 )
102β
103 expect(rendered.getByText('data: test0')).toBeVisible()
104 expect(rendered.queryByText('loading')).not.toBeInTheDocument()
105β
106 await act(() => vi.advanceTimersByTimeAsync(10))
107β
108 expect(rendered.getByText('data: test1')).toBeInTheDocument()
109 })
110})