2016-06-08 19:38:52 -04:00
|
|
|
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xit,} from '@angular/core/testing/testing_internal';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
|
|
|
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
|
|
|
import {Component, Directive, TemplateRef, ContentChildren, QueryList} from '@angular/core';
|
|
|
|
import {NgTemplateOutlet} from '@angular/common';
|
2016-04-11 11:47:28 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('insert', () => {
|
|
|
|
it('should do nothing if templateRef is null',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template = `<template [ngTemplateOutlet]="null"></template>`;
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
2016-04-11 11:47:28 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-04-11 11:47:28 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-04-11 11:47:28 -04:00
|
|
|
|
|
|
|
it('should insert content specified by TemplateRef',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template =
|
|
|
|
`<tpl-refs #refs="tplRefs"><template>foo</template></tpl-refs><template [ngTemplateOutlet]="currentTplRef"></template>`;
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
2016-04-11 11:47:28 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-04-11 11:47:28 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
var refs = fixture.debugElement.children[0].references['refs'];
|
2016-04-11 11:47:28 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
fixture.componentInstance.currentTplRef = refs.tplRefs.first;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('foo');
|
2016-04-11 11:47:28 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-04-11 11:47:28 -04:00
|
|
|
|
|
|
|
it('should clear content if TemplateRef becomes null',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template =
|
|
|
|
`<tpl-refs #refs="tplRefs"><template>foo</template></tpl-refs><template [ngTemplateOutlet]="currentTplRef"></template>`;
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
var refs = fixture.debugElement.children[0].references['refs'];
|
|
|
|
|
|
|
|
fixture.componentInstance.currentTplRef = refs.tplRefs.first;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('foo');
|
|
|
|
|
|
|
|
fixture.componentInstance.currentTplRef = null;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-04-11 11:47:28 -04:00
|
|
|
|
|
|
|
it('should swap content if TemplateRef changes',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template =
|
|
|
|
`<tpl-refs #refs="tplRefs"><template>foo</template><template>bar</template></tpl-refs><template [ngTemplateOutlet]="currentTplRef"></template>`;
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
var refs = fixture.debugElement.children[0].references['refs'];
|
|
|
|
|
|
|
|
fixture.componentInstance.currentTplRef = refs.tplRefs.first;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('foo');
|
|
|
|
|
|
|
|
fixture.componentInstance.currentTplRef = refs.tplRefs.last;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('bar');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-04-11 11:47:28 -04:00
|
|
|
|
2016-06-06 12:03:32 -04:00
|
|
|
it('should display template if context is null',
|
2016-06-10 13:35:36 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template =
|
|
|
|
`<tpl-refs #refs="tplRefs"><template>foo</template></tpl-refs><template [ngTemplateOutlet]="currentTplRef" [ngOutletContext]="null"></template>`;
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
var refs = fixture.debugElement.children[0].references['refs'];
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.componentInstance.currentTplRef = refs.tplRefs.first;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('foo');
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-06-06 12:03:32 -04:00
|
|
|
|
|
|
|
it('should reflect initial context and changes',
|
2016-06-10 13:35:36 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template =
|
|
|
|
`<tpl-refs #refs="tplRefs"><template let-foo="foo"><span>{{foo}}</span></template></tpl-refs><template [ngTemplateOutlet]="currentTplRef" [ngOutletContext]="context"></template>`;
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
var refs = fixture.debugElement.children[0].references['refs'];
|
|
|
|
fixture.componentInstance.currentTplRef = refs.tplRefs.first;
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('bar');
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.componentInstance.context.foo = 'alter-bar';
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('alter-bar');
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-06-06 12:03:32 -04:00
|
|
|
|
|
|
|
it('should reflect user defined $implicit property in the context',
|
2016-06-10 13:35:36 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template =
|
|
|
|
`<tpl-refs #refs="tplRefs"><template let-ctx><span>{{ctx.foo}}</span></template></tpl-refs><template [ngTemplateOutlet]="currentTplRef" [ngOutletContext]="context"></template>`;
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
var refs = fixture.debugElement.children[0].references['refs'];
|
|
|
|
fixture.componentInstance.currentTplRef = refs.tplRefs.first;
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.componentInstance.context = {
|
|
|
|
$implicit: fixture.componentInstance.context
|
|
|
|
};
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('bar');
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-06-06 12:03:32 -04:00
|
|
|
|
|
|
|
it('should reflect context re-binding',
|
2016-06-10 13:35:36 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template =
|
|
|
|
`<tpl-refs #refs="tplRefs"><template let-shawshank="shawshank"><span>{{shawshank}}</span></template></tpl-refs><template [ngTemplateOutlet]="currentTplRef" [ngOutletContext]="context"></template>`;
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
var refs = fixture.debugElement.children[0].references['refs'];
|
|
|
|
fixture.componentInstance.currentTplRef = refs.tplRefs.first;
|
|
|
|
fixture.componentInstance.context = {shawshank: 'brooks'};
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('brooks');
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.componentInstance.context = {shawshank: 'was here'};
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('was here');
|
2016-06-06 12:03:32 -04:00
|
|
|
|
2016-06-10 13:35:36 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-04-11 11:47:28 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Directive({selector: 'tpl-refs', exportAs: 'tplRefs'})
|
|
|
|
class CaptureTplRefs {
|
2016-04-28 17:00:31 -04:00
|
|
|
@ContentChildren(TemplateRef) tplRefs: QueryList<TemplateRef<any>>;
|
2016-04-11 11:47:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'test-cmp', directives: [NgTemplateOutlet, CaptureTplRefs], template: ''})
|
|
|
|
class TestComponent {
|
2016-04-28 17:00:31 -04:00
|
|
|
currentTplRef: TemplateRef<any>;
|
2016-06-10 13:35:36 -04:00
|
|
|
context: any = {foo: 'bar'};
|
2016-04-11 11:47:28 -04:00
|
|
|
}
|