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

inject-query.test.ts

218 lines5.7 KB

1import { computed, signal } from '@angular/core'
2import { TestBed, fakeAsync, flush, tick } from '@angular/core/testing'
3import { QueryClient } from '@tanstack/query-core'
4import { expect, vi } from 'vitest'
5import { injectQuery } from '../inject-query'
6import { provideAngularQuery } from '../providers'
7import {
8 delayedFetcher,
9 getSimpleFetcherWithReturnData,
10 rejectFetcher,
11 simpleFetcher,
12} from './test-utils'
13​
14describe('injectQuery', () => {
15 beforeEach(() => {
16 TestBed.configureTestingModule({
17 providers: [provideAngularQuery(new QueryClient())],
18 })
19 })
20​
21 it('should return pending status initially', fakeAsync(() => {
22 const query = TestBed.runInInjectionContext(() => {
23 return injectQuery(() => ({
24 queryKey: ['key1'],
25 queryFn: simpleFetcher,
26 }))
27 })
28​
29 expect(query.status()).toBe('pending')
30 expect(query.isPending()).toBe(true)
31 expect(query.isFetching()).toBe(true)
32 expect(query.isStale()).toBe(true)
33 expect(query.isFetched()).toBe(false)
34​
35 flush()
36 }))
37​
38 it('should resolve to success and update signal: injectQuery()', fakeAsync(() => {
39 const query = TestBed.runInInjectionContext(() => {
40 return injectQuery(() => ({
41 queryKey: ['key2'],
42 queryFn: getSimpleFetcherWithReturnData('result2'),
43 }))
44 })
45​
46 flush()
47​
48 expect(query.status()).toBe('success')
49 expect(query.data()).toBe('result2')
50 expect(query.isPending()).toBe(false)
51 expect(query.isFetching()).toBe(false)
52 expect(query.isFetched()).toBe(true)
53 expect(query.isSuccess()).toBe(true)
54 }))
55​
56 it('should reject and update signal', fakeAsync(() => {
57 const query = TestBed.runInInjectionContext(() => {
58 return injectQuery(() => ({
59 retry: false,
60 queryKey: ['key3'],
61 queryFn: rejectFetcher,
62 }))
63 })
64​
65 flush()
66​
67 expect(query.status()).toBe('error')
68 expect(query.data()).toBe(undefined)
69 expect(query.error()).toMatchObject({ message: 'Some error' })
70 expect(query.isPending()).toBe(false)
71 expect(query.isFetching()).toBe(false)
72 expect(query.isError()).toBe(true)
73 expect(query.failureCount()).toBe(1)
74 expect(query.failureReason()).toMatchObject({ message: 'Some error' })
75 }))
76​
77 it('should update query on options contained signal change', fakeAsync(() => {
78 const key = signal(['key6', 'key7'])
79 const spy = vi.fn(simpleFetcher)
80​
81 const query = TestBed.runInInjectionContext(() => {
82 return injectQuery(() => ({
83 queryKey: key(),
84 queryFn: spy,
85 }))
86 })
87 flush()
88 expect(spy).toHaveBeenCalledTimes(1)
89​
90 expect(query.status()).toBe('success')
91​
92 key.set(['key8'])
93 TestBed.flushEffects()
94​
95 expect(spy).toHaveBeenCalledTimes(2)
96​
97 flush()
98 }))
99​
100 it('should only run query once enabled signal is set to true', fakeAsync(() => {
101 const spy = vi.fn(simpleFetcher)
102 const enabled = signal(false)
103​
104 const query = TestBed.runInInjectionContext(() => {
105 return injectQuery(() => ({
106 queryKey: ['key9'],
107 queryFn: spy,
108 enabled: enabled(),
109 }))
110 })
111​
112 expect(spy).not.toHaveBeenCalled()
113 expect(query.status()).toBe('pending')
114​
115 enabled.set(true)
116 TestBed.flushEffects()
117 flush()
118 expect(spy).toHaveBeenCalledTimes(1)
119 expect(query.status()).toBe('success')
120 }))
121​
122 it('should properly execute dependant queries', fakeAsync(() => {
123 const query1 = TestBed.runInInjectionContext(() => {
124 return injectQuery(() => ({
125 queryKey: ['dependant1'],
126 queryFn: simpleFetcher,
127 }))
128 })
129​
130 const dependentQueryFn = vi.fn().mockImplementation(delayedFetcher(1000))
131​
132 const query2 = TestBed.runInInjectionContext(() => {
133 return injectQuery(
134 computed(() => ({
135 queryKey: ['dependant2'],
136 queryFn: dependentQueryFn,
137 enabled: !!query1.data(),
138 })),
139 )
140 })
141​
142 expect(query1.data()).toStrictEqual(undefined)
143 expect(query2.fetchStatus()).toStrictEqual('idle')
144 expect(dependentQueryFn).not.toHaveBeenCalled()
145​
146 tick()
147 TestBed.flushEffects()
148​
149 expect(query1.data()).toStrictEqual('Some data')
150 expect(query2.fetchStatus()).toStrictEqual('fetching')
151​
152 flush()
153​
154 expect(query2.fetchStatus()).toStrictEqual('idle')
155 expect(query2.status()).toStrictEqual('success')
156 expect(dependentQueryFn).toHaveBeenCalledTimes(1)
157 expect(dependentQueryFn).toHaveBeenCalledWith(
158 expect.objectContaining({ queryKey: ['dependant2'] }),
159 )
160 }))
161​
162 it('should use the current value for the queryKey when refetch is called', fakeAsync(() => {
163 const fetchFn = vi.fn(simpleFetcher)
164 const keySignal = signal('key11')
165​
166 const query = TestBed.runInInjectionContext(() => {
167 return injectQuery(() => ({
168 queryKey: ['key10', keySignal()],
169 queryFn: fetchFn,
170 enabled: false,
171 }))
172 })
173​
174 expect(fetchFn).not.toHaveBeenCalled()
175​
176 query.refetch().then(() => {
177 expect(fetchFn).toHaveBeenCalledTimes(1)
178 expect(fetchFn).toHaveBeenCalledWith(
179 expect.objectContaining({
180 queryKey: ['key10', 'key11'],
181 }),
182 )
183 })
184​
185 flush()
186​
187 keySignal.set('key12')
188​
189 TestBed.flushEffects()
190​
191 query.refetch().then(() => {
192 expect(fetchFn).toHaveBeenCalledTimes(2)
193 expect(fetchFn).toHaveBeenCalledWith(
194 expect.objectContaining({
195 queryKey: ['key10', 'key12'],
196 }),
197 )
198 })
199​
200 flush()
201 }))
202​
203 it('should set state to error when queryFn returns reject promise', fakeAsync(() => {
204 const query = TestBed.runInInjectionContext(() => {
205 return injectQuery(() => ({
206 retry: false,
207 queryKey: ['key13'],
208 queryFn: rejectFetcher,
209 }))
210 })
211​
212 expect(query.status()).toBe('pending')
213​
214 flush()
215​
216 expect(query.status()).toBe('error')
217 }))
218})