|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {PLATFORM_BROWSER_ID, PLATFORM_SERVER_ID} from '@angular/common/src/platform_id'; |
10 | | -import {afterNextRender, afterRender, AfterRenderPhase, AfterRenderRef, ApplicationRef, ChangeDetectorRef, Component, computed, createComponent, effect, ErrorHandler, inject, Injector, NgZone, PLATFORM_ID, Type, ViewContainerRef, ɵinternalAfterNextRender as internalAfterNextRender} from '@angular/core'; |
| 10 | +import {afterNextRender, afterRender, AfterRenderPhase, AfterRenderRef, ApplicationRef, ChangeDetectorRef, Component, computed, createComponent, effect, ErrorHandler, inject, Injector, NgZone, PLATFORM_ID, signal, Type, ViewContainerRef, ɵinternalAfterNextRender as internalAfterNextRender} from '@angular/core'; |
11 | 11 | import {NoopNgZone} from '@angular/core/src/zone/ng_zone'; |
12 | 12 | import {TestBed} from '@angular/core/testing'; |
13 | 13 |
|
@@ -894,6 +894,98 @@ describe('after render hooks', () => { |
894 | 894 | ]); |
895 | 895 | }); |
896 | 896 | }); |
| 897 | + |
| 898 | + it('allows writing to a signal in afterRender', () => { |
| 899 | + const counter = signal(0); |
| 900 | + |
| 901 | + @Component({ |
| 902 | + selector: 'test-component', |
| 903 | + standalone: true, |
| 904 | + template: ` {{counter()}} `, |
| 905 | + }) |
| 906 | + class TestCmp { |
| 907 | + counter = counter; |
| 908 | + injector = inject(EnvironmentInjector); |
| 909 | + ngOnInit() { |
| 910 | + afterNextRender(() => { |
| 911 | + this.counter.set(1); |
| 912 | + }, {injector: this.injector}); |
| 913 | + } |
| 914 | + } |
| 915 | + TestBed.configureTestingModule( |
| 916 | + {providers: [{provide: PLATFORM_ID, useValue: PLATFORM_BROWSER_ID}]}); |
| 917 | + |
| 918 | + const fixture = TestBed.createComponent(TestCmp); |
| 919 | + const appRef = TestBed.inject(ApplicationRef); |
| 920 | + appRef.attachView(fixture.componentRef.hostView); |
| 921 | + appRef.tick(); |
| 922 | + expect(fixture.nativeElement.innerText).toBe('1'); |
| 923 | + }); |
| 924 | + |
| 925 | + it('allows updating state and calling markForCheck in afterRender', () => { |
| 926 | + @Component({ |
| 927 | + selector: 'test-component', |
| 928 | + standalone: true, |
| 929 | + template: ` {{counter}} `, |
| 930 | + }) |
| 931 | + class TestCmp { |
| 932 | + counter = 0; |
| 933 | + injector = inject(EnvironmentInjector); |
| 934 | + cdr = inject(ChangeDetectorRef); |
| 935 | + ngOnInit() { |
| 936 | + afterNextRender(() => { |
| 937 | + this.counter = 1; |
| 938 | + this.cdr.markForCheck(); |
| 939 | + }, {injector: this.injector}); |
| 940 | + } |
| 941 | + } |
| 942 | + TestBed.configureTestingModule( |
| 943 | + {providers: [{provide: PLATFORM_ID, useValue: PLATFORM_BROWSER_ID}]}); |
| 944 | + |
| 945 | + const fixture = TestBed.createComponent(TestCmp); |
| 946 | + const appRef = TestBed.inject(ApplicationRef); |
| 947 | + appRef.attachView(fixture.componentRef.hostView); |
| 948 | + appRef.tick(); |
| 949 | + expect(fixture.nativeElement.innerText).toBe('1'); |
| 950 | + }); |
| 951 | + |
| 952 | + it('throws error when causing infinite updates', () => { |
| 953 | + const counter = signal(0); |
| 954 | + |
| 955 | + @Component({ |
| 956 | + selector: 'test-component', |
| 957 | + standalone: true, |
| 958 | + template: ` {{counter()}} `, |
| 959 | + }) |
| 960 | + class TestCmp { |
| 961 | + counter = counter; |
| 962 | + injector = inject(EnvironmentInjector); |
| 963 | + ngOnInit() { |
| 964 | + afterRender(() => { |
| 965 | + this.counter.update(v => v + 1); |
| 966 | + }, {injector: this.injector}); |
| 967 | + } |
| 968 | + } |
| 969 | + TestBed.configureTestingModule({ |
| 970 | + providers: [ |
| 971 | + {provide: PLATFORM_ID, useValue: PLATFORM_BROWSER_ID}, |
| 972 | + { |
| 973 | + provide: ErrorHandler, useClass: class extends ErrorHandler { |
| 974 | + override handleError(error: any) { |
| 975 | + throw error; |
| 976 | + } |
| 977 | + } |
| 978 | + }, |
| 979 | + ] |
| 980 | + }); |
| 981 | + |
| 982 | + const fixture = TestBed.createComponent(TestCmp); |
| 983 | + const appRef = TestBed.inject(ApplicationRef); |
| 984 | + appRef.attachView(fixture.componentRef.hostView); |
| 985 | + expect(() => { |
| 986 | + appRef.tick(); |
| 987 | + }).toThrowError(/NG0103.*(afterRender|afterNextRender)/); |
| 988 | + }); |
897 | 989 | }); |
898 | 990 |
|
899 | 991 | describe('server', () => { |
|
0 commit comments