Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

inject-is-mutating.test.ts

67 lines1.6 KB

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', async () => {
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, TestBed.inject(Injector)),
64 ).not.toThrow()
65 })
66 })
67})