Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { TestBed } from '@angular/core/testing'
3import { Injector } from '@angular/core'
4import { sleep } from '@tanstack/query-test-utils'
5import { QueryClient, injectIsMutating, injectMutation } from '..'
6import { flushQueryUpdates, setupTanStackQueryTestBed } from './test-utils'
7β
8describe('injectIsMutating', () => {
9 let queryClient: QueryClient
10β
11 beforeEach(() => {
12 vi.useFakeTimers()
13 queryClient = new QueryClient()
14β
15 setupTanStackQueryTestBed(queryClient)
16 })
17β
18 afterEach(() => {
19 vi.useRealTimers()
20 })
21β
22 test('should properly return isMutating state', async () => {
23 const [mutation, isMutating] = TestBed.runInInjectionContext(() => [
24 injectMutation(() => ({
25 mutationKey: ['isMutating1'],
26 mutationFn: (params: { par1: string }) => sleep(10).then(() => params),
27 })),
28 injectIsMutating(),
29 ])
30β
31 expect(isMutating()).toBe(0)
32β
33 mutation.mutate({
34 par1: 'par1',
35 })
36β
37 expect(isMutating()).toBe(0)
38 await flushQueryUpdates()
39 expect(isMutating()).toBe(1)
40 await vi.advanceTimersByTimeAsync(11)
41 expect(isMutating()).toBe(0)
42 })
43β
44 describe('injection context', () => {
45 test('throws NG0203 with descriptive error outside injection context', () => {
46 expect(() => {
47 injectIsMutating()
48 }).toThrowError(/NG0203(.*?)injectIsMutating/)
49 })
50β
51 test('can be used outside injection context when passing an injector', () => {
52 expect(
53 injectIsMutating(undefined, {
54 injector: TestBed.inject(Injector),
55 }),
56 ).not.toThrow()
57 })
58 })
59})