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 {ViewMetadata} from '@angular/core/src/metadata/view';
import {AsyncTestCompleter, TestComponentBuilder, beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
import {TestBed} from '@angular/core/testing';
export function main() {
describe('ViewChild', () => {
it('should support type selector',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(
ViewChildTypeSelectorComponent, new ViewMetadata({
template: `<simple [marker]="'1'"></simple><simple [marker]="'2'"></simple>`,
directives: [Simple]
}))
.createAsync(ViewChildTypeSelectorComponent)
.then((view) => {
view.detectChanges();
expect(view.componentInstance.child).toBeDefined();
expect(view.componentInstance.child.marker).toBe('1');
async.done();
});
}));
it('should support string selector',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(
ViewChildStringSelectorComponent,
new ViewMetadata({template: `<simple #child></simple>`, directives: [Simple]}))
.createAsync(ViewChildStringSelectorComponent)
.then((view) => {
view.detectChanges();
expect(view.componentInstance.child).toBeDefined();
async.done();
});
}));
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ViewChildTypeSelectorComponent, ViewChildStringSelectorComponent, Simple]
});
});
it('should support type selector', () => {
TestBed.overrideComponent(
ViewChildTypeSelectorComponent,
{set: {template: `<simple [marker]="'1'"></simple><simple [marker]="'2'"></simple>`}});
const view = TestBed.createComponent(ViewChildTypeSelectorComponent);
view.detectChanges();
expect(view.componentInstance.child).toBeDefined();
expect(view.componentInstance.child.marker).toBe('1');
});
it('should support string selector', () => {
TestBed.overrideComponent(
ViewChildStringSelectorComponent, {set: {template: `<simple #child></simple>`}});
const view = TestBed.createComponent(ViewChildStringSelectorComponent);
view.detectChanges();
expect(view.componentInstance.child).toBeDefined();
});
});
describe('ViewChildren', () => {
it('should support type selector',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(
ViewChildrenTypeSelectorComponent,
new ViewMetadata(
{template: `<simple></simple><simple></simple>`, directives: [Simple]}))
.createAsync(ViewChildrenTypeSelectorComponent)
.then((view) => {
view.detectChanges();
expect(view.componentInstance.children).toBeDefined();
expect(view.componentInstance.children.length).toBe(2);
async.done();
});
}));
it('should support string selector',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(ViewChildrenStringSelectorComponent, new ViewMetadata({
template: `<simple #child1></simple><simple #child2></simple>`,
directives: [Simple]
}))
.createAsync(ViewChildrenStringSelectorComponent)
.then((view) => {
view.detectChanges();
expect(view.componentInstance.children).toBeDefined();
expect(view.componentInstance.children.length).toBe(2);
async.done();
});
}));
beforeEach(() => {
TestBed.configureTestingModule({
declarations:
[ViewChildrenTypeSelectorComponent, ViewChildrenStringSelectorComponent, Simple]
});
});
it('should support type selector', () => {
TestBed.overrideComponent(
ViewChildrenTypeSelectorComponent,
{set: {template: `<simple></simple><simple></simple>`}});
const view = TestBed.createComponent(ViewChildrenTypeSelectorComponent);
view.detectChanges();
expect(view.componentInstance.children).toBeDefined();
expect(view.componentInstance.children.length).toBe(2);
});
it('should support string selector', () => {
TestBed.overrideComponent(
ViewChildrenStringSelectorComponent,
{set: {template: `<simple #child1></simple><simple #child2></simple>`}});
const view = TestBed.createComponent(ViewChildrenStringSelectorComponent);
view.detectChanges();
expect(view.componentInstance.children).toBeDefined();
expect(view.componentInstance.children.length).toBe(2);
});
});
}
@ -86,22 +76,22 @@ class Simple {
@Input() marker: string;
}
@Component({selector: 'view-child-type-selector'})
@Component({selector: 'view-child-type-selector', template: ''})
class ViewChildTypeSelectorComponent {
@ViewChild(Simple) child: Simple;
}
@Component({selector: 'view-child-string-selector'})
@Component({selector: 'view-child-string-selector', template: ''})
class ViewChildStringSelectorComponent {
@ViewChild('child') child: ElementRef;
}
@Component({selector: 'view-children-type-selector'})
@Component({selector: 'view-children-type-selector', template: ''})
class ViewChildrenTypeSelectorComponent {
@ViewChildren(Simple) children: QueryList<Simple>;
}
@Component({selector: 'view-child-string-selector'})
@Component({selector: 'view-child-string-selector', template: ''})
class ViewChildrenStringSelectorComponent {
// Allow comma separated selector (with spaces).
@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 {RootRenderer} from '@angular/core/src/render/api';
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 {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {DomRootRenderer, DomRootRenderer_} from '@angular/platform-browser/src/dom/dom_renderer';
@ -81,6 +80,7 @@ export function main() {
workerRenderStore = new RenderStore();
TestBed.configureTestingModule({
declarations: [MyComp2],
providers: [
Serializer, {provide: RenderStore, useValue: workerRenderStore}, {
provide: RootRenderer,
@ -104,142 +104,103 @@ export function main() {
return (<any>componentRef.hostView).internalView.renderer;
}
it('should update text nodes',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(MyComp2, new ViewMetadata({template: '<div>{{ctxProp}}</div>'}))
.createAsync(MyComp2)
.then((fixture) => {
var renderEl = getRenderElement(fixture.debugElement.nativeElement);
expect(renderEl).toHaveText('');
it('should update text nodes', () => {
TestBed.overrideComponent(MyComp2, {set: {template: '<div>{{ctxProp}}</div>'}});
const fixture = TestBed.createComponent(MyComp2);
fixture.debugElement.componentInstance.ctxProp = 'Hello World!';
fixture.detectChanges();
expect(renderEl).toHaveText('Hello World!');
async.done();
var renderEl = getRenderElement(fixture.debugElement.nativeElement);
expect(renderEl).toHaveText('');
});
}));
fixture.debugElement.componentInstance.ctxProp = 'Hello World!';
fixture.detectChanges();
expect(renderEl).toHaveText('Hello World!');
});
it('should update any element property/attributes/class/style(s) independent of the compilation on the root element and other elements',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(
MyComp2,
new ViewMetadata({template: '<input [title]="y" style="position:absolute">'}))
.createAsync(MyComp2)
.then((fixture) => {
var checkSetters =
(componentRef: any /** TODO #9100 */, workerEl: any /** TODO #9100 */) => {
var renderer = getRenderer(componentRef);
var el = getRenderElement(workerEl);
renderer.setElementProperty(workerEl, 'tabIndex', 1);
expect((<HTMLInputElement>el).tabIndex).toEqual(1);
() => {
TestBed.overrideComponent(
MyComp2, {set: {template: '<input [title]="y" style="position:absolute">'}});
const fixture = TestBed.createComponent(MyComp2);
renderer.setElementClass(workerEl, 'a', true);
expect(getDOM().hasClass(el, 'a')).toBe(true);
renderer.setElementClass(workerEl, 'a', false);
expect(getDOM().hasClass(el, 'a')).toBe(false);
var checkSetters =
(componentRef: any /** TODO #9100 */, workerEl: any /** TODO #9100 */) => {
var renderer = getRenderer(componentRef);
var el = getRenderElement(workerEl);
renderer.setElementProperty(workerEl, 'tabIndex', 1);
expect((<HTMLInputElement>el).tabIndex).toEqual(1);
renderer.setElementStyle(workerEl, 'width', '10px');
expect(getDOM().getStyle(el, 'width')).toEqual('10px');
renderer.setElementStyle(workerEl, 'width', null);
expect(getDOM().getStyle(el, 'width')).toEqual('');
renderer.setElementClass(workerEl, 'a', true);
expect(getDOM().hasClass(el, 'a')).toBe(true);
renderer.setElementClass(workerEl, 'a', false);
expect(getDOM().hasClass(el, 'a')).toBe(false);
renderer.setElementAttribute(workerEl, 'someattr', 'someValue');
expect(getDOM().getAttribute(el, 'someattr')).toEqual('someValue');
};
renderer.setElementStyle(workerEl, 'width', '10px');
expect(getDOM().getStyle(el, 'width')).toEqual('10px');
renderer.setElementStyle(workerEl, 'width', null);
expect(getDOM().getStyle(el, 'width')).toEqual('');
// root element
checkSetters(fixture.componentRef, fixture.debugElement.nativeElement);
// nested elements
checkSetters(
fixture.componentRef, fixture.debugElement.children[0].nativeElement);
renderer.setElementAttribute(workerEl, 'someattr', 'someValue');
expect(getDOM().getAttribute(el, 'someattr')).toEqual('someValue');
};
async.done();
});
}));
// root element
checkSetters(fixture.componentRef, fixture.debugElement.nativeElement);
// nested elements
checkSetters(fixture.componentRef, fixture.debugElement.children[0].nativeElement);
});
it('should update any template comment property/attributes',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
var tpl = '<template [ngIf]="ctxBoolProp"></template>';
tcb.overrideView(MyComp2, new ViewMetadata({template: tpl, directives: [NgIf]}))
it('should update any template comment property/attributes', () => {
.createAsync(MyComp2)
.then((fixture) => {
(<MyComp2>fixture.debugElement.componentInstance).ctxBoolProp = true;
fixture.detectChanges();
var el = getRenderElement(fixture.debugElement.nativeElement);
expect(getDOM().getInnerHTML(el)).toContain('"ng-reflect-ng-if": "true"');
async.done();
});
}));
TestBed.overrideComponent(
MyComp2, {set: {template: '<template [ngIf]="ctxBoolProp"></template>'}});
const fixture = TestBed.createComponent(MyComp2);
it('should add and remove fragments',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(MyComp2, new ViewMetadata({
template: '<template [ngIf]="ctxBoolProp">hello</template>',
directives: [NgIf]
}))
.createAsync(MyComp2)
.then((fixture) => {
(<MyComp2>fixture.debugElement.componentInstance).ctxBoolProp = true;
fixture.detectChanges();
var el = getRenderElement(fixture.debugElement.nativeElement);
expect(getDOM().getInnerHTML(el)).toContain('"ng-reflect-ng-if": "true"');
});
var rootEl = getRenderElement(fixture.debugElement.nativeElement);
expect(rootEl).toHaveText('');
it('should add and remove fragments', () => {
TestBed.overrideComponent(
MyComp2, {set: {template: '<template [ngIf]="ctxBoolProp">hello</template>'}});
const fixture = TestBed.createComponent(MyComp2);
fixture.debugElement.componentInstance.ctxBoolProp = true;
fixture.detectChanges();
expect(rootEl).toHaveText('hello');
var rootEl = getRenderElement(fixture.debugElement.nativeElement);
expect(rootEl).toHaveText('');
fixture.debugElement.componentInstance.ctxBoolProp = false;
fixture.detectChanges();
expect(rootEl).toHaveText('');
fixture.debugElement.componentInstance.ctxBoolProp = true;
fixture.detectChanges();
expect(rootEl).toHaveText('hello');
async.done();
});
}));
fixture.debugElement.componentInstance.ctxBoolProp = false;
fixture.detectChanges();
expect(rootEl).toHaveText('');
});
if (getDOM().supportsDOMEvents()) {
it('should call actions on the element',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(MyComp2, new ViewMetadata({template: '<input [title]="y">'}))
.createAsync(MyComp2)
.then((fixture) => {
var el = fixture.debugElement.children[0];
getRenderer(fixture.componentRef)
.invokeElementMethod(el.nativeElement, 'setAttribute', ['a', 'b']);
it('should call actions on the element', () => {
TestBed.overrideComponent(MyComp2, {set: {template: '<input [title]="y">'}});
const fixture = TestBed.createComponent(MyComp2);
var el = fixture.debugElement.children[0];
getRenderer(fixture.componentRef).invokeElementMethod(el.nativeElement, 'setAttribute', [
'a', 'b'
]);
expect(getDOM().getAttribute(getRenderElement(el.nativeElement), 'a'))
.toEqual('b');
async.done();
});
}));
expect(getDOM().getAttribute(getRenderElement(el.nativeElement), 'a')).toEqual('b');
it('should listen to events',
inject(
[TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
tcb.overrideView(
MyComp2, new ViewMetadata({template: '<input (change)="ctxNumProp = 1">'}))
.createAsync(MyComp2)
.then((fixture) => {
var el = fixture.debugElement.children[0];
dispatchEvent(getRenderElement(el.nativeElement), 'change');
expect(fixture.componentInstance.ctxNumProp).toBe(1);
});
fixture.destroy();
it('should listen to events', () => {
TestBed.overrideComponent(MyComp2, {set: {template: '<input (change)="ctxNumProp = 1">'}});
const fixture = TestBed.createComponent(MyComp2);
async.done();
});
}));
var el = fixture.debugElement.children[0];
dispatchEvent(getRenderElement(el.nativeElement), 'change');
expect(fixture.componentInstance.ctxNumProp).toBe(1);
fixture.destroy();
});
}
});
}