Open History to see the last commit
1import { beforeEach, describe } from 'vitest'
2import { TestBed } from '@angular/core/testing'
3import {
4 Injector,
5 provideExperimentalZonelessChangeDetection,
6} from '@angular/core'
7import {
8 QueryClient,
9 injectIsMutating,
10 injectMutation,
11 provideTanStackQuery,
12} from '..'
13import { successMutator } from './test-utils'
14β
15describe('injectIsMutating', () => {
16 let queryClient: QueryClient
17β
18 beforeEach(() => {
19 vi.useFakeTimers()
20 queryClient = new QueryClient()
21β
22 TestBed.configureTestingModule({
23 providers: [
24 provideExperimentalZonelessChangeDetection(),
25 provideTanStackQuery(queryClient),
26 ],
27 })
28 })
29β
30 afterEach(() => {
31 vi.useRealTimers()
32 })
33β
34 test('should properly return isMutating state', () => {
35 TestBed.runInInjectionContext(() => {
36 const isMutating = injectIsMutating()
37 const mutation = injectMutation(() => ({
38 mutationKey: ['isMutating1'],
39 mutationFn: successMutator<{ par1: string }>,
40 }))
41β
42 expect(isMutating()).toBe(0)
43β
44 mutation.mutate({
45 par1: 'par1',
46 })
47β
48 vi.advanceTimersByTime(1)
49β
50 expect(isMutating()).toBe(1)
51 })
52 })
53β
54 describe('injection context', () => {
55 test('throws NG0203 with descriptive error outside injection context', () => {
56 expect(() => {
57 injectIsMutating()
58 }).toThrowError(/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})