Skip to content

Commit a55c8bb

Browse files
atscottthePunderWoman
authored andcommitted
test(core): Ensure signals can be read after view creation during change detection (#52495)
These tests ensure signals can be read in a template after embedded views are created in the middle of template execution of an update pass. The embedded view templates are executed in create mode in the middle of the component template being executed in update mode. This behavior was found to not work correctly in past implementations of the reactive template consumers. PR Close #52495
1 parent 3831942 commit a55c8bb

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

packages/core/test/acceptance/change_detection_signals_in_zones_spec.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {NgFor, NgIf} from '@angular/common';
1010
import {PLATFORM_BROWSER_ID} from '@angular/common/src/platform_id';
11-
import {afterNextRender, ChangeDetectionStrategy, ChangeDetectorRef, Component, computed, Directive, inject, Input, PLATFORM_ID, signal, ViewChild} from '@angular/core';
11+
import {afterNextRender, ChangeDetectionStrategy, ChangeDetectorRef, Component, computed, Directive, inject, Input, PLATFORM_ID, signal, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core';
1212
import {TestBed} from '@angular/core/testing';
1313

1414
describe('CheckAlways components', () => {
@@ -628,6 +628,53 @@ describe('OnPush components with signals', () => {
628628
});
629629

630630
describe('embedded views', () => {
631+
describe('with a signal read after view creation during an update pass', () => {
632+
it('should work with native control flow', () => {
633+
@Component({
634+
template: `
635+
@if (true) { }
636+
{{val()}}
637+
`,
638+
standalone: true,
639+
changeDetection: ChangeDetectionStrategy.OnPush,
640+
})
641+
class MyComp {
642+
val = signal('initial');
643+
}
644+
645+
const fixture = TestBed.createComponent(MyComp);
646+
fixture.detectChanges();
647+
fixture.componentInstance.val.set('new');
648+
fixture.detectChanges();
649+
expect(fixture.nativeElement.innerText).toBe('new');
650+
});
651+
652+
it('should work with createEmbeddedView', () => {
653+
@Component({
654+
template: `
655+
<ng-template #template></ng-template>
656+
{{createEmbeddedView(template)}}
657+
{{val()}}
658+
`,
659+
standalone: true,
660+
changeDetection: ChangeDetectionStrategy.OnPush,
661+
})
662+
class MyComp {
663+
val = signal('initial');
664+
vcr = inject(ViewContainerRef);
665+
createEmbeddedView(ref: TemplateRef<{}>) {
666+
this.vcr.createEmbeddedView(ref);
667+
}
668+
}
669+
670+
const fixture = TestBed.createComponent(MyComp);
671+
fixture.detectChanges();
672+
fixture.componentInstance.val.set('new');
673+
fixture.detectChanges();
674+
expect(fixture.nativeElement.innerText).toBe('new');
675+
});
676+
});
677+
631678
it('refreshes an embedded view in a component', () => {
632679
@Component({
633680
selector: 'signal-component',

0 commit comments

Comments
 (0)