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

inject-queries.test.ts

196 lines5.5 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import {
3 Component,
4 effect,
5 provideZonelessChangeDetection,
6 signal,
7} from '@angular/core'
8import { TestBed } from '@angular/core/testing'
9import { render } from '@testing-library/angular'
10import { queryKey, sleep } from '@tanstack/query-test-utils'
11import { QueryClient, provideIsRestoring, provideTanStackQuery } from '..'
12import { injectQueries } from '../inject-queries'
13​
14describe('injectQueries', () => {
15 let queryClient: QueryClient
16​
17 beforeEach(() => {
18 vi.useFakeTimers()
19 queryClient = new QueryClient()
20 TestBed.configureTestingModule({
21 providers: [
22 provideZonelessChangeDetection(),
23 provideTanStackQuery(queryClient),
24 ],
25 })
26 })
27​
28 afterEach(() => {
29 vi.useRealTimers()
30 })
31​
32 it('should return the correct states', async () => {
33 const key1 = queryKey()
34 const key2 = queryKey()
35 const results: Array<Array<Record<string, any>>> = []
36​
37 @Component({
38 template: `
39 <div>
40 <div>
41 data1: {{ result()[0].data() ?? 'null' }}, data2:
42 {{ result()[1].data() ?? 'null' }}
43 </div>
44 </div>
45 `,
46 })
47 class Page {
48 readonly result = injectQueries(() => ({
49 queries: [
50 {
51 queryKey: key1,
52 queryFn: () => sleep(10).then(() => 1),
53 },
54 {
55 queryKey: key2,
56 queryFn: () => sleep(100).then(() => 2),
57 },
58 ],
59 }))
60​
61 constructor() {
62 effect(() => {
63 const snapshot = this.result().map((q) => ({ data: q.data() }))
64 results.push(snapshot)
65 })
66 }
67 }
68​
69 const rendered = await render(Page)
70​
71 await vi.advanceTimersByTimeAsync(101)
72 rendered.fixture.detectChanges()
73​
74 expect(rendered.getByText('data1: 1, data2: 2')).toBeInTheDocument()
75​
76 expect(results.length).toBe(3)
77 expect(results[0]).toMatchObject([{ data: undefined }, { data: undefined }])
78 expect(results[1]).toMatchObject([{ data: 1 }, { data: undefined }])
79 expect(results[2]).toMatchObject([{ data: 1 }, { data: 2 }])
80 })
81​
82 it('should return the combined result when combine is provided', async () => {
83 const key1 = queryKey()
84 const key2 = queryKey()
85 const results: Array<Record<string, any>> = []
86​
87 @Component({
88 template: `
89 <div>data: {{ combined().data.join(',') }}</div>
90 <div>isPending: {{ combined().isPending }}</div>
91 `,
92 })
93 class Page {
94 readonly combined = injectQueries(() => ({
95 queries: [
96 {
97 queryKey: key1,
98 queryFn: () => sleep(10).then(() => 1),
99 },
100 {
101 queryKey: key2,
102 queryFn: () => sleep(10).then(() => 2),
103 },
104 ],
105 combine: (queries) => ({
106 data: queries.map((q) => q.data),
107 isPending: queries.some((q) => q.isPending),
108 }),
109 }))
110​
111 constructor() {
112 effect(() => {
113 results.push({ ...this.combined() })
114 })
115 }
116 }
117​
118 const rendered = await render(Page)
119​
120 expect(rendered.getByText('data: ,')).toBeInTheDocument()
121 expect(rendered.getByText('isPending: true')).toBeInTheDocument()
122 expect(results[0]).toMatchObject({
123 data: [undefined, undefined],
124 isPending: true,
125 })
126​
127 await vi.advanceTimersByTimeAsync(11)
128 rendered.fixture.detectChanges()
129​
130 expect(rendered.getByText('data: 1,2')).toBeInTheDocument()
131 expect(rendered.getByText('isPending: false')).toBeInTheDocument()
132 expect(results[results.length - 1]).toMatchObject({
133 data: [1, 2],
134 isPending: false,
135 })
136 expect(results.length).toBeGreaterThanOrEqual(2)
137 })
138​
139 describe('isRestoring', () => {
140 it('should not fetch for the duration of the restoring period when isRestoring is true', async () => {
141 const key1 = queryKey()
142 const key2 = queryKey()
143 const queryFn1 = vi.fn().mockImplementation(() => sleep(10).then(() => 1))
144 const queryFn2 = vi.fn().mockImplementation(() => sleep(10).then(() => 2))
145​
146 TestBed.configureTestingModule({
147 providers: [provideIsRestoring(signal(true).asReadonly())],
148 })
149​
150 @Component({
151 template: `
152 <div>
153 status1: {{ queries()[0].status() }}, fetchStatus1:
154 {{ queries()[0].fetchStatus() }}
155 </div>
156 <div>
157 status2: {{ queries()[1].status() }}, fetchStatus2:
158 {{ queries()[1].fetchStatus() }}
159 </div>
160 `,
161 })
162 class Page {
163 readonly queries = injectQueries(() => ({
164 queries: [
165 { queryKey: key1, queryFn: queryFn1 },
166 { queryKey: key2, queryFn: queryFn2 },
167 ],
168 }))
169 }
170​
171 const rendered = await render(Page)
172​
173 await vi.advanceTimersByTimeAsync(0)
174 rendered.fixture.detectChanges()
175 expect(
176 rendered.getByText('status1: pending, fetchStatus1: idle'),
177 ).toBeInTheDocument()
178 expect(
179 rendered.getByText('status2: pending, fetchStatus2: idle'),
180 ).toBeInTheDocument()
181 expect(queryFn1).toHaveBeenCalledTimes(0)
182 expect(queryFn2).toHaveBeenCalledTimes(0)
183​
184 await vi.advanceTimersByTimeAsync(11)
185 rendered.fixture.detectChanges()
186 expect(
187 rendered.getByText('status1: pending, fetchStatus1: idle'),
188 ).toBeInTheDocument()
189 expect(
190 rendered.getByText('status2: pending, fetchStatus2: idle'),
191 ).toBeInTheDocument()
192 expect(queryFn1).toHaveBeenCalledTimes(0)
193 expect(queryFn2).toHaveBeenCalledTimes(0)
194 })
195 })
196})