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

inject-is-mutating.test.ts

66 lines1.6 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', () => {
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})