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