Open History to see the last commit
1import { TestBed, fakeAsync, flush, tick } from '@angular/core/testing'
2import { beforeEach, describe, expect } from 'vitest'
3import { Injector } from '@angular/core'
4import {
5 QueryClient,
6 injectIsFetching,
7 injectQuery,
8 provideAngularQuery,
9} from '..'
10import { delayedFetcher } from './test-utils'
11β
12describe('injectIsFetching', () => {
13 let queryClient: QueryClient
14β
15 beforeEach(() => {
16 queryClient = new QueryClient()
17β
18 TestBed.configureTestingModule({
19 providers: [provideAngularQuery(queryClient)],
20 })
21 })
22β
23 test('Returns number of fetching queries', fakeAsync(() => {
24 const isFetching = TestBed.runInInjectionContext(() => {
25 injectQuery(() => ({
26 queryKey: ['isFetching1'],
27 queryFn: delayedFetcher(100),
28 }))
29 return injectIsFetching()
30 })
31β
32 tick()
33β
34 expect(isFetching()).toStrictEqual(1)
35 flush()
36 expect(isFetching()).toStrictEqual(0)
37 }))
38β
39 describe('injection context', () => {
40 test('throws NG0203 with descriptive error outside injection context', () => {
41 expect(() => {
42 injectIsFetching()
43 }).toThrowError(/NG0203(.*?)injectIsFetching/)
44 })
45β
46 test('can be used outside injection context when passing an injector', () => {
47 expect(
48 injectIsFetching(undefined, TestBed.inject(Injector)),
49 ).not.toThrow()
50 })
51 })
52})