Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent } from '@solidjs/testing-library'
3import { Loading, Show, createSignal } 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 Loading 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={() => setShowSignal((value) => !value)}
42 >
43 {showSignal() ? 'Hide' : 'Show'}
44 </button>
45 <Loading fallback="Loading">
46 <Show when={showSignal()}>
47 <Suspended />
48 </Show>
49 </Loading>
50 </div>
51 )
52 }
53β
54 const rendered = renderWithClient(queryClient, () => <Page />)
55β
56 expect(rendered.getByText('Show')).toBeInTheDocument()
57β
58 fireEvent.click(rendered.getByLabelText('toggle'))
59 await vi.advanceTimersByTimeAsync(10)
60 expect(rendered.getByText('Message')).toBeInTheDocument()
61 // verify that the button also updated. See https://github.com/solidjs/solid/issues/1249
62 expect(rendered.getByText('Hide')).toBeInTheDocument()
63 })
64})