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

transition.test.tsx

69 lines2.0 KB

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