Skip to content

Commit

Permalink
chore: test for 6486
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo committed Dec 29, 2023
1 parent 0b33cd8 commit a2f75bf
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/react-query/src/__tests__/suspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1033,4 +1033,61 @@ describe('useSuspenseQueries', () => {

await waitFor(() => rendered.getByText('data1'))
})

it('should not request old data inside transitions (issue #6486)', async () => {
const key = queryKey()
let queryFnCount = 0

function App() {
const [count, setCount] = React.useState(0)

return (
<div>
<button
onClick={() => React.startTransition(() => setCount(count + 1))}
>
inc
</button>
<React.Suspense fallback={'Loading...'}>
<Page count={count} />
</React.Suspense>
</div>
)
}

function Page({ count }: { count: number }) {
const { data } = useSuspenseQuery({
queryKey: [key, count],
queryFn: async () => {
queryFnCount++
await sleep(10)
return 'data' + count
},
})

return (
<div>
<div>{String(data)}</div>
</div>
)
}

const rendered = renderWithClient(
queryClient,

<App />,
)

await waitFor(() => rendered.getByText('Loading...'))

await waitFor(() => rendered.getByText('data0'))

fireEvent.click(rendered.getByText('inc'))

await waitFor(() => rendered.getByText('data1'))

await sleep(20)

expect(queryFnCount).toBe(2)
})
})

0 comments on commit a2f75bf

Please sign in to comment.