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

inject-is-mutating.test.ts

68 lines1.7 KB

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