From 51cee50ee3b338f3de6da2444fccbbba097a79db Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Wed, 20 Nov 2019 18:03:51 +0100 Subject: [PATCH] test(ivy): non-regression test for ViewContainerRef queried on ng-container (#33939) Closes #31971 PR Close #33939 --- .../test/acceptance/view_insertion_spec.ts | 94 ++++++++++++++----- 1 file changed, 72 insertions(+), 22 deletions(-) diff --git a/packages/core/test/acceptance/view_insertion_spec.ts b/packages/core/test/acceptance/view_insertion_spec.ts index 9e846e25dd..41099fd247 100644 --- a/packages/core/test/acceptance/view_insertion_spec.ts +++ b/packages/core/test/acceptance/view_insertion_spec.ts @@ -543,12 +543,62 @@ describe('view insertion', () => { describe('non-regression', () => { - // https://github.com/angular/angular/issues/33679 - it('should insert views into ViewContainerRef injected by querying ', () => { + // https://github.com/angular/angular/issues/31971 + it('should insert component views into ViewContainerRef injected by querying ', + () => { - @Component({ - selector: 'app-root', - template: ` + @Component({selector: 'dynamic-cmpt', template: 'dynamic'}) + class DynamicComponent { + } + + @Component({ + selector: 'app-root', + template: ` +
start|
+ +
|end
+ +
|click
+ ` + }) + class AppComponent { + @ViewChild('container', {read: ViewContainerRef, static: true}) + vcr !: ViewContainerRef; + + constructor(private _cfr: ComponentFactoryResolver) {} + + click() { + this.vcr.createComponent(this._cfr.resolveComponentFactory(DynamicComponent)); + } + } + + @NgModule({ + declarations: [DynamicComponent], + entryComponents: [DynamicComponent], + }) + class TestModule { + } + + TestBed.configureTestingModule({ + declarations: [AppComponent], + imports: [TestModule], + }); + const fixture = TestBed.createComponent(AppComponent); + fixture.detectChanges(); + expect(fixture.nativeElement.textContent).toBe('start||end|click'); + + fixture.componentInstance.click(); + fixture.detectChanges(); + expect(fixture.nativeElement.textContent).toBe('start|dynamic|end|click'); + }); + + // https://github.com/angular/angular/issues/33679 + it('should insert embedded views into ViewContainerRef injected by querying ', + () => { + + @Component({ + selector: 'app-root', + template: `
container start|
|container end
@@ -556,27 +606,27 @@ describe('view insertion', () => { test
|click
` - }) - class AppComponent { - @ViewChild('container', {read: ViewContainerRef, static: true}) - vcr !: ViewContainerRef; + }) + class AppComponent { + @ViewChild('container', {read: ViewContainerRef, static: true}) + vcr !: ViewContainerRef; - @ViewChild('template', {read: TemplateRef, static: true}) template !: TemplateRef; + @ViewChild('template', {read: TemplateRef, static: true}) template !: TemplateRef; - click() { this.vcr.createEmbeddedView(this.template, undefined, 0); } - } + click() { this.vcr.createEmbeddedView(this.template, undefined, 0); } + } - TestBed.configureTestingModule({ - declarations: [AppComponent], - }); - const fixture = TestBed.createComponent(AppComponent); - fixture.detectChanges(); - expect(fixture.nativeElement.textContent).toBe('container start||container end|click'); + TestBed.configureTestingModule({ + declarations: [AppComponent], + }); + const fixture = TestBed.createComponent(AppComponent); + fixture.detectChanges(); + expect(fixture.nativeElement.textContent).toBe('container start||container end|click'); - fixture.componentInstance.click(); - fixture.detectChanges(); - expect(fixture.nativeElement.textContent).toBe('container start|test|container end|click'); - }); + fixture.componentInstance.click(); + fixture.detectChanges(); + expect(fixture.nativeElement.textContent).toBe('container start|test|container end|click'); + }); it('should properly insert before views in a ViewContainerRef injected on ng-container', () => { @Component({