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

lazy-init.test.ts

126 lines2.9 KB

1import { describe, expect, test } from 'vitest'
2import {
3 ChangeDetectionStrategy,
4 Component,
5 type WritableSignal,
6 computed,
7 effect,
8 input,
9 signal,
10} from '@angular/core'
11import { TestBed } from '@angular/core/testing'
12import { flushQueue, setFixtureSignalInputs } from '../../test-utils'
13import { lazyInit } from '../../../util/lazy-init/lazy-init'
14​
15describe('lazyInit', () => {
16 test('should init lazily in next tick when not accessing manually', async () => {
17 const mockFn = vi.fn()
18​
19 TestBed.runInInjectionContext(() => {
20 lazyInit(() => {
21 mockFn()
22 return {
23 data: signal(true),
24 }
25 })
26 })
27​
28 expect(mockFn).not.toHaveBeenCalled()
29​
30 await new Promise(setImmediate)
31​
32 expect(mockFn).toHaveBeenCalled()
33 })
34​
35 test('should init eagerly accessing manually', async () => {
36 const mockFn = vi.fn()
37​
38 TestBed.runInInjectionContext(() => {
39 const lazySignal = lazyInit(() => {
40 mockFn()
41 return {
42 data: signal(true),
43 }
44 })
45​
46 lazySignal.data()
47​
48 console.log(lazySignal)
49 })
50​
51 expect(mockFn).toHaveBeenCalled()
52 })
53​
54 test('should init lazily and only once', async () => {
55 const initCallFn = vi.fn()
56 const registerDataValue = vi.fn<(arg: number) => any>()
57​
58 let value!: { data: WritableSignal<number> }
59 const outerSignal = signal(0)
60​
61 TestBed.runInInjectionContext(() => {
62 value = lazyInit(() => {
63 initCallFn()
64​
65 void outerSignal()
66​
67 return { data: signal(0) }
68 })
69​
70 effect(() => registerDataValue(value.data()))
71 })
72​
73 value.data()
74​
75 await flushQueue()
76​
77 expect(outerSignal).toBeDefined()
78​
79 expect(initCallFn).toHaveBeenCalledTimes(1)
80​
81 outerSignal.set(1)
82 await flushQueue()
83 outerSignal.set(2)
84 await flushQueue()
85 value.data.set(4)
86 await flushQueue()
87​
88 expect(initCallFn).toHaveBeenCalledTimes(1)
89 expect(registerDataValue).toHaveBeenCalledTimes(2)
90 })
91​
92 test('should support required signal input', async () => {
93 @Component({
94 standalone: true,
95 template: `{{ call }} - {{ lazySignal.data() }}`,
96 changeDetection: ChangeDetectionStrategy.OnPush,
97 })
98 class Test {
99 readonly title = input.required<string>()
100 call = 0
101​
102 lazySignal = lazyInit(() => {
103 this.call++
104 return {
105 data: computed(() => this.title()),
106 }
107 })
108 }
109​
110 const fixture = TestBed.createComponent(Test)
111​
112 setFixtureSignalInputs(fixture, { title: 'newValue' })
113 expect(fixture.debugElement.nativeElement.textContent).toBe('0 - newValue')
114 await flushQueue()
115​
116 setFixtureSignalInputs(fixture, { title: 'updatedValue' })
117 expect(fixture.debugElement.nativeElement.textContent).toBe(
118 '1 - updatedValue',
119 )
120​
121 setFixtureSignalInputs(fixture, { title: 'newUpdatedValue' })
122 expect(fixture.debugElement.nativeElement.textContent).toBe(
123 '1 - newUpdatedValue',
124 )
125 })
126})