test(ivy): port TemplateRef tests over to acceptance tests (#30443)
- Moves template ref tests from render3 unit tests to acceptance tests. - Marks tests testing ivy specific changes as `onlyInIvy`. - Deletes old render3 unit tests that are no longer necessary PR Close #30443
This commit is contained in:
@ -9,6 +9,7 @@
|
||||
import {Component, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
||||
import {onlyInIvy} from '@angular/private/testing';
|
||||
|
||||
describe('TemplateRef', () => {
|
||||
describe('rootNodes', () => {
|
||||
@ -52,6 +53,117 @@ describe('TemplateRef', () => {
|
||||
|
||||
expect(rootNodeTextContent).toEqual(['Header', 'Item one', 'Item two']);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return root render nodes for an embedded view instance', () => {
|
||||
@Component({
|
||||
template: `
|
||||
<ng-template #templateRef>
|
||||
<div></div>
|
||||
some text
|
||||
<span></span>
|
||||
</ng-template>
|
||||
`
|
||||
})
|
||||
class App {
|
||||
@ViewChild('templateRef')
|
||||
templateRef !: TemplateRef<any>;
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [App],
|
||||
});
|
||||
const fixture = TestBed.createComponent(App);
|
||||
fixture.detectChanges();
|
||||
|
||||
const embeddedView = fixture.componentInstance.templateRef.createEmbeddedView({});
|
||||
expect(embeddedView.rootNodes.length).toBe(3);
|
||||
});
|
||||
|
||||
/**
|
||||
* This is different as compared to the view engine implementation which returns a comment node
|
||||
* in this case:
|
||||
* https://stackblitz.com/edit/angular-uiqry6?file=src/app/app.component.ts
|
||||
*
|
||||
* Returning a comment node for a template ref with no nodes is wrong is fixed in Ivy.
|
||||
*/
|
||||
onlyInIvy('Fixed: Ivy no longer adds a comment node in this case.')
|
||||
.it('should return an empty array for embedded view with no nodes', () => {
|
||||
@Component({
|
||||
template: `
|
||||
<ng-template #templateRef></ng-template>
|
||||
`
|
||||
})
|
||||
class App {
|
||||
@ViewChild('templateRef')
|
||||
templateRef !: TemplateRef<any>;
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [App],
|
||||
});
|
||||
const fixture = TestBed.createComponent(App);
|
||||
fixture.detectChanges();
|
||||
|
||||
const embeddedView = fixture.componentInstance.templateRef.createEmbeddedView({});
|
||||
expect(embeddedView.rootNodes.length).toBe(0);
|
||||
});
|
||||
|
||||
it('should not descend into containers when retrieving root nodes', () => {
|
||||
/**
|
||||
* NOTE: In VE, if `SUFFIX` text node below is _not_ present, VE will add an
|
||||
* additional `<!---->` comment, thus being slightly different than Ivy.
|
||||
* (resulting in 1 root node in Ivy and 2 in VE).
|
||||
*/
|
||||
@Component({
|
||||
template: `
|
||||
<ng-template #templateRef><ng-template [ngIf]="true">text</ng-template>SUFFIX</ng-template>
|
||||
`
|
||||
})
|
||||
class App {
|
||||
@ViewChild('templateRef')
|
||||
templateRef !: TemplateRef<any>;
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [App],
|
||||
});
|
||||
const fixture = TestBed.createComponent(App);
|
||||
fixture.detectChanges();
|
||||
|
||||
const embeddedView = fixture.componentInstance.templateRef.createEmbeddedView({});
|
||||
expect(embeddedView.rootNodes.length).toBe(2);
|
||||
expect(embeddedView.rootNodes[0].nodeType).toBe(Node.COMMENT_NODE);
|
||||
expect(embeddedView.rootNodes[1].nodeType).toBe(Node.TEXT_NODE);
|
||||
});
|
||||
|
||||
/**
|
||||
* Contrary to containers (<ng-template>) we _do_ descend into element containers
|
||||
* (<ng-container>)
|
||||
*/
|
||||
it('should descend into element containers when retrieving root nodes', () => {
|
||||
@Component({
|
||||
template: `
|
||||
<ng-template #templateRef>
|
||||
<ng-container>text</ng-container>
|
||||
</ng-template>
|
||||
`
|
||||
})
|
||||
class App {
|
||||
@ViewChild('templateRef')
|
||||
templateRef !: TemplateRef<any>;
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [App],
|
||||
});
|
||||
const fixture = TestBed.createComponent(App);
|
||||
fixture.detectChanges();
|
||||
|
||||
const embeddedView = fixture.componentInstance.templateRef.createEmbeddedView({});
|
||||
|
||||
expect(embeddedView.rootNodes.length).toBe(2);
|
||||
expect(embeddedView.rootNodes[0].nodeType).toBe(Node.COMMENT_NODE);
|
||||
expect(embeddedView.rootNodes[1].nodeType).toBe(Node.TEXT_NODE);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user