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

transition.test.tsx

110 lines3.0 KB

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})