-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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): lazyInit test and init without reactive context
- Loading branch information
1 parent
8012b4f
commit 9449e53
Showing
7 changed files
with
161 additions
and
44 deletions.
There are no files selected for viewing
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
126 changes: 126 additions & 0 deletions
126
packages/angular-query-experimental/src/__tests__/util/lazy-init/lazy-init.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,126 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
type WritableSignal, | ||
computed, | ||
effect, | ||
input, | ||
signal, | ||
} from '@angular/core' | ||
import { TestBed } from '@angular/core/testing' | ||
import { flushQueue, setFixtureSignalInputs } from '../../test-utils' | ||
import { lazyInit } from '../../../util/lazy-init/lazy-init' | ||
|
||
describe('lazyInit', () => { | ||
test('should init lazily in next tick when not accessing manually', async () => { | ||
const mockFn = vi.fn() | ||
|
||
TestBed.runInInjectionContext(() => { | ||
lazyInit(() => { | ||
mockFn() | ||
return { | ||
data: 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 = lazyInit(() => { | ||
mockFn() | ||
return { | ||
data: signal(true), | ||
} | ||
}) | ||
|
||
lazySignal.data() | ||
|
||
console.log(lazySignal) | ||
}) | ||
|
||
expect(mockFn).toHaveBeenCalled() | ||
}) | ||
|
||
test('should init lazily and only once', async () => { | ||
const initCallFn = vi.fn() | ||
const registerDataValue = vi.fn<[number]>() | ||
|
||
let value!: { data: WritableSignal<number> } | ||
const outerSignal = signal(0) | ||
|
||
TestBed.runInInjectionContext(() => { | ||
value = lazyInit(() => { | ||
initCallFn() | ||
|
||
void outerSignal() | ||
|
||
return { data: signal(0) } | ||
}) | ||
|
||
effect(() => registerDataValue(value.data())) | ||
}) | ||
|
||
value.data() | ||
|
||
await flushQueue() | ||
|
||
expect(outerSignal).toBeDefined() | ||
|
||
expect(initCallFn).toHaveBeenCalledTimes(1) | ||
|
||
outerSignal.set(1) | ||
await flushQueue() | ||
outerSignal.set(2) | ||
await flushQueue() | ||
value.data.set(4) | ||
await flushQueue() | ||
|
||
expect(initCallFn).toHaveBeenCalledTimes(1) | ||
expect(registerDataValue).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
test('should support required signal input', async () => { | ||
@Component({ | ||
standalone: true, | ||
template: `{{ call }} - {{ lazySignal.data() }}`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
class Test { | ||
readonly title = input.required<string>() | ||
call = 0 | ||
|
||
lazySignal = lazyInit(() => { | ||
this.call++ | ||
return { | ||
data: computed(() => this.title()), | ||
} | ||
}) | ||
} | ||
|
||
const fixture = TestBed.createComponent(Test) | ||
|
||
setFixtureSignalInputs(fixture, { title: 'newValue' }) | ||
expect(fixture.debugElement.nativeElement.textContent).toBe('0 - newValue') | ||
await flushQueue() | ||
|
||
setFixtureSignalInputs(fixture, { title: 'updatedValue' }) | ||
expect(fixture.debugElement.nativeElement.textContent).toBe( | ||
'1 - updatedValue', | ||
) | ||
|
||
setFixtureSignalInputs(fixture, { title: 'newUpdatedValue' }) | ||
expect(fixture.debugElement.nativeElement.textContent).toBe( | ||
'1 - newUpdatedValue', | ||
) | ||
}) | ||
}) |
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
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
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
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
4 changes: 3 additions & 1 deletion
4
...gular-query-experimental/src/lazy-init.ts → ...erimental/src/util/lazy-init/lazy-init.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