forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject-is-mutating.test.ts
69 lines (58 loc) · 1.6 KB
/
inject-is-mutating.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { beforeEach, describe } from 'vitest'
import { TestBed } from '@angular/core/testing'
import {
Injector,
provideExperimentalZonelessChangeDetection,
} from '@angular/core'
import {
QueryClient,
injectIsMutating,
injectMutation,
provideTanStackQuery,
} from '..'
import { successMutator } from './test-utils'
describe('injectIsMutating', () => {
let queryClient: QueryClient
beforeEach(() => {
vi.useFakeTimers()
queryClient = new QueryClient()
TestBed.configureTestingModule({
providers: [
provideExperimentalZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})
})
afterEach(() => {
vi.useRealTimers()
})
test('should properly return isMutating state', async () => {
TestBed.runInInjectionContext(() => {
const isMutating = injectIsMutating()
const mutation = injectMutation(() => ({
mutationKey: ['isMutating1'],
mutationFn: successMutator<{ par1: string }>,
}))
expect(isMutating()).toBe(0)
mutation.mutate({
par1: 'par1',
})
vi.advanceTimersByTime(1)
expect(isMutating()).toBe(1)
})
})
describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectIsMutating()
}).toThrowError(/NG0203(.*?)injectIsMutating/)
})
test('can be used outside injection context when passing an injector', () => {
expect(
injectIsMutating(undefined, {
injector: TestBed.inject(Injector),
}),
).not.toThrow()
})
})
})