refactor(core): Removed linker test reference to TestComponentBuilder (#10867)

Removed TestComponentBuilder references from ng_container_integration_spect.ts
This commit is contained in:
Chuck Jazdzewski 2016-08-17 15:45:29 -07:00 committed by Kara
parent 00e157dc3b
commit 6f18bd18bb
1 changed files with 102 additions and 147 deletions

View File

@ -8,8 +8,8 @@
import {NgIf} from '@angular/common'; import {NgIf} from '@angular/common';
import {AfterContentInit, AfterViewInit, Component, ContentChildren, Directive, Input, QueryList, ViewChildren} from '@angular/core'; import {AfterContentInit, AfterViewInit, Component, ContentChildren, Directive, Input, QueryList, ViewChildren} from '@angular/core';
import {TestBed} from '@angular/core/testing'; import {TestBed, async} from '@angular/core/testing';
import {AsyncTestCompleter, TestComponentBuilder, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; import {AsyncTestCompleter, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {expect} from '@angular/platform-browser/testing/matchers'; import {expect} from '@angular/platform-browser/testing/matchers';
@ -21,170 +21,130 @@ export function main() {
function declareTests({useJit}: {useJit: boolean}) { function declareTests({useJit}: {useJit: boolean}) {
describe('<ng-container>', function() { describe('<ng-container>', function() {
beforeEach(() => { TestBed.configureCompiler({useJit: useJit}); }); beforeEach(() => {
TestBed.configureCompiler({useJit: useJit});
TestBed.configureTestingModule(
{declarations: [MyComp, NeedsContentChildren, NeedsViewChildren, TextDirective, Simple]});
});
it('should support the "i18n" attribute', it('should support the "i18n" attribute', () => {
inject( const template = '<ng-container i18n>foo</ng-container>';
[TestComponentBuilder, AsyncTestCompleter], TestBed.overrideComponent(MyComp, {set: {template}});
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { const fixture = TestBed.createComponent(MyComp);
tcb.overrideTemplate(MyComp, '<ng-container i18n>foo</ng-container>')
.createAsync(MyComp)
.then((fixture) => {
fixture.detectChanges();
const el = fixture.debugElement.nativeElement; fixture.detectChanges();
expect(el).toHaveText('foo');
async.done(); const el = fixture.debugElement.nativeElement;
}); expect(el).toHaveText('foo');
})); });
it('should be rendered as comment with children as siblings', it('should be rendered as comment with children as siblings', () => {
inject( const template = '<ng-container><p></p></ng-container>';
[TestComponentBuilder, AsyncTestCompleter], TestBed.overrideComponent(MyComp, {set: {template}});
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { const fixture = TestBed.createComponent(MyComp);
tcb.overrideTemplate(MyComp, '<ng-container><p></p></ng-container>')
.createAsync(MyComp)
.then((fixture) => {
fixture.detectChanges();
const el = fixture.debugElement.nativeElement; fixture.detectChanges();
const children = getDOM().childNodes(el);
expect(children.length).toBe(2);
expect(getDOM().isCommentNode(children[0])).toBe(true);
expect(getDOM().tagName(children[1]).toUpperCase()).toEqual('P');
async.done(); const el = fixture.debugElement.nativeElement;
}); const children = getDOM().childNodes(el);
})); expect(children.length).toBe(2);
expect(getDOM().isCommentNode(children[0])).toBe(true);
expect(getDOM().tagName(children[1]).toUpperCase()).toEqual('P');
});
it('should support nesting', it('should support nesting', () => {
inject( const template =
[TestComponentBuilder, AsyncTestCompleter], '<ng-container>1</ng-container><ng-container><ng-container>2</ng-container></ng-container>';
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { TestBed.overrideComponent(MyComp, {set: {template}});
tcb.overrideTemplate( const fixture = TestBed.createComponent(MyComp);
MyComp,
'<ng-container>1</ng-container><ng-container><ng-container>2</ng-container></ng-container>')
.createAsync(MyComp)
.then((fixture) => {
fixture.detectChanges();
const el = fixture.debugElement.nativeElement; fixture.detectChanges();
const children = getDOM().childNodes(el);
expect(children.length).toBe(5);
expect(getDOM().isCommentNode(children[0])).toBe(true);
expect(children[1]).toHaveText('1');
expect(getDOM().isCommentNode(children[2])).toBe(true);
expect(getDOM().isCommentNode(children[3])).toBe(true);
expect(children[4]).toHaveText('2');
async.done(); const el = fixture.debugElement.nativeElement;
}); const children = getDOM().childNodes(el);
})); expect(children.length).toBe(5);
expect(getDOM().isCommentNode(children[0])).toBe(true);
expect(children[1]).toHaveText('1');
expect(getDOM().isCommentNode(children[2])).toBe(true);
expect(getDOM().isCommentNode(children[3])).toBe(true);
expect(children[4]).toHaveText('2');
});
it('should group inner nodes', it('should group inner nodes', () => {
inject( const template = '<ng-container *ngIf="ctxBoolProp"><p></p><b></b></ng-container>';
[TestComponentBuilder, AsyncTestCompleter], TestBed.overrideComponent(MyComp, {set: {template}});
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { const fixture = TestBed.createComponent(MyComp);
tcb.overrideTemplate(
MyComp, '<ng-container *ngIf="ctxBoolProp"><p></p><b></b></ng-container>')
.createAsync(MyComp)
.then((fixture) => {
fixture.debugElement.componentInstance.ctxBoolProp = true;
fixture.detectChanges();
const el = fixture.debugElement.nativeElement; fixture.debugElement.componentInstance.ctxBoolProp = true;
const children = getDOM().childNodes(el); fixture.detectChanges();
expect(children.length).toBe(4); const el = fixture.debugElement.nativeElement;
// ngIf anchor const children = getDOM().childNodes(el);
expect(getDOM().isCommentNode(children[0])).toBe(true);
// ng-container anchor
expect(getDOM().isCommentNode(children[1])).toBe(true);
expect(getDOM().tagName(children[2]).toUpperCase()).toEqual('P');
expect(getDOM().tagName(children[3]).toUpperCase()).toEqual('B');
fixture.debugElement.componentInstance.ctxBoolProp = false; expect(children.length).toBe(4);
fixture.detectChanges(); // ngIf anchor
expect(getDOM().isCommentNode(children[0])).toBe(true);
// ng-container anchor
expect(getDOM().isCommentNode(children[1])).toBe(true);
expect(getDOM().tagName(children[2]).toUpperCase()).toEqual('P');
expect(getDOM().tagName(children[3]).toUpperCase()).toEqual('B');
expect(children.length).toBe(1); fixture.debugElement.componentInstance.ctxBoolProp = false;
expect(getDOM().isCommentNode(children[0])).toBe(true); fixture.detectChanges();
async.done(); expect(children.length).toBe(1);
}); expect(getDOM().isCommentNode(children[0])).toBe(true);
})); });
it('should work with static content projection', it('should work with static content projection', () => {
inject( const template = `<simple><ng-container><p>1</p><p>2</p></ng-container></simple>`;
[TestComponentBuilder, AsyncTestCompleter], TestBed.overrideComponent(MyComp, {set: {template}});
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { const fixture = TestBed.createComponent(MyComp);
tcb.overrideTemplate(
MyComp, `<simple><ng-container><p>1</p><p>2</p></ng-container></simple>`)
.createAsync(MyComp)
.then((fixture) => {
fixture.detectChanges();
const el = fixture.debugElement.nativeElement; fixture.detectChanges();
expect(el).toHaveText('SIMPLE(12)');
async.done(); const el = fixture.debugElement.nativeElement;
}); expect(el).toHaveText('SIMPLE(12)');
})); });
it('should support injecting the container from children', it('should support injecting the container from children', () => {
inject( const template = `<ng-container [text]="'container'"><p></p></ng-container>`;
[TestComponentBuilder, AsyncTestCompleter], TestBed.overrideComponent(MyComp, {set: {template}});
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { const fixture = TestBed.createComponent(MyComp);
tcb.overrideTemplate(
MyComp, `<ng-container [text]="'container'"><p></p></ng-container>`)
.createAsync(MyComp)
.then((fixture) => {
fixture.detectChanges();
const dir = fixture.debugElement.children[0].injector.get(TextDirective); fixture.detectChanges();
expect(dir).toBeAnInstanceOf(TextDirective);
expect(dir.text).toEqual('container');
async.done(); const dir = fixture.debugElement.children[0].injector.get(TextDirective);
}); expect(dir).toBeAnInstanceOf(TextDirective);
})); expect(dir.text).toEqual('container');
});
it('should contain all direct child directives in a <ng-container> (content dom)', it('should contain all direct child directives in a <ng-container> (content dom)', () => {
inject( const template =
[TestComponentBuilder, AsyncTestCompleter], '<needs-content-children #q><ng-container><div text="foo"></div></ng-container></needs-content-children>';
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { TestBed.overrideComponent(MyComp, {set: {template}});
const template = const fixture = TestBed.createComponent(MyComp);
'<needs-content-children #q><ng-container><div text="foo"></div></ng-container></needs-content-children>';
tcb.overrideTemplate(MyComp, template).createAsync(MyComp).then((view) => { fixture.detectChanges();
view.detectChanges(); var q = fixture.debugElement.children[0].references['q'];
var q = view.debugElement.children[0].references['q']; fixture.detectChanges();
view.detectChanges();
expect(q.textDirChildren.length).toEqual(1); expect(q.textDirChildren.length).toEqual(1);
expect(q.numberOfChildrenAfterContentInit).toEqual(1); expect(q.numberOfChildrenAfterContentInit).toEqual(1);
});
async.done(); it('should contain all child directives in a <ng-container> (view dom)', () => {
}); const template = '<needs-view-children #q></needs-view-children>';
})); TestBed.overrideComponent(MyComp, {set: {template}});
const fixture = TestBed.createComponent(MyComp);
it('should contain all child directives in a <ng-container> (view dom)', fixture.detectChanges();
inject( var q = fixture.debugElement.children[0].references['q'];
[TestComponentBuilder, AsyncTestCompleter], fixture.detectChanges();
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
const template = '<needs-view-children #q></needs-view-children>';
tcb.overrideTemplate(MyComp, template).createAsync(MyComp).then((view) => { expect(q.textDirChildren.length).toEqual(1);
view.detectChanges(); expect(q.numberOfChildrenAfterViewInit).toEqual(1);
var q = view.debugElement.children[0].references['q']; });
view.detectChanges();
expect(q.textDirChildren.length).toEqual(1);
expect(q.numberOfChildrenAfterViewInit).toEqual(1);
async.done();
});
}));
}); });
} }
@ -201,8 +161,7 @@ class NeedsContentChildren implements AfterContentInit {
ngAfterContentInit() { this.numberOfChildrenAfterContentInit = this.textDirChildren.length; } ngAfterContentInit() { this.numberOfChildrenAfterContentInit = this.textDirChildren.length; }
} }
@Component( @Component({selector: 'needs-view-children', template: '<div text></div>'})
{selector: 'needs-view-children', template: '<div text></div>', directives: [TextDirective]})
class NeedsViewChildren implements AfterViewInit { class NeedsViewChildren implements AfterViewInit {
@ViewChildren(TextDirective) textDirChildren: QueryList<TextDirective>; @ViewChildren(TextDirective) textDirChildren: QueryList<TextDirective>;
numberOfChildrenAfterViewInit: number; numberOfChildrenAfterViewInit: number;
@ -210,15 +169,11 @@ class NeedsViewChildren implements AfterViewInit {
ngAfterViewInit() { this.numberOfChildrenAfterViewInit = this.textDirChildren.length; } ngAfterViewInit() { this.numberOfChildrenAfterViewInit = this.textDirChildren.length; }
} }
@Component({selector: 'simple', template: 'SIMPLE(<ng-content></ng-content>)', directives: []}) @Component({selector: 'simple', template: 'SIMPLE(<ng-content></ng-content>)'})
class Simple { class Simple {
} }
@Component({ @Component({selector: 'my-comp', template: ''})
selector: 'my-comp',
directives: [NeedsContentChildren, NeedsViewChildren, TextDirective, NgIf, Simple],
template: ''
})
class MyComp { class MyComp {
ctxBoolProp: boolean = false; ctxBoolProp: boolean = false;
} }