Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent } from '@solidjs/testing-library'
3import { Show, Suspense, createSignal, startTransition } from 'solid-js'
4import { createKey, 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 = createKey()
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})