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

inject-is-mutating.test.ts

59 lines1.6 KB

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