angular/packages/forms/signals/test/node/field_proxy.spec.ts
Alex Rickabaugh b96f65a963 fix(forms): memoize reads of child fields in signal forms (#65802)
Previously, navigating a `FieldTree` in signal forms involved reactive reads
of the value of the parent field(s), both directly and via `.childrenMap()`.

This meant that on _any_ change to the value of a field, reactive
notifications would trigger updates of computeds, reruns of effects, etc.
So for example, this effect would run on every change to the form:

```ts
const f = form(signal({data: 'abc', unrelated: 0}));
effect(() => {
  // accessing f.data incurs a dependency on f().value() which changes
  // on every change in the whole form
  console.log(f.data().value());
});
```

This is deeply counterintuitive and troublesome when attempting to write
effect logic, and also results in `computed`s unnecessarily updating.

This change introduces the concept of a "reader" computed, which memoizes
the access of a field at a given key via the reactive graph. With this, the
same `f.data` access above now depends on the `data` reader in `f` only,
which is effectively a constant computed. As a result, the effect only
reruns on changes to `data`'s value, as intended.

PR Close #65802
2025-12-03 12:52:42 -08:00

155 lines
4.6 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
Component,
Input,
Injector,
input,
signal,
ApplicationRef,
effect,
untracked,
computed,
} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {form, FieldTree} from '../../public_api';
import {ChangeDetectionStrategy} from '@angular/compiler';
describe('FieldTree proxy', () => {
it('should not forward methods through the proxy', () => {
const f = form(signal(new Date()), {injector: TestBed.inject(Injector)});
// @ts-expect-error
expect(f.getDate).toBe(undefined as any);
});
it('should allow spreading field arrays', () => {
const f = form(signal([0, 1, 2]), {injector: TestBed.inject(Injector)});
expect([...f].map((i) => i().value())).toEqual([0, 1, 2]);
});
it('should not allow mutation of the field structure', () => {
const f = form(signal({arr: [0, 1]}), {injector: TestBed.inject(Injector)});
// Just to have an expectation, really this test is just to check the typings below.
expect(f).toBeDefined();
// @ts-expect-error
f.arr = f.arr;
// @ts-expect-error
f.arr[0] = f.arr[0];
});
it('should get keys and values for object field', () => {
const f = form(signal({x: 1, y: 2}), {injector: TestBed.inject(Injector)});
expect(Object.keys(f)).toEqual(['x', 'y']);
expect(Object.getOwnPropertyNames(f)).toEqual(['x', 'y']);
expect(Object.entries(f).map(([key, child]) => [key, child().value()])).toEqual([
['x', 1],
['y', 2],
]);
expect(Object.values(f).map((child) => child().value())).toEqual([1, 2]);
});
it('should get keys and values for array field', () => {
const f = form(signal([1, 2]), {injector: TestBed.inject(Injector)});
expect(Object.keys(f)).toEqual(['0', '1']);
expect(Object.getOwnPropertyNames(f)).toEqual(['0', '1', 'length']);
expect(Object.entries(f).map(([key, child]) => [key, child().value()])).toEqual([
['0', 1],
['1', 2],
]);
expect(Object.values(f).map((child) => child().value())).toEqual([1, 2]);
});
it('should be reactive when children change, but not on value mutations', async () => {
const injector = TestBed.inject(Injector);
const appRef = injector.get(ApplicationRef);
const value = signal<{a: number; b?: number}>({a: 1});
const f = form(value, {injector});
expect(f.b).not.toBeDefined();
const log: string[] = [];
effect(
() => {
log.push(`a: ${f.a().value()}`);
},
{injector},
);
await appRef.whenStable();
expect(log).toEqual(['a: 1']);
value.set({a: 1, b: 2});
await appRef.whenStable();
expect(log).toEqual(['a: 1']);
value.set({a: 2, b: 2});
await appRef.whenStable();
expect(log).toEqual(['a: 1', 'a: 2']);
});
it('should be reactive to array values swapping', async () => {
const injector = TestBed.inject(Injector);
const appRef = injector.get(ApplicationRef);
const value = signal([{value: 1}, {value: 2}]);
const f = form(value, {injector});
const log: string[] = [];
effect(
() => {
log.push(`[${untracked(f[0]().value).value}, ${untracked(f[1]().value).value}]`);
},
{injector},
);
await appRef.whenStable();
expect(log).toEqual(['[1, 2]']);
value.update((v) => [v[1], v[0]]);
appRef.tick();
expect(log).toEqual(['[1, 2]', '[2, 1]']);
});
it('should get keys and values for primitive field', () => {
const f = form(signal(1), {injector: TestBed.inject(Injector)});
expect(Object.keys(f)).toEqual([]);
expect(Object.getOwnPropertyNames(f)).toEqual([]);
expect(Object.entries(f)).toEqual([]);
expect(Object.values(f)).toEqual([]);
});
it('should iterate over object field', () => {
const f = form(signal({x: 1, y: 2}), {injector: TestBed.inject(Injector)});
const result: [string, number][] = [];
for (const [key, child] of f) {
result.push([key, child().value()]);
}
expect(result).toEqual([
['x', 1],
['y', 2],
]);
});
it('should iterate over array field', () => {
const f = form(signal([1, 2]), {injector: TestBed.inject(Injector)});
const result: number[] = [];
for (const child of f) {
result.push(child().value());
}
expect(result).toEqual([1, 2]);
});
it('should not iterate over primitive field', () => {
const f = form(signal(1), {injector: TestBed.inject(Injector)});
expect(() => {
// @ts-expect-error - not iterable
for (const child of f) {
}
}).toThrow();
});
});