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

transition.test.tsx

66 lines1.9 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent } from '@solidjs/testing-library'
3import { Show, Suspense, createSignal, startTransition } from 'solid-js'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import { QueryCache, QueryClient, useQuery } from '..'
6import { renderWithClient } from './utils'
7​
8describe("useQuery's in Suspense mode with transitions", () => {
9 let queryCache: QueryCache
10 let queryClient: QueryClient
11​
12 beforeEach(() => {
13 vi.useFakeTimers()
14 queryCache = new QueryCache()
15 queryClient = new QueryClient({ queryCache })
16 })
17​
18 afterEach(() => {
19 queryClient.clear()
20 vi.useRealTimers()
21 })
22​
23 it('should render the content when the transition is done', async () => {
24 const key = queryKey()
25​
26 function Suspended() {
27 const state = useQuery(() => ({
28 queryKey: key,
29 queryFn: () => sleep(10).then(() => true),
30 }))
31 return <Show when={state.data}>Message</Show>
32 }
33​
34 function Page() {
35 const [showSignal, setShowSignal] = createSignal(false)
36​
37 return (
38 <div>
39 <button
40 aria-label="toggle"
41 onClick={() =>
42 startTransition(() => setShowSignal((value) => !value))
43 }
44 >
45 {showSignal() ? 'Hide' : 'Show'}
46 </button>
47 <Suspense fallback="Loading">
48 <Show when={showSignal()}>
49 <Suspended />
50 </Show>
51 </Suspense>
52 </div>
53 )
54 }
55​
56 const rendered = renderWithClient(queryClient, () => <Page />)
57​
58 expect(rendered.getByText('Show')).toBeInTheDocument()
59​
60 fireEvent.click(rendered.getByLabelText('toggle'))
61 await vi.advanceTimersByTimeAsync(10)
62 expect(rendered.getByText('Message')).toBeInTheDocument()
63 // verify that the button also updated. See https://github.com/solidjs/solid/issues/1249
64 expect(rendered.getByText('Hide')).toBeInTheDocument()
65 })
66})