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 let queryCache: QueryCache
9 let queryClient: QueryClient
10​
11 beforeEach(() => {
12 vi.useFakeTimers()
13 queryCache = new QueryCache()
14 queryClient = new QueryClient({ queryCache })
15 })
16​
17 afterEach(() => {
18 vi.useRealTimers()
19 queryClient.clear()
20 })
21​
22 it('should render the content when the transition is done', async () => {
23 const key = queryKey()
24​
25 function Suspended() {
26 const state = useQuery(() => ({
27 queryKey: key,
28 queryFn: () => sleep(10).then(() => true),
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})