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 { 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', () => {
32 TestBed.runInInjectionContext(() => {
33 const isMutating = injectIsMutating()
34 const mutation = injectMutation(() => ({
35 mutationKey: ['isMutating1'],
36 mutationFn: (params: { par1: string }) => sleep(0).then(() => params),
37 }))
38β
39 expect(isMutating()).toBe(0)
40β
41 mutation.mutate({
42 par1: 'par1',
43 })
44β
45 vi.advanceTimersByTime(1)
46β
47 expect(isMutating()).toBe(1)
48 })
49 })
50β
51 describe('injection context', () => {
52 test('throws NG0203 with descriptive error outside injection context', () => {
53 expect(() => {
54 injectIsMutating()
55 }).toThrowError(/NG0203(.*?)injectIsMutating/)
56 })
57β
58 test('can be used outside injection context when passing an injector', () => {
59 expect(
60 injectIsMutating(undefined, {
61 injector: TestBed.inject(Injector),
62 }),
63 ).not.toThrow()
64 })
65 })
66})