Skip to content

Commit b67caee

Browse files
sukvvonmanudeli
andauthored
test(angular-query-experimental/inject-mutation): use precise time in 'advanceTimersByTimeAsync' (#9434)
Co-authored-by: Jonghyeon Ko <manudeli.ko@gmail.com>
1 parent 343501c commit b67caee

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

packages/angular-query-experimental/src/__tests__/inject-mutation.test.ts

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import { sleep } from '@tanstack/query-test-utils'
1212
import { QueryClient, injectMutation, provideTanStackQuery } from '..'
1313
import { expectSignals, setFixtureSignalInputs } from './test-utils'
1414

15-
const MUTATION_DURATION = 1000
16-
17-
const resolveMutations = () => vi.advanceTimersByTimeAsync(MUTATION_DURATION)
18-
1915
describe('injectMutation', () => {
2016
let queryClient: QueryClient
2117

@@ -49,19 +45,19 @@ describe('injectMutation', () => {
4945
})
5046
})
5147

52-
test('should change state after invoking mutate', () => {
48+
test('should change state after invoking mutate', async () => {
5349
const result = 'Mock data'
5450

5551
const mutation = TestBed.runInInjectionContext(() => {
5652
return injectMutation(() => ({
57-
mutationFn: (params: string) => sleep(0).then(() => params),
53+
mutationFn: (params: string) => sleep(10).then(() => params),
5854
}))
5955
})
6056

6157
TestBed.tick()
6258

6359
mutation.mutate(result)
64-
vi.advanceTimersByTime(1)
60+
await vi.advanceTimersByTimeAsync(0)
6561

6662
expectSignals(mutation, {
6763
isIdle: false,
@@ -77,13 +73,13 @@ describe('injectMutation', () => {
7773
const mutation = TestBed.runInInjectionContext(() => {
7874
return injectMutation(() => ({
7975
mutationFn: () =>
80-
sleep(0).then(() => Promise.reject(new Error('Some error'))),
76+
sleep(10).then(() => Promise.reject(new Error('Some error'))),
8177
}))
8278
})
8379

8480
mutation.mutate()
8581

86-
await resolveMutations()
82+
await vi.advanceTimersByTimeAsync(11)
8783

8884
expectSignals(mutation, {
8985
isIdle: false,
@@ -99,13 +95,13 @@ describe('injectMutation', () => {
9995
const result = 'Mock data'
10096
const mutation = TestBed.runInInjectionContext(() => {
10197
return injectMutation(() => ({
102-
mutationFn: (params: string) => sleep(0).then(() => params),
98+
mutationFn: (params: string) => sleep(10).then(() => params),
10399
}))
104100
})
105101

106102
mutation.mutate(result)
107103

108-
await resolveMutations()
104+
await vi.advanceTimersByTimeAsync(11)
109105

110106
expectSignals(mutation, {
111107
isIdle: false,
@@ -142,19 +138,19 @@ describe('injectMutation', () => {
142138
const mutation = TestBed.runInInjectionContext(() => {
143139
return injectMutation(() => ({
144140
mutationFn: () =>
145-
sleep(0).then(() => Promise.reject(new Error('Some error'))),
141+
sleep(10).then(() => Promise.reject(new Error('Some error'))),
146142
}))
147143
})
148144

149145
mutation.mutate()
150146

151-
await resolveMutations()
147+
await vi.advanceTimersByTimeAsync(11)
152148

153149
expect(mutation.isError()).toBe(true)
154150

155151
mutation.reset()
156152

157-
await resolveMutations()
153+
await vi.advanceTimersByTimeAsync(0)
158154

159155
expectSignals(mutation, {
160156
isIdle: true,
@@ -175,14 +171,14 @@ describe('injectMutation', () => {
175171
const onMutate = vi.fn()
176172
const mutation = TestBed.runInInjectionContext(() => {
177173
return injectMutation(() => ({
178-
mutationFn: (params: string) => sleep(0).then(() => params),
174+
mutationFn: (params: string) => sleep(10).then(() => params),
179175
onMutate,
180176
}))
181177
})
182178

183179
mutation.mutate('')
184180

185-
await resolveMutations()
181+
await vi.advanceTimersByTimeAsync(0)
186182

187183
expect(onMutate).toHaveBeenCalledTimes(1)
188184
})
@@ -192,14 +188,14 @@ describe('injectMutation', () => {
192188
const mutation = TestBed.runInInjectionContext(() => {
193189
return injectMutation(() => ({
194190
mutationFn: (_params: string) =>
195-
sleep(0).then(() => Promise.reject(new Error('Some error'))),
191+
sleep(10).then(() => Promise.reject(new Error('Some error'))),
196192
onError,
197193
}))
198194
})
199195

200196
mutation.mutate('')
201197

202-
await resolveMutations()
198+
await vi.advanceTimersByTimeAsync(11)
203199

204200
expect(onError).toHaveBeenCalledTimes(1)
205201
})
@@ -208,14 +204,14 @@ describe('injectMutation', () => {
208204
const onSuccess = vi.fn()
209205
const mutation = TestBed.runInInjectionContext(() => {
210206
return injectMutation(() => ({
211-
mutationFn: (params: string) => sleep(0).then(() => params),
207+
mutationFn: (params: string) => sleep(10).then(() => params),
212208
onSuccess,
213209
}))
214210
})
215211

216212
mutation.mutate('')
217213

218-
await resolveMutations()
214+
await vi.advanceTimersByTimeAsync(11)
219215

220216
expect(onSuccess).toHaveBeenCalledTimes(1)
221217
})
@@ -224,14 +220,14 @@ describe('injectMutation', () => {
224220
const onSettled = vi.fn()
225221
const mutation = TestBed.runInInjectionContext(() => {
226222
return injectMutation(() => ({
227-
mutationFn: (params: string) => sleep(0).then(() => params),
223+
mutationFn: (params: string) => sleep(10).then(() => params),
228224
onSettled,
229225
}))
230226
})
231227

232228
mutation.mutate('')
233229

234-
await resolveMutations()
230+
await vi.advanceTimersByTimeAsync(11)
235231

236232
expect(onSettled).toHaveBeenCalledTimes(1)
237233
})
@@ -241,13 +237,13 @@ describe('injectMutation', () => {
241237
const mutation = TestBed.runInInjectionContext(() => {
242238
return injectMutation(() => ({
243239
mutationFn: (_params: string) =>
244-
sleep(0).then(() => Promise.reject(new Error('Some error'))),
240+
sleep(10).then(() => Promise.reject(new Error('Some error'))),
245241
}))
246242
})
247243

248244
mutation.mutate('', { onError })
249245

250-
await resolveMutations()
246+
await vi.advanceTimersByTimeAsync(11)
251247

252248
expect(onError).toHaveBeenCalledTimes(1)
253249
})
@@ -256,13 +252,13 @@ describe('injectMutation', () => {
256252
const onSuccess = vi.fn()
257253
const mutation = TestBed.runInInjectionContext(() => {
258254
return injectMutation(() => ({
259-
mutationFn: (params: string) => sleep(0).then(() => params),
255+
mutationFn: (params: string) => sleep(10).then(() => params),
260256
}))
261257
})
262258

263259
mutation.mutate('', { onSuccess })
264260

265-
await resolveMutations()
261+
await vi.advanceTimersByTimeAsync(11)
266262

267263
expect(onSuccess).toHaveBeenCalledTimes(1)
268264
})
@@ -271,13 +267,13 @@ describe('injectMutation', () => {
271267
const onSettled = vi.fn()
272268
const mutation = TestBed.runInInjectionContext(() => {
273269
return injectMutation(() => ({
274-
mutationFn: (params: string) => sleep(0).then(() => params),
270+
mutationFn: (params: string) => sleep(10).then(() => params),
275271
}))
276272
})
277273

278274
mutation.mutate('', { onSettled })
279275

280-
await resolveMutations()
276+
await vi.advanceTimersByTimeAsync(11)
281277

282278
expect(onSettled).toHaveBeenCalledTimes(1)
283279
})
@@ -287,14 +283,14 @@ describe('injectMutation', () => {
287283
const onSettledOnFunction = vi.fn()
288284
const mutation = TestBed.runInInjectionContext(() => {
289285
return injectMutation(() => ({
290-
mutationFn: (params: string) => sleep(0).then(() => params),
286+
mutationFn: (params: string) => sleep(10).then(() => params),
291287
onSettled,
292288
}))
293289
})
294290

295291
mutation.mutate('', { onSettled: onSettledOnFunction })
296292

297-
await resolveMutations()
293+
await vi.advanceTimersByTimeAsync(11)
298294

299295
expect(onSettled).toHaveBeenCalledTimes(1)
300296
expect(onSettledOnFunction).toHaveBeenCalledTimes(1)
@@ -317,7 +313,7 @@ describe('injectMutation', () => {
317313

318314
mutation = injectMutation(() => ({
319315
mutationKey: ['fake', this.name()],
320-
mutationFn: () => sleep(0).then(() => this.name()),
316+
mutationFn: () => sleep(10).then(() => this.name()),
321317
}))
322318

323319
mutate(): void {
@@ -332,7 +328,7 @@ describe('injectMutation', () => {
332328
const button = debugElement.query(By.css('button'))
333329
button.triggerEventHandler('click')
334330

335-
await resolveMutations()
331+
await vi.advanceTimersByTimeAsync(11)
336332
fixture.detectChanges()
337333

338334
const text = debugElement.query(By.css('span')).nativeElement.textContent
@@ -358,7 +354,7 @@ describe('injectMutation', () => {
358354

359355
mutation = injectMutation(() => ({
360356
mutationKey: ['fake', this.name()],
361-
mutationFn: () => sleep(0).then(() => this.name()),
357+
mutationFn: () => sleep(10).then(() => this.name()),
362358
}))
363359

364360
mutate(): void {
@@ -374,15 +370,15 @@ describe('injectMutation', () => {
374370
const span = debugElement.query(By.css('span'))
375371

376372
button.triggerEventHandler('click')
377-
await resolveMutations()
373+
await vi.advanceTimersByTimeAsync(11)
378374
fixture.detectChanges()
379375

380376
expect(span.nativeElement.textContent).toEqual('value')
381377

382378
setFixtureSignalInputs(fixture, { name: 'updatedValue' })
383379

384380
button.triggerEventHandler('click')
385-
await resolveMutations()
381+
await vi.advanceTimersByTimeAsync(11)
386382
fixture.detectChanges()
387383

388384
expect(span.nativeElement.textContent).toEqual('updatedValue')
@@ -412,7 +408,7 @@ describe('injectMutation', () => {
412408

413409
mutate()
414410

415-
await resolveMutations()
411+
await vi.advanceTimersByTimeAsync(0)
416412

417413
expect(boundaryFn).toHaveBeenCalledTimes(1)
418414
expect(boundaryFn).toHaveBeenCalledWith(err)

0 commit comments

Comments
 (0)