test(ivy): move content projection tests to acceptance (#30357)
Moves all manual `render3` content projection tests that use the `ɵɵelementProperty` to acceptance tests. Additionally a few other content projection tests were moved over to acceptance. Eventually we'll be able to move all remaining content projection tests to acceptance. PR Close #30357
This commit is contained in:

committed by
Alex Rickabaugh

parent
24e172d779
commit
1f2b39ad7d
@ -6,11 +6,393 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ChangeDetectorRef, Component, Directive} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {ChangeDetectorRef, Component, Directive, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core';
|
||||
import {Input} from '@angular/core/src/metadata';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {By} from '@angular/platform-browser';
|
||||
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
||||
|
||||
describe('projection', () => {
|
||||
|
||||
function getElementHtml(element: HTMLElement) {
|
||||
return element.innerHTML.replace(/<!--(\W|\w)*?-->/g, '')
|
||||
.replace(/\sng-reflect-\S*="[^"]*"/g, '');
|
||||
}
|
||||
|
||||
it('should project content', () => {
|
||||
@Component({selector: 'child', template: `<div><ng-content></ng-content></div>`})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({selector: 'parent', template: '<child>content</child>'})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.nativeElement.innerHTML).toBe(`<child><div>content</div></child>`);
|
||||
});
|
||||
|
||||
it('should project content when <ng-content> is at a template root', () => {
|
||||
@Component({
|
||||
selector: 'child',
|
||||
template: '<ng-content></ng-content>',
|
||||
})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent',
|
||||
template: '<child>content</child>',
|
||||
})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.nativeElement.innerHTML).toBe(`<child>content</child>`);
|
||||
});
|
||||
|
||||
it('should project content with siblings', () => {
|
||||
@Component({selector: 'child', template: '<ng-content></ng-content>'})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({selector: 'parent', template: `<child>before<div>content</div>after</child>`})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.nativeElement.innerHTML).toBe(`<child>before<div>content</div>after</child>`);
|
||||
});
|
||||
|
||||
it('should be able to re-project content', () => {
|
||||
@Component({selector: 'grand-child', template: `<div><ng-content></ng-content></div>`})
|
||||
class GrandChild {
|
||||
}
|
||||
|
||||
@Component(
|
||||
{selector: 'child', template: `<grand-child><ng-content></ng-content></grand-child>`})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent',
|
||||
template: `<child><b>Hello</b>World!</child>`,
|
||||
})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child, GrandChild]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.nativeElement.innerHTML)
|
||||
.toBe('<child><grand-child><div><b>Hello</b>World!</div></grand-child></child>');
|
||||
});
|
||||
|
||||
it('should project components', () => {
|
||||
@Component({
|
||||
selector: 'child',
|
||||
template: `<div><ng-content></ng-content></div>`,
|
||||
})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'projected-comp',
|
||||
template: 'content',
|
||||
})
|
||||
class ProjectedComp {
|
||||
}
|
||||
|
||||
@Component({selector: 'parent', template: `<child><projected-comp></projected-comp></child>`})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child, ProjectedComp]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.nativeElement.innerHTML)
|
||||
.toBe('<child><div><projected-comp>content</projected-comp></div></child>');
|
||||
});
|
||||
|
||||
it('should project components that have their own projection', () => {
|
||||
@Component({selector: 'child', template: `<div><ng-content></ng-content></div>`})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({selector: 'projected-comp', template: `<p><ng-content></ng-content></p>`})
|
||||
class ProjectedComp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent',
|
||||
template: `
|
||||
<child>
|
||||
<projected-comp><div>Some content</div>Other content</projected-comp>
|
||||
</child>`,
|
||||
})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child, ProjectedComp]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.nativeElement.innerHTML)
|
||||
.toBe(
|
||||
`<child><div><projected-comp><p><div>Some content</div>Other content</p></projected-comp></div></child>`);
|
||||
});
|
||||
|
||||
it('should project into dynamic views (with createEmbeddedView)', () => {
|
||||
@Component({
|
||||
selector: 'child',
|
||||
template: `Before-<ng-template [ngIf]="showing"><ng-content></ng-content></ng-template>-After`
|
||||
})
|
||||
class Child {
|
||||
showing = false;
|
||||
}
|
||||
|
||||
@Component({selector: 'parent', template: `<child><div>A</div>Some text</child>`})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child], imports: [CommonModule]});
|
||||
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
const childDebugEl = fixture.debugElement.query(By.directive(Child));
|
||||
const childInstance = childDebugEl.injector.get(Child);
|
||||
const childElement = childDebugEl.nativeElement as HTMLElement;
|
||||
|
||||
childInstance.showing = true;
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getElementHtml(childElement)).toBe(`Before-<div>A</div>Some text-After`);
|
||||
|
||||
childInstance.showing = false;
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getElementHtml(childElement)).toBe(`Before--After`);
|
||||
|
||||
childInstance.showing = true;
|
||||
fixture.detectChanges();
|
||||
expect(getElementHtml(childElement)).toBe(`Before-<div>A</div>Some text-After`);
|
||||
});
|
||||
|
||||
it('should project into dynamic views with specific selectors', () => {
|
||||
@Component({
|
||||
selector: 'child',
|
||||
template: `
|
||||
<ng-content></ng-content>
|
||||
Before-
|
||||
<ng-template [ngIf]="showing">
|
||||
<ng-content select="div"></ng-content>
|
||||
</ng-template>
|
||||
-After`
|
||||
})
|
||||
class Child {
|
||||
showing = false;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent',
|
||||
template: `
|
||||
<child>
|
||||
<div>A</div>
|
||||
<span>B</span>
|
||||
</child>
|
||||
`
|
||||
})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child], imports: [CommonModule]});
|
||||
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
const childDebugEl = fixture.debugElement.query(By.directive(Child));
|
||||
const childInstance = childDebugEl.injector.get(Child);
|
||||
|
||||
childInstance.showing = true;
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getElementHtml(fixture.nativeElement))
|
||||
.toBe('<child><span>B</span> Before- <div>A</div> -After</child>');
|
||||
|
||||
childInstance.showing = false;
|
||||
fixture.detectChanges();
|
||||
expect(getElementHtml(fixture.nativeElement))
|
||||
.toBe('<child><span>B</span> Before- -After</child>');
|
||||
|
||||
childInstance.showing = true;
|
||||
fixture.detectChanges();
|
||||
expect(getElementHtml(fixture.nativeElement))
|
||||
.toBe('<child><span>B</span> Before- <div>A</div> -After</child>');
|
||||
});
|
||||
|
||||
it('should project if <ng-content> is in a template that has different declaration/insertion points',
|
||||
() => {
|
||||
@Component(
|
||||
{selector: 'comp', template: `<ng-template><ng-content></ng-content></ng-template>`})
|
||||
class Comp {
|
||||
@ViewChild(TemplateRef) template !: TemplateRef<any>;
|
||||
}
|
||||
|
||||
@Directive({selector: '[trigger]'})
|
||||
class Trigger {
|
||||
@Input() trigger !: Comp;
|
||||
|
||||
constructor(public vcr: ViewContainerRef) {}
|
||||
|
||||
open() { this.vcr.createEmbeddedView(this.trigger.template); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent',
|
||||
template: `
|
||||
<button [trigger]="comp"></button>
|
||||
<comp #comp>Some content</comp>
|
||||
`
|
||||
})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Trigger, Comp]});
|
||||
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
const trigger = fixture.debugElement.query(By.directive(Trigger)).injector.get(Trigger);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getElementHtml(fixture.nativeElement)).toBe(`<button></button><comp></comp>`);
|
||||
|
||||
trigger.open();
|
||||
expect(getElementHtml(fixture.nativeElement))
|
||||
.toBe(`<button></button>Some content<comp></comp>`);
|
||||
});
|
||||
|
||||
// https://stackblitz.com/edit/angular-ceqmnw?file=src%2Fapp%2Fapp.component.ts
|
||||
it('should project nodes into the last ng-content unrolled by ngFor', () => {
|
||||
@Component({
|
||||
selector: 'child',
|
||||
template:
|
||||
`<div *ngFor="let item of [1, 2]; let i = index">({{i}}):<ng-content></ng-content></div>`
|
||||
})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({selector: 'parent', template: `<child>content</child>`})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Parent, Child], imports: [CommonModule]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getElementHtml(fixture.nativeElement))
|
||||
.toBe('<child><div>(0):</div><div>(1):content</div></child>');
|
||||
});
|
||||
|
||||
it('should handle projected containers inside other containers', () => {
|
||||
@Component({selector: 'nested-comp', template: `<div>Child content</div>`})
|
||||
class NestedComp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'root-comp',
|
||||
template: `<ng-content></ng-content>`,
|
||||
})
|
||||
class RootComp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'my-app',
|
||||
template: `
|
||||
<root-comp>
|
||||
<ng-container *ngFor="let item of items; last as last">
|
||||
<nested-comp *ngIf="!last"></nested-comp>
|
||||
</ng-container>
|
||||
</root-comp>
|
||||
`
|
||||
})
|
||||
class MyApp {
|
||||
items = [1, 2];
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule(
|
||||
{declarations: [MyApp, RootComp, NestedComp], imports: [CommonModule]});
|
||||
const fixture = TestBed.createComponent(MyApp);
|
||||
fixture.detectChanges();
|
||||
|
||||
// expecting # of divs to be (items.length - 1), since last element is filtered out by *ngIf,
|
||||
// this applies to all other assertions below
|
||||
expect(fixture.nativeElement.querySelectorAll('div').length).toBe(1);
|
||||
|
||||
fixture.componentInstance.items = [3, 4, 5];
|
||||
fixture.detectChanges();
|
||||
expect(fixture.nativeElement.querySelectorAll('div').length).toBe(2);
|
||||
|
||||
fixture.componentInstance.items = [6, 7, 8, 9];
|
||||
fixture.detectChanges();
|
||||
expect(fixture.nativeElement.querySelectorAll('div').length).toBe(3);
|
||||
});
|
||||
|
||||
describe('with selectors', () => {
|
||||
// https://stackblitz.com/edit/angular-psokum?file=src%2Fapp%2Fapp.module.ts
|
||||
it('should project nodes where attribute selector matches a binding', () => {
|
||||
@Component({
|
||||
selector: 'child',
|
||||
template: `<ng-content select="[title]"></ng-content>`,
|
||||
})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent',
|
||||
template: `<child><span [title]="'Some title'">Has title</span></child>`
|
||||
})
|
||||
class Parent {
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [Child, Parent]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getElementHtml(fixture.nativeElement))
|
||||
.toEqual('<child><span title="Some title">Has title</span></child>');
|
||||
|
||||
});
|
||||
|
||||
it('should match selectors against projected containers', () => {
|
||||
@Component(
|
||||
{selector: 'child', template: `<span><ng-content select="div"></ng-content></span>`})
|
||||
class Child {
|
||||
}
|
||||
|
||||
@Component({template: `<child><div *ngIf="value">content</div></child>`})
|
||||
class Parent {
|
||||
value = false;
|
||||
}
|
||||
TestBed.configureTestingModule({declarations: [Child, Parent]});
|
||||
const fixture = TestBed.createComponent(Parent);
|
||||
|
||||
fixture.componentInstance.value = true;
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getElementHtml(fixture.nativeElement))
|
||||
.toEqual('<child><span><div>content</div></span></child>');
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle projected containers inside other containers', () => {
|
||||
@Component({
|
||||
selector: 'child-comp', //
|
||||
|
Reference in New Issue
Block a user