fix(ivy): attach host element for views created via TestBed.createComponent (#31318)

Prior to this commit, host element of a view created via TestBed.createComponent was not attached to the component's host, making it problematic to use TestBed.createComponent API in component factories, which might be used for testing purposes only. This behavior is observed in google3 app tests and was supported by VE, so this commit aligns Ivy and VE.

PR Close #31318
This commit is contained in:
Andrew Kushnir 2019-06-27 10:56:40 -07:00 committed by Alex Rickabaugh
parent a29dc961a2
commit c12b6fa028
2 changed files with 35 additions and 2 deletions

View File

@ -214,8 +214,9 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
this.componentType, component,
createElementRef(viewEngine_ElementRef, tElementNode, rootLView), rootLView, tElementNode);
if (isInternalRootView) {
// The host element of the internal root view is attached to the component's host view node
if (isInternalRootView || isIsolated) {
// The host element of the internal or isolated root view is attached to the component's host
// view node.
componentRef.hostView._tViewNode !.child = tElementNode;
}
return componentRef;

View File

@ -1086,6 +1086,38 @@ describe('ViewContainerRef', () => {
expect((element.namespaceURI || '').toLowerCase()).not.toContain('svg');
});
it('should be compatible with componentRef generated via TestBed.createComponent in component factory',
() => {
@Component({
selector: 'child',
template: `Child Component`,
})
class Child {
}
@Component({
selector: 'comp',
template: '<ng-template #ref></ng-template>',
})
class Comp {
@ViewChild('ref', {read: ViewContainerRef, static: true})
viewContainerRef?: ViewContainerRef;
ngOnInit() {
const makeComponentFactory = (componentType: any) => ({
create: () => TestBed.createComponent(componentType).componentRef,
});
this.viewContainerRef !.createComponent(makeComponentFactory(Child) as any);
}
}
TestBed.configureTestingModule({declarations: [Comp, Child]});
const fixture = TestBed.createComponent(Comp);
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.innerHTML).toContain('Child Component');
});
});
describe('insertion points and declaration points', () => {