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

inject-is-mutating.test.ts

56 lines1.4 KB

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