Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

inject-is-restoring.test.ts

93 lines2.4 KB

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})