-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathinject-is-mutating.test.ts
More file actions
113 lines (94 loc) · 3.1 KB
/
inject-is-mutating.test.ts
File metadata and controls
113 lines (94 loc) · 3.1 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { TestBed } from '@angular/core/testing'
import {
Component,
Injector,
provideZonelessChangeDetection,
} from '@angular/core'
import { render } from '@testing-library/angular'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import {
QueryClient,
injectIsMutating,
injectMutation,
provideTanStackQuery,
} from '..'
describe('injectIsMutating', () => {
let queryClient: QueryClient
beforeEach(() => {
vi.useFakeTimers()
queryClient = new QueryClient()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})
})
afterEach(() => {
vi.useRealTimers()
})
it('should properly return isMutating state', async () => {
const key = queryKey()
@Component({
template: `<div>mutating: {{ isMutating() }}</div>`,
})
class Page {
readonly mutation = injectMutation(() => ({
mutationKey: key,
mutationFn: (params: { par1: string }) => sleep(10).then(() => params),
}))
readonly isMutating = injectIsMutating()
}
const rendered = await render(Page)
expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
rendered.fixture.componentInstance.mutation.mutate({ par1: 'par1' })
await vi.advanceTimersByTimeAsync(0)
rendered.fixture.detectChanges()
expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(11)
rendered.fixture.detectChanges()
expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
})
it('should be able to filter by mutationKey', async () => {
const key1 = queryKey()
const key2 = queryKey()
@Component({
template: `<div>mutating: {{ isMutating() }}</div>`,
})
class Page {
readonly mutation1 = injectMutation(() => ({
mutationKey: key1,
mutationFn: () => sleep(10).then(() => 'data1'),
}))
readonly mutation2 = injectMutation(() => ({
mutationKey: key2,
mutationFn: () => sleep(100).then(() => 'data2'),
}))
readonly isMutating = injectIsMutating({ mutationKey: key1 })
}
const rendered = await render(Page)
rendered.fixture.componentInstance.mutation1.mutate()
rendered.fixture.componentInstance.mutation2.mutate()
await vi.advanceTimersByTimeAsync(0)
rendered.fixture.detectChanges()
expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(11)
rendered.fixture.detectChanges()
expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
})
describe('injection context', () => {
it('should throw NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectIsMutating()
}).toThrow(/NG0203(.*?)injectIsMutating/)
})
it('should be usable outside injection context when passing an injector', () => {
expect(
injectIsMutating(undefined, {
injector: TestBed.inject(Injector),
}),
).not.toThrow()
})
})
})