Open History to see the last commit
1import { describe, expect, test } from 'vitest'
2import { render, waitFor } from '@testing-library/svelte'
3import { derived, writable } from 'svelte/store'
4import { QueryClient } from '@tanstack/query-core'
5import CreateQuery from './CreateQuery.svelte'
6import { sleep } from './utils'
7β
8describe('createQuery', () => {
9 test('Render and wait for success', async () => {
10 const rendered = render(CreateQuery, {
11 props: {
12 options: {
13 queryKey: ['test'],
14 queryFn: async () => {
15 await sleep(10)
16 return 'Success'
17 },
18 },
19 queryClient: new QueryClient(),
20 },
21 })
22β
23 await waitFor(() => {
24 expect(rendered.queryByText('Loading')).toBeInTheDocument()
25 })
26β
27 await waitFor(() => {
28 expect(rendered.queryByText('Success')).toBeInTheDocument()
29 })
30 })
31β
32 test('Accept a writable store for options', async () => {
33 const optionsStore = writable({
34 queryKey: ['test'],
35 queryFn: async () => {
36 await sleep(10)
37 return 'Success'
38 },
39 })
40β
41 const rendered = render(CreateQuery, {
42 props: {
43 options: optionsStore,
44 queryClient: new QueryClient(),
45 },
46 })
47β
48 await waitFor(() => {
49 expect(rendered.queryByText('Success')).toBeInTheDocument()
50 })
51 })
52β
53 test('Accept a derived store for options', async () => {
54 const writableStore = writable('test')
55β
56 const derivedStore = derived(writableStore, ($store) => ({
57 queryKey: [$store],
58 queryFn: async () => {
59 await sleep(10)
60 return 'Success'
61 },
62 }))
63β
64 const rendered = render(CreateQuery, {
65 props: {
66 options: derivedStore,
67 queryClient: new QueryClient(),
68 },
69 })
70β
71 await waitFor(() => {
72 expect(rendered.queryByText('Success')).toBeInTheDocument()
73 })
74 })
75β
76 test('Ensure reactivity when queryClient defaults are set', async () => {
77 const writableStore = writable(1)
78β
79 const derivedStore = derived(writableStore, ($store) => ({
80 queryKey: [$store],
81 queryFn: async () => {
82 await sleep(10)
83 return `Success ${$store}`
84 },
85 }))
86β
87 const rendered = render(CreateQuery, {
88 props: {
89 options: derivedStore,
90 queryClient: new QueryClient({
91 defaultOptions: { queries: { staleTime: 60 * 1000 } },
92 }),
93 },
94 })
95β
96 await waitFor(() => {
97 expect(rendered.queryByText('Success 1')).toBeInTheDocument()
98 expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
99 })
100β
101 writableStore.set(2)
102β
103 await waitFor(() => {
104 expect(rendered.queryByText('Success 1')).not.toBeInTheDocument()
105 expect(rendered.queryByText('Success 2')).toBeInTheDocument()
106 })
107β
108 writableStore.set(1)
109β
110 await waitFor(() => {
111 expect(rendered.queryByText('Success 1')).toBeInTheDocument()
112 expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
113 })
114 })
115β
116 test('Keep previous data when returned as placeholder data', async () => {
117 const writableStore = writable<Array<number>>([1])
118β
119 const derivedStore = derived(writableStore, ($store) => ({
120 queryKey: ['test', $store],
121 queryFn: async () => {
122 await sleep(10)
123 return $store.map((id) => `Success ${id}`)
124 },
125 placeholderData: (previousData: string) => previousData,
126 }))
127β
128 const rendered = render(CreateQuery, {
129 props: {
130 options: derivedStore,
131 queryClient: new QueryClient(),
132 },
133 })
134β
135 await waitFor(() => {
136 expect(rendered.queryByText('Success 1')).not.toBeInTheDocument()
137 expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
138 })
139β
140 await waitFor(() => {
141 expect(rendered.queryByText('Success 1')).toBeInTheDocument()
142 expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
143 })
144β
145 writableStore.set([1, 2])
146β
147 await waitFor(() => {
148 expect(rendered.queryByText('Success 1')).toBeInTheDocument()
149 expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
150 })
151β
152 await waitFor(() => {
153 expect(rendered.queryByText('Success 1')).toBeInTheDocument()
154 expect(rendered.queryByText('Success 2')).toBeInTheDocument()
155 })
156 })
157})