-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathinject-is-fetching.test.ts
69 lines (58 loc) · 1.65 KB
/
inject-is-fetching.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 { TestBed } from '@angular/core/testing'
import { beforeEach, describe, expect } from 'vitest'
import {
Injector,
provideExperimentalZonelessChangeDetection,
} from '@angular/core'
import {
QueryClient,
injectIsFetching,
injectQuery,
provideTanStackQuery,
} from '..'
import { delayedFetcher } from './test-utils'
const QUERY_DURATION = 100
const resolveQueries = () => vi.advanceTimersByTimeAsync(QUERY_DURATION)
describe('injectIsFetching', () => {
let queryClient: QueryClient
beforeEach(() => {
vi.useFakeTimers()
queryClient = new QueryClient()
TestBed.configureTestingModule({
providers: [
provideExperimentalZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})
})
afterEach(() => {
vi.useRealTimers()
})
test('Returns number of fetching queries', async () => {
const isFetching = TestBed.runInInjectionContext(() => {
injectQuery(() => ({
queryKey: ['isFetching1'],
queryFn: delayedFetcher(100),
}))
return injectIsFetching()
})
vi.advanceTimersByTime(1)
expect(isFetching()).toStrictEqual(1)
await resolveQueries()
expect(isFetching()).toStrictEqual(0)
})
describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectIsFetching()
}).toThrowError(/NG0203(.*?)injectIsFetching/)
})
test('can be used outside injection context when passing an injector', () => {
expect(
injectIsFetching(undefined, {
injector: TestBed.inject(Injector),
}),
).not.toThrow()
})
})
})