Open History to see the last commit
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})