Remove TCB (#10900)

* refactor(webworker): change tests not to use TestComponentBuilder

* refactor(core): change tests not to use TestComponentBuilder
This commit is contained in:
Victor Savkin 2016-08-17 15:05:22 -07:00 committed by Kara
parent 3009be8d6e
commit 4be863c223
2 changed files with 135 additions and 184 deletions

View File

@ -7,76 +7,66 @@
*/ */
import {Component, Directive, ElementRef, Input, QueryList, ViewChild, ViewChildren} from '@angular/core'; import {Component, Directive, ElementRef, Input, QueryList, ViewChild, ViewChildren} from '@angular/core';
import {ViewMetadata} from '@angular/core/src/metadata/view'; import {TestBed} from '@angular/core/testing';
import {AsyncTestCompleter, TestComponentBuilder, beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
export function main() { export function main() {
describe('ViewChild', () => { describe('ViewChild', () => {
it('should support type selector', beforeEach(() => {
inject( TestBed.configureTestingModule({
[TestComponentBuilder, AsyncTestCompleter], declarations: [ViewChildTypeSelectorComponent, ViewChildStringSelectorComponent, Simple]
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { });
tcb.overrideView( });
ViewChildTypeSelectorComponent, new ViewMetadata({
template: `<simple [marker]="'1'"></simple><simple [marker]="'2'"></simple>`, it('should support type selector', () => {
directives: [Simple] TestBed.overrideComponent(
})) ViewChildTypeSelectorComponent,
.createAsync(ViewChildTypeSelectorComponent) {set: {template: `<simple [marker]="'1'"></simple><simple [marker]="'2'"></simple>`}});
.then((view) => { const view = TestBed.createComponent(ViewChildTypeSelectorComponent);
view.detectChanges(); view.detectChanges();
expect(view.componentInstance.child).toBeDefined(); expect(view.componentInstance.child).toBeDefined();
expect(view.componentInstance.child.marker).toBe('1'); expect(view.componentInstance.child.marker).toBe('1');
async.done();
}); });
}));
it('should support string selector', it('should support string selector', () => {
inject( TestBed.overrideComponent(
[TestComponentBuilder, AsyncTestCompleter], ViewChildStringSelectorComponent, {set: {template: `<simple #child></simple>`}});
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { const view = TestBed.createComponent(ViewChildStringSelectorComponent);
tcb.overrideView(
ViewChildStringSelectorComponent,
new ViewMetadata({template: `<simple #child></simple>`, directives: [Simple]}))
.createAsync(ViewChildStringSelectorComponent)
.then((view) => {
view.detectChanges(); view.detectChanges();
expect(view.componentInstance.child).toBeDefined(); expect(view.componentInstance.child).toBeDefined();
async.done();
}); });
}));
}); });
describe('ViewChildren', () => { describe('ViewChildren', () => {
it('should support type selector', beforeEach(() => {
inject( TestBed.configureTestingModule({
[TestComponentBuilder, AsyncTestCompleter], declarations:
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { [ViewChildrenTypeSelectorComponent, ViewChildrenStringSelectorComponent, Simple]
tcb.overrideView( });
});
it('should support type selector', () => {
TestBed.overrideComponent(
ViewChildrenTypeSelectorComponent, ViewChildrenTypeSelectorComponent,
new ViewMetadata( {set: {template: `<simple></simple><simple></simple>`}});
{template: `<simple></simple><simple></simple>`, directives: [Simple]}))
.createAsync(ViewChildrenTypeSelectorComponent) const view = TestBed.createComponent(ViewChildrenTypeSelectorComponent);
.then((view) => {
view.detectChanges(); view.detectChanges();
expect(view.componentInstance.children).toBeDefined(); expect(view.componentInstance.children).toBeDefined();
expect(view.componentInstance.children.length).toBe(2); expect(view.componentInstance.children.length).toBe(2);
async.done();
}); });
}));
it('should support string selector', it('should support string selector', () => {
inject( TestBed.overrideComponent(
[TestComponentBuilder, AsyncTestCompleter], ViewChildrenStringSelectorComponent,
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { {set: {template: `<simple #child1></simple><simple #child2></simple>`}});
tcb.overrideView(ViewChildrenStringSelectorComponent, new ViewMetadata({ const view = TestBed.createComponent(ViewChildrenStringSelectorComponent);
template: `<simple #child1></simple><simple #child2></simple>`,
directives: [Simple]
}))
.createAsync(ViewChildrenStringSelectorComponent)
.then((view) => {
view.detectChanges(); view.detectChanges();
expect(view.componentInstance.children).toBeDefined(); expect(view.componentInstance.children).toBeDefined();
expect(view.componentInstance.children.length).toBe(2); expect(view.componentInstance.children.length).toBe(2);
async.done();
}); });
}));
}); });
} }
@ -86,22 +76,22 @@ class Simple {
@Input() marker: string; @Input() marker: string;
} }
@Component({selector: 'view-child-type-selector'}) @Component({selector: 'view-child-type-selector', template: ''})
class ViewChildTypeSelectorComponent { class ViewChildTypeSelectorComponent {
@ViewChild(Simple) child: Simple; @ViewChild(Simple) child: Simple;
} }
@Component({selector: 'view-child-string-selector'}) @Component({selector: 'view-child-string-selector', template: ''})
class ViewChildStringSelectorComponent { class ViewChildStringSelectorComponent {
@ViewChild('child') child: ElementRef; @ViewChild('child') child: ElementRef;
} }
@Component({selector: 'view-children-type-selector'}) @Component({selector: 'view-children-type-selector', template: ''})
class ViewChildrenTypeSelectorComponent { class ViewChildrenTypeSelectorComponent {
@ViewChildren(Simple) children: QueryList<Simple>; @ViewChildren(Simple) children: QueryList<Simple>;
} }
@Component({selector: 'view-child-string-selector'}) @Component({selector: 'view-child-string-selector', template: ''})
class ViewChildrenStringSelectorComponent { class ViewChildrenStringSelectorComponent {
// Allow comma separated selector (with spaces). // Allow comma separated selector (with spaces).
@ViewChildren('child1 , child2') children: QueryList<ElementRef>; @ViewChildren('child1 , child2') children: QueryList<ElementRef>;

View File

@ -12,7 +12,6 @@ import {DebugDomRootRenderer} from '@angular/core/src/debug/debug_renderer';
import {ViewMetadata} from '@angular/core/src/metadata/view'; import {ViewMetadata} from '@angular/core/src/metadata/view';
import {RootRenderer} from '@angular/core/src/render/api'; import {RootRenderer} from '@angular/core/src/render/api';
import {TestBed} from '@angular/core/testing'; import {TestBed} from '@angular/core/testing';
import {AsyncTestCompleter, TestComponentBuilder, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it} from '@angular/core/testing/testing_internal';
import {platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing'; import {platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {DomRootRenderer, DomRootRenderer_} from '@angular/platform-browser/src/dom/dom_renderer'; import {DomRootRenderer, DomRootRenderer_} from '@angular/platform-browser/src/dom/dom_renderer';
@ -81,6 +80,7 @@ export function main() {
workerRenderStore = new RenderStore(); workerRenderStore = new RenderStore();
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [MyComp2],
providers: [ providers: [
Serializer, {provide: RenderStore, useValue: workerRenderStore}, { Serializer, {provide: RenderStore, useValue: workerRenderStore}, {
provide: RootRenderer, provide: RootRenderer,
@ -104,33 +104,24 @@ export function main() {
return (<any>componentRef.hostView).internalView.renderer; return (<any>componentRef.hostView).internalView.renderer;
} }
it('should update text nodes', it('should update text nodes', () => {
inject( TestBed.overrideComponent(MyComp2, {set: {template: '<div>{{ctxProp}}</div>'}});
[TestComponentBuilder, AsyncTestCompleter], const fixture = TestBed.createComponent(MyComp2);
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(MyComp2, new ViewMetadata({template: '<div>{{ctxProp}}</div>'}))
.createAsync(MyComp2)
.then((fixture) => {
var renderEl = getRenderElement(fixture.debugElement.nativeElement); var renderEl = getRenderElement(fixture.debugElement.nativeElement);
expect(renderEl).toHaveText(''); expect(renderEl).toHaveText('');
fixture.debugElement.componentInstance.ctxProp = 'Hello World!'; fixture.debugElement.componentInstance.ctxProp = 'Hello World!';
fixture.detectChanges(); fixture.detectChanges();
expect(renderEl).toHaveText('Hello World!'); expect(renderEl).toHaveText('Hello World!');
async.done();
}); });
}));
it('should update any element property/attributes/class/style(s) independent of the compilation on the root element and other elements', it('should update any element property/attributes/class/style(s) independent of the compilation on the root element and other elements',
inject( () => {
[TestComponentBuilder, AsyncTestCompleter], TestBed.overrideComponent(
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { MyComp2, {set: {template: '<input [title]="y" style="position:absolute">'}});
tcb.overrideView( const fixture = TestBed.createComponent(MyComp2);
MyComp2,
new ViewMetadata({template: '<input [title]="y" style="position:absolute">'}))
.createAsync(MyComp2)
.then((fixture) => {
var checkSetters = var checkSetters =
(componentRef: any /** TODO #9100 */, workerEl: any /** TODO #9100 */) => { (componentRef: any /** TODO #9100 */, workerEl: any /** TODO #9100 */) => {
var renderer = getRenderer(componentRef); var renderer = getRenderer(componentRef);
@ -155,40 +146,25 @@ export function main() {
// root element // root element
checkSetters(fixture.componentRef, fixture.debugElement.nativeElement); checkSetters(fixture.componentRef, fixture.debugElement.nativeElement);
// nested elements // nested elements
checkSetters( checkSetters(fixture.componentRef, fixture.debugElement.children[0].nativeElement);
fixture.componentRef, fixture.debugElement.children[0].nativeElement);
async.done();
}); });
}));
it('should update any template comment property/attributes', it('should update any template comment property/attributes', () => {
inject(
[TestComponentBuilder, AsyncTestCompleter], TestBed.overrideComponent(
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { MyComp2, {set: {template: '<template [ngIf]="ctxBoolProp"></template>'}});
var tpl = '<template [ngIf]="ctxBoolProp"></template>'; const fixture = TestBed.createComponent(MyComp2);
tcb.overrideView(MyComp2, new ViewMetadata({template: tpl, directives: [NgIf]}))
.createAsync(MyComp2)
.then((fixture) => {
(<MyComp2>fixture.debugElement.componentInstance).ctxBoolProp = true; (<MyComp2>fixture.debugElement.componentInstance).ctxBoolProp = true;
fixture.detectChanges(); fixture.detectChanges();
var el = getRenderElement(fixture.debugElement.nativeElement); var el = getRenderElement(fixture.debugElement.nativeElement);
expect(getDOM().getInnerHTML(el)).toContain('"ng-reflect-ng-if": "true"'); expect(getDOM().getInnerHTML(el)).toContain('"ng-reflect-ng-if": "true"');
async.done();
}); });
}));
it('should add and remove fragments', it('should add and remove fragments', () => {
inject( TestBed.overrideComponent(
[TestComponentBuilder, AsyncTestCompleter], MyComp2, {set: {template: '<template [ngIf]="ctxBoolProp">hello</template>'}});
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => { const fixture = TestBed.createComponent(MyComp2);
tcb.overrideView(MyComp2, new ViewMetadata({
template: '<template [ngIf]="ctxBoolProp">hello</template>',
directives: [NgIf]
}))
.createAsync(MyComp2)
.then((fixture) => {
var rootEl = getRenderElement(fixture.debugElement.nativeElement); var rootEl = getRenderElement(fixture.debugElement.nativeElement);
expect(rootEl).toHaveText(''); expect(rootEl).toHaveText('');
@ -200,46 +176,31 @@ export function main() {
fixture.debugElement.componentInstance.ctxBoolProp = false; fixture.debugElement.componentInstance.ctxBoolProp = false;
fixture.detectChanges(); fixture.detectChanges();
expect(rootEl).toHaveText(''); expect(rootEl).toHaveText('');
async.done();
}); });
}));
if (getDOM().supportsDOMEvents()) { if (getDOM().supportsDOMEvents()) {
it('should call actions on the element', it('should call actions on the element', () => {
inject( TestBed.overrideComponent(MyComp2, {set: {template: '<input [title]="y">'}});
[TestComponentBuilder, AsyncTestCompleter], const fixture = TestBed.createComponent(MyComp2);
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(MyComp2, new ViewMetadata({template: '<input [title]="y">'}))
.createAsync(MyComp2)
.then((fixture) => {
var el = fixture.debugElement.children[0]; var el = fixture.debugElement.children[0];
getRenderer(fixture.componentRef) getRenderer(fixture.componentRef).invokeElementMethod(el.nativeElement, 'setAttribute', [
.invokeElementMethod(el.nativeElement, 'setAttribute', ['a', 'b']); 'a', 'b'
]);
expect(getDOM().getAttribute(getRenderElement(el.nativeElement), 'a')).toEqual('b');
expect(getDOM().getAttribute(getRenderElement(el.nativeElement), 'a'))
.toEqual('b');
async.done();
}); });
}));
it('should listen to events', it('should listen to events', () => {
inject( TestBed.overrideComponent(MyComp2, {set: {template: '<input (change)="ctxNumProp = 1">'}});
[TestComponentBuilder, AsyncTestCompleter], const fixture = TestBed.createComponent(MyComp2);
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(
MyComp2, new ViewMetadata({template: '<input (change)="ctxNumProp = 1">'}))
.createAsync(MyComp2)
.then((fixture) => {
var el = fixture.debugElement.children[0]; var el = fixture.debugElement.children[0];
dispatchEvent(getRenderElement(el.nativeElement), 'change'); dispatchEvent(getRenderElement(el.nativeElement), 'change');
expect(fixture.componentInstance.ctxNumProp).toBe(1); expect(fixture.componentInstance.ctxNumProp).toBe(1);
fixture.destroy(); fixture.destroy();
async.done();
}); });
}));
} }
}); });
} }