-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular-query): lazy signal initializer test
- Loading branch information
1 parent
bbeb63b
commit 302aee0
Showing
2 changed files
with
148 additions
and
11 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
...y-experimental/src/__tests__/util/lazy-signal-initializer/lazy-signal-initializer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { | ||
Component, | ||
type Signal, | ||
type WritableSignal, | ||
effect, | ||
input, | ||
signal, | ||
} from '@angular/core' | ||
import { TestBed } from '@angular/core/testing' | ||
import { lazySignalInitializer } from '../../../util/lazy-signal-initializer/lazy-signal-initializer' | ||
import { setFixtureSignalInputs } from '../../test-utils' | ||
|
||
async function flushQueue() { | ||
await new Promise(setImmediate) | ||
} | ||
|
||
describe('lazySignalInitializer', () => { | ||
test('should init lazily in next tick when not accessing manually', async () => { | ||
const mockFn = vi.fn() | ||
|
||
TestBed.runInInjectionContext(() => { | ||
lazySignalInitializer(() => { | ||
mockFn() | ||
return signal(true) | ||
}) | ||
}) | ||
|
||
expect(mockFn).not.toHaveBeenCalled() | ||
|
||
await new Promise(setImmediate) | ||
|
||
expect(mockFn).toHaveBeenCalled() | ||
}) | ||
|
||
test('should init eagerly accessing manually', async () => { | ||
const mockFn = vi.fn() | ||
|
||
TestBed.runInInjectionContext(() => { | ||
const lazySignal = lazySignalInitializer(() => { | ||
mockFn() | ||
return signal(true) | ||
}) | ||
|
||
lazySignal() | ||
}) | ||
|
||
expect(mockFn).toHaveBeenCalled() | ||
}) | ||
|
||
test('should init lazily and only once', async () => { | ||
const initCallFn = vi.fn() | ||
const registerEffectValue = vi.fn<[number]>() | ||
|
||
let value!: Signal<number> | ||
const outerSignal = signal(0) | ||
let innerSignal!: WritableSignal<number> | ||
|
||
TestBed.runInInjectionContext(() => { | ||
value = lazySignalInitializer(() => { | ||
initCallFn() | ||
innerSignal = signal(0) | ||
|
||
void outerSignal() | ||
|
||
return innerSignal | ||
}) | ||
|
||
effect(() => registerEffectValue(value())) | ||
}) | ||
|
||
expect(initCallFn).toHaveBeenCalledTimes(0) | ||
expect(outerSignal).toBeDefined() | ||
expect(innerSignal).not.toBeDefined() | ||
|
||
await flushQueue() | ||
|
||
expect(outerSignal).toBeDefined() | ||
expect(innerSignal).toBeDefined() | ||
|
||
expect(initCallFn).toHaveBeenCalledTimes(1) | ||
|
||
innerSignal.set(1) | ||
await flushQueue() | ||
outerSignal.set(2) | ||
await flushQueue() | ||
|
||
expect(initCallFn).toHaveBeenCalledTimes(1) | ||
expect(registerEffectValue).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
test('should init lazily', async () => { | ||
@Component({ | ||
standalone: true, | ||
template: `{{ subscribed }}`, | ||
}) | ||
class Test { | ||
subscribed = false | ||
|
||
lazySignal = lazySignalInitializer(() => { | ||
this.subscribed = true | ||
return signal('value') | ||
}) | ||
} | ||
|
||
const fixture = TestBed.createComponent(Test) | ||
const { debugElement } = fixture | ||
fixture.detectChanges() | ||
|
||
expect(debugElement.nativeElement.textContent).toBe('false') | ||
|
||
await new Promise(setImmediate) | ||
|
||
fixture.detectChanges() | ||
|
||
expect(debugElement.nativeElement.textContent).toBe('true') | ||
}) | ||
|
||
test('should support required signal input', () => { | ||
@Component({ | ||
standalone: true, | ||
template: `{{ subscribed }}`, | ||
}) | ||
class Test { | ||
readonly title = input.required<string>() | ||
subscribed = false | ||
|
||
lazySignal = lazySignalInitializer(() => { | ||
return signal(this.title()) | ||
}) | ||
} | ||
|
||
const fixture = TestBed.createComponent(Test) | ||
setFixtureSignalInputs(fixture, { title: 'newValue' }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters