|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import {Component, Directive, effect, forwardRef, signal} from '@angular/core'; |
| 10 | +import {TestBed} from '@angular/core/testing'; |
| 11 | +import {of} from 'rxjs'; |
9 | 12 | import { |
10 | 13 | AbstractControl, |
| 14 | + ControlValueAccessor, |
11 | 15 | FormArray, |
12 | 16 | FormControl, |
13 | 17 | FormGroup, |
| 18 | + NG_VALUE_ACCESSOR, |
| 19 | + ReactiveFormsModule, |
14 | 20 | ValidationErrors, |
15 | 21 | ValidatorFn, |
16 | 22 | } from '../index'; |
17 | 23 | import {Validators} from '../src/validators'; |
18 | | -import {of} from 'rxjs'; |
19 | 24 |
|
20 | | -import {useAutoTick, timeout} from '@angular/private/testing'; |
| 25 | +import {timeout, useAutoTick} from '@angular/private/testing'; |
21 | 26 | import {asyncValidator} from './util'; |
22 | 27 |
|
23 | 28 | (function () { |
@@ -1626,5 +1631,132 @@ import {asyncValidator} from './util'; |
1626 | 1631 | }); |
1627 | 1632 | }); |
1628 | 1633 | }); |
| 1634 | + |
| 1635 | + describe('FormArray.setValue is untracked', () => { |
| 1636 | + @Directive({ |
| 1637 | + selector: '[testCva]', |
| 1638 | + providers: [ |
| 1639 | + { |
| 1640 | + provide: NG_VALUE_ACCESSOR, |
| 1641 | + useExisting: forwardRef(() => TestCvaDirective), |
| 1642 | + multi: true, |
| 1643 | + }, |
| 1644 | + ], |
| 1645 | + }) |
| 1646 | + class TestCvaDirective implements ControlValueAccessor { |
| 1647 | + // This is the “dangerous” signal read that must NOT get tracked by the caller of setValue(). |
| 1648 | + static cvaSignal = signal(0); |
| 1649 | + |
| 1650 | + writeValue(value: unknown): void { |
| 1651 | + // If setValue() is not untracked, the *caller* effect/computed may accidentally track this read. |
| 1652 | + TestCvaDirective.cvaSignal(); |
| 1653 | + } |
| 1654 | + |
| 1655 | + registerOnChange(_: (value: unknown) => void): void {} |
| 1656 | + registerOnTouched(_: () => void): void {} |
| 1657 | + setDisabledState(_: boolean): void {} |
| 1658 | + } |
| 1659 | + |
| 1660 | + @Component({ |
| 1661 | + imports: [ReactiveFormsModule, TestCvaDirective], |
| 1662 | + template: `<input testCva [formControl]="control.at(0)" />`, |
| 1663 | + }) |
| 1664 | + class HostComponent { |
| 1665 | + control = new FormArray([new FormControl('')]); |
| 1666 | + } |
| 1667 | + |
| 1668 | + it('should NOT track signals read inside CVA.writeValue when setValue is called inside an effect', async () => { |
| 1669 | + const fixture = TestBed.createComponent(HostComponent); |
| 1670 | + fixture.detectChanges(); // wires up FormControlDirective + CVA |
| 1671 | + |
| 1672 | + const driver = signal('A'); |
| 1673 | + let runs = 0; |
| 1674 | + |
| 1675 | + // Create the effect inside the Angular injection context. |
| 1676 | + TestBed.runInInjectionContext(() => { |
| 1677 | + effect(() => { |
| 1678 | + runs++; |
| 1679 | + |
| 1680 | + // Only dependency we *want* is `driver()`. |
| 1681 | + fixture.componentInstance.control.setValue([driver()]); |
| 1682 | + }); |
| 1683 | + }); |
| 1684 | + |
| 1685 | + await fixture.whenStable(); |
| 1686 | + expect(runs).toBe(1); |
| 1687 | + |
| 1688 | + // Changing the CVA signal should NOT re-run the effect. |
| 1689 | + TestCvaDirective.cvaSignal.set(1); |
| 1690 | + await fixture.whenStable(); |
| 1691 | + expect(runs).toBe(1); |
| 1692 | + |
| 1693 | + // Changing the driver signal SHOULD re-run the effect. |
| 1694 | + driver.set('B'); |
| 1695 | + await fixture.whenStable(); |
| 1696 | + expect(runs).toBe(2); |
| 1697 | + }); |
| 1698 | + |
| 1699 | + it('should NOT track signals read inside CVA.writeValue when patchValue is called inside an effect', async () => { |
| 1700 | + const fixture = TestBed.createComponent(HostComponent); |
| 1701 | + fixture.detectChanges(); // wires up FormControlDirective + CVA |
| 1702 | + |
| 1703 | + const driver = signal('A'); |
| 1704 | + let runs = 0; |
| 1705 | + |
| 1706 | + // Create the effect inside the Angular injection context. |
| 1707 | + TestBed.runInInjectionContext(() => { |
| 1708 | + effect(() => { |
| 1709 | + runs++; |
| 1710 | + |
| 1711 | + // Only dependency we *want* is `driver()`. |
| 1712 | + fixture.componentInstance.control.patchValue([driver()]); |
| 1713 | + }); |
| 1714 | + }); |
| 1715 | + |
| 1716 | + await fixture.whenStable(); |
| 1717 | + expect(runs).toBe(1); |
| 1718 | + |
| 1719 | + // Changing the CVA signal should NOT re-run the effect. |
| 1720 | + TestCvaDirective.cvaSignal.set(1); |
| 1721 | + await fixture.whenStable(); |
| 1722 | + expect(runs).toBe(1); |
| 1723 | + |
| 1724 | + // Changing the driver signal SHOULD re-run the effect. |
| 1725 | + driver.set('B'); |
| 1726 | + await fixture.whenStable(); |
| 1727 | + expect(runs).toBe(2); |
| 1728 | + }); |
| 1729 | + |
| 1730 | + it('should NOT track signals read inside CVA.writeValue when reset is called inside an effect', async () => { |
| 1731 | + const fixture = TestBed.createComponent(HostComponent); |
| 1732 | + fixture.detectChanges(); // wires up FormControlDirective + CVA |
| 1733 | + |
| 1734 | + const driver = signal('A'); |
| 1735 | + let runs = 0; |
| 1736 | + |
| 1737 | + // Create the effect inside the Angular injection context. |
| 1738 | + TestBed.runInInjectionContext(() => { |
| 1739 | + effect(() => { |
| 1740 | + runs++; |
| 1741 | + |
| 1742 | + // Only dependency we *want* is `driver()`. |
| 1743 | + fixture.componentInstance.control.reset([driver()]); |
| 1744 | + }); |
| 1745 | + }); |
| 1746 | + |
| 1747 | + await fixture.whenStable(); |
| 1748 | + expect(runs).toBe(1); |
| 1749 | + |
| 1750 | + // Changing the CVA signal should NOT re-run the effect. |
| 1751 | + TestCvaDirective.cvaSignal.set(1); |
| 1752 | + await fixture.whenStable(); |
| 1753 | + expect(runs).toBe(1); |
| 1754 | + |
| 1755 | + // Changing the driver signal SHOULD re-run the effect. |
| 1756 | + driver.set('B'); |
| 1757 | + await fixture.whenStable(); |
| 1758 | + expect(runs).toBe(2); |
| 1759 | + }); |
| 1760 | + }); |
1629 | 1761 | }); |
1630 | 1762 | })(); |
0 commit comments