Open History to see the last commit
1import { TestBed } from '@angular/core/testing'
2import { beforeEach, describe, expect, it } from 'vitest'
3import { Injector, provideZonelessChangeDetection, signal } from '@angular/core'
4import {
5 QueryClient,
6 injectIsRestoring,
7 provideIsRestoring,
8 provideTanStackQuery,
9} from '..'
10β
11describe('injectIsRestoring', () => {
12 let queryClient: QueryClient
13β
14 beforeEach(() => {
15 queryClient = new QueryClient()
16 TestBed.configureTestingModule({
17 providers: [
18 provideZonelessChangeDetection(),
19 provideTanStackQuery(queryClient),
20 ],
21 })
22 })
23β
24 it('should return false by default when provideIsRestoring is not used', () => {
25 const isRestoring = TestBed.runInInjectionContext(() => {
26 return injectIsRestoring()
27 })
28β
29 expect(isRestoring()).toBe(false)
30 })
31β
32 it('should return the provided signal value when provideIsRestoring is used', () => {
33 const restoringSignal = signal(true)
34β
35 TestBed.configureTestingModule({
36 providers: [provideIsRestoring(restoringSignal.asReadonly())],
37 })
38β
39 const isRestoring = TestBed.runInInjectionContext(() => {
40 return injectIsRestoring()
41 })
42β
43 expect(isRestoring()).toBe(true)
44 })
45β
46 it('should reactively reflect changes to the provided signal', () => {
47 const restoringSignal = signal(true)
48β
49 TestBed.configureTestingModule({
50 providers: [provideIsRestoring(restoringSignal.asReadonly())],
51 })
52β
53 const isRestoring = TestBed.runInInjectionContext(() => {
54 return injectIsRestoring()
55 })
56β
57 expect(isRestoring()).toBe(true)
58β
59 restoringSignal.set(false)
60 expect(isRestoring()).toBe(false)
61β
62 restoringSignal.set(true)
63 expect(isRestoring()).toBe(true)
64 })
65β
66 it('should be usable outside injection context when passing an injector', () => {
67 const isRestoring = injectIsRestoring({
68 injector: TestBed.inject(Injector),
69 })
70β
71 expect(isRestoring()).toBe(false)
72 })
73β
74 it('should return the provided signal value when using injector option', () => {
75 const restoringSignal = signal(true)
76β
77 TestBed.configureTestingModule({
78 providers: [provideIsRestoring(restoringSignal.asReadonly())],
79 })
80β
81 const isRestoring = injectIsRestoring({
82 injector: TestBed.inject(Injector),
83 })
84β
85 expect(isRestoring()).toBe(true)
86 })
87β
88 it('should throw NG0203 with descriptive error outside injection context', () => {
89 expect(() => {
90 injectIsRestoring()
91 }).toThrow(/NG0203(.*?)injectIsRestoring/)
92 })
93})