|
| 1 | +import { TestBed } from '@angular/core/testing' |
| 2 | +import { describe, expect, test } from 'vitest' |
| 3 | +import { Injector, provideZonelessChangeDetection, signal } from '@angular/core' |
| 4 | +import { |
| 5 | + QueryClient, |
| 6 | + injectIsRestoring, |
| 7 | + provideIsRestoring, |
| 8 | + provideTanStackQuery, |
| 9 | +} from '..' |
| 10 | + |
| 11 | +describe('injectIsRestoring', () => { |
| 12 | + let queryClient: QueryClient |
| 13 | + |
| 14 | + test('returns false by default when provideIsRestoring is not used', () => { |
| 15 | + queryClient = new QueryClient() |
| 16 | + |
| 17 | + TestBed.configureTestingModule({ |
| 18 | + providers: [ |
| 19 | + provideZonelessChangeDetection(), |
| 20 | + provideTanStackQuery(queryClient), |
| 21 | + ], |
| 22 | + }) |
| 23 | + |
| 24 | + const isRestoring = TestBed.runInInjectionContext(() => { |
| 25 | + return injectIsRestoring() |
| 26 | + }) |
| 27 | + |
| 28 | + expect(isRestoring()).toBe(false) |
| 29 | + }) |
| 30 | + |
| 31 | + test('returns provided signal value when provideIsRestoring is used', () => { |
| 32 | + queryClient = new QueryClient() |
| 33 | + const restoringSignal = signal(true) |
| 34 | + |
| 35 | + TestBed.configureTestingModule({ |
| 36 | + providers: [ |
| 37 | + provideZonelessChangeDetection(), |
| 38 | + provideTanStackQuery(queryClient), |
| 39 | + provideIsRestoring(restoringSignal.asReadonly()), |
| 40 | + ], |
| 41 | + }) |
| 42 | + |
| 43 | + const isRestoring = TestBed.runInInjectionContext(() => { |
| 44 | + return injectIsRestoring() |
| 45 | + }) |
| 46 | + |
| 47 | + expect(isRestoring()).toBe(true) |
| 48 | + }) |
| 49 | + |
| 50 | + test('can be used outside injection context when passing an injector', () => { |
| 51 | + queryClient = new QueryClient() |
| 52 | + |
| 53 | + TestBed.configureTestingModule({ |
| 54 | + providers: [ |
| 55 | + provideZonelessChangeDetection(), |
| 56 | + provideTanStackQuery(queryClient), |
| 57 | + ], |
| 58 | + }) |
| 59 | + |
| 60 | + const isRestoring = injectIsRestoring({ |
| 61 | + injector: TestBed.inject(Injector), |
| 62 | + }) |
| 63 | + |
| 64 | + expect(isRestoring()).toBe(false) |
| 65 | + }) |
| 66 | + |
| 67 | + test('throws NG0203 with descriptive error outside injection context', () => { |
| 68 | + expect(() => { |
| 69 | + injectIsRestoring() |
| 70 | + }).toThrowError(/NG0203(.*?)injectIsRestoring/) |
| 71 | + }) |
| 72 | +}) |
0 commit comments