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

transition.test.tsx

158 lines4.8 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, useQuery, 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('keeps the committed observer result when a render for another key suspends', async () => {
23 const key = queryKey()
24 queryClient.setQueryData([key, 1], { value: 1 })
25 queryClient.setQueryData([key, 2], { value: 2 })
26 const pending = new Promise<void>(() => {})
27 const renderB = vi.fn()
28 const select = (data: { value: number }) => ({ selected: data.value })
29​
30 function Page() {
31 const [id, setId] = React.useState(1)
32 const { data } = useQuery({
33 queryKey: [key, id],
34 queryFn: () => Promise.resolve({ value: id }),
35 staleTime: Infinity,
36 select,
37 })
38 if (id === 2) {
39 renderB(data)
40 throw pending
41 }
42 return (
43 <>
44 <button onClick={() => startTransition(() => setId(2))}>
45 switch
46 </button>
47 <div>data: {data?.selected}</div>
48 </>
49 )
50 }
51​
52 const rendered = renderWithClient(
53 queryClient,
54 <Suspense fallback="loading">
55 <Page />
56 </Suspense>,
57 )
58 const queryA = queryClient.getQueryCache().find({ queryKey: [key, 1] })!
59 const observer = queryA.observers[0]!
60 const committed = observer.getCurrentResult()
61 await act(() =>
62 fireEvent.click(rendered.getByRole('button', { name: 'switch' })),
63 )
64 expect(renderB).toHaveBeenCalledWith({ selected: 2 })
65 expect(observer.getCurrentResult()).toBe(committed)
66 expect(observer.getCurrentQuery()).toBe(queryA)
67 rendered.getByText('data: 1')
68​
69 await act(async () => {
70 queryClient.setQueryData([key, 1], { value: 3 })
71 await vi.advanceTimersByTimeAsync(0)
72 })
73 rendered.getByText('data: 3')
74 expect(observer.getCurrentResult().data).toEqual({ selected: 3 })
75 expect(
76 queryClient.getQueryCache().find({ queryKey: [key, 2] })!.observers,
77 ).toEqual([])
78 })
79​
80 it('should keep values of old key around with useDeferredValue', async () => {
81 const key = queryKey()
82​
83 function Page() {
84 const [count, setCount] = React.useState(0)
85 const deferredCount = useDeferredValue(count)
86 const query = useSuspenseQuery({
87 queryKey: [key, deferredCount],
88 queryFn: () => sleep(10).then(() => 'test' + deferredCount),
89 })
90​
91 return (
92 <div>
93 <button onClick={() => setCount((c) => c + 1)}>increment</button>
94 <div>data: {query.data}</div>
95 </div>
96 )
97 }
98​
99 const rendered = await renderWithClient(
100 queryClient,
101 <Suspense fallback="loading">
102 <Page />
103 </Suspense>,
104 )
105​
106 expect(rendered.getByText('loading')).toBeInTheDocument()
107 await act(() => vi.advanceTimersByTimeAsync(10))
108 expect(rendered.getByText('data: test0')).toBeInTheDocument()
109​
110 await act(() =>
111 fireEvent.click(rendered.getByRole('button', { name: 'increment' })),
112 )
113 expect(rendered.getByText('data: test0')).toBeVisible()
114 expect(rendered.queryByText('loading')).not.toBeInTheDocument()
115 await act(() => vi.advanceTimersByTimeAsync(10))
116 expect(rendered.getByText('data: test1')).toBeInTheDocument()
117 })
118​
119 it('should keep values of old key around with startTransition', async () => {
120 const key = queryKey()
121​
122 function Page() {
123 const [count, setCount] = React.useState(0)
124 const query = useSuspenseQuery({
125 queryKey: [key, count],
126 queryFn: () => sleep(10).then(() => 'test' + count),
127 })
128​
129 return (
130 <div>
131 <button onClick={() => startTransition(() => setCount((c) => c + 1))}>
132 increment
133 </button>
134 <div>data: {query.data}</div>
135 </div>
136 )
137 }
138​
139 const rendered = await renderWithClient(
140 queryClient,
141 <Suspense fallback="loading">
142 <Page />
143 </Suspense>,
144 )
145​
146 expect(rendered.getByText('loading')).toBeInTheDocument()
147 await act(() => vi.advanceTimersByTimeAsync(10))
148 expect(rendered.getByText('data: test0')).toBeInTheDocument()
149​
150 await act(() =>
151 fireEvent.click(rendered.getByRole('button', { name: 'increment' })),
152 )
153 expect(rendered.getByText('data: test0')).toBeVisible()
154 expect(rendered.queryByText('loading')).not.toBeInTheDocument()
155 await act(() => vi.advanceTimersByTimeAsync(10))
156 expect(rendered.getByText('data: test1')).toBeInTheDocument()
157 })
158})