refactor(core): update change_detection_integration_spec not to use TestComponentBuilder

This commit is contained in:
vsavkin 2016-08-16 20:20:09 -07:00 committed by Vikram Subramanian
parent 75405ae0f5
commit cc22b051e3
1 changed files with 66 additions and 72 deletions

View File

@ -10,7 +10,7 @@ import {AsyncPipe, NgFor} from '@angular/common';
import {ElementSchemaRegistry} from '@angular/compiler/src/schema/element_schema_registry';
import {TEST_COMPILER_PROVIDERS} from '@angular/compiler/test/test_bindings';
import {MockSchemaRegistry} from '@angular/compiler/testing';
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, DebugElement, Directive, DoCheck, Injectable, Input, OnChanges, OnDestroy, OnInit, Output, Pipe, PipeTransform, RenderComponentType, Renderer, RootRenderer, SimpleChange, SimpleChanges, TemplateRef, Type, ViewContainerRef, WrappedValue, forwardRef} from '@angular/core';
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentMetadata, DebugElement, Directive, DoCheck, Injectable, Input, OnChanges, OnDestroy, OnInit, Output, Pipe, PipeTransform, RenderComponentType, Renderer, RootRenderer, SimpleChange, SimpleChanges, TemplateRef, Type, ViewContainerRef, WrappedValue, forwardRef} from '@angular/core';
import {DebugDomRenderer} from '@angular/core/src/debug/debug_renderer';
import {ViewMetadata} from '@angular/core/src/metadata/view';
import {ComponentFixture, TestBed, fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';
@ -25,7 +25,6 @@ import {BaseException} from '../../src/facade/exceptions';
import {NumberWrapper, isBlank} from '../../src/facade/lang';
export function main() {
let tcb: TestComponentBuilder;
let elSchema: MockSchemaRegistry;
let renderLog: RenderLog;
let directiveLog: DirectiveLog;
@ -33,18 +32,19 @@ export function main() {
function createCompFixture<T>(template: string): ComponentFixture<TestComponent>;
function createCompFixture<T>(template: string, compType: Type<T>): ComponentFixture<T>;
function createCompFixture<T>(
template: string, compType: Type<T>, _tcb: TestComponentBuilder): ComponentFixture<T>;
function createCompFixture<T>(
template: string, compType: Type<T> = <any>TestComponent,
_tcb: TestComponentBuilder = null): ComponentFixture<T> {
if (isBlank(_tcb)) {
_tcb = tcb;
template: string, compType: Type<T> = <any>TestComponent): ComponentFixture<T> {
TestBed.overrideComponent(compType, {set: new ComponentMetadata({template})});
initHelpers();
return TestBed.createComponent(compType);
}
return _tcb
.overrideView(
compType,
new ViewMetadata({template: template, directives: ALL_DIRECTIVES, pipes: ALL_PIPES}))
.createFakeAsync(compType);
function initHelpers(): void {
elSchema = TestBed.get(ElementSchemaRegistry);
renderLog = TestBed.get(RenderLog);
directiveLog = TestBed.get(DirectiveLog);
elSchema.existingProperties['someProp'] = true;
}
function queryDirs(el: DebugElement, dirType: Type<any>): any {
@ -81,24 +81,36 @@ export function main() {
beforeEach(() => {
TestBed.configureCompiler({providers: TEST_COMPILER_PROVIDERS});
TestBed.configureTestingModule({
declarations: [
TestData,
TestDirective,
TestComponent,
AnotherComponent,
TestLocals,
CompWithRef,
EmitterDirective,
PushComp,
OrderCheckDirective2,
OrderCheckDirective0,
OrderCheckDirective1,
Gh9882,
Uninitialized,
Person,
PersonHolder,
PersonHolderHolder,
CountingPipe,
CountingImpurePipe,
MultiArgPipe,
PipeWithOnDestroy,
IdentityPipe,
WrappedPipe,
],
providers:
[RenderLog, DirectiveLog, {provide: RootRenderer, useClass: LoggingRootRenderer}]
});
});
beforeEach(fakeAsync(inject(
[TestComponentBuilder, ElementSchemaRegistry, RenderLog, DirectiveLog],
(_tcb: TestComponentBuilder, _elSchema: MockSchemaRegistry, _renderLog: RenderLog,
_directiveLog: DirectiveLog) => {
tcb = _tcb;
elSchema = _elSchema;
renderLog = _renderLog;
directiveLog = _directiveLog;
elSchema.existingProperties['someProp'] = true;
})));
describe('expressions', () => {
it('should support literals',
fakeAsync(() => { expect(_bindAndCheckSimpleValue(10)).toEqual(['someProp=10']); }));
@ -646,10 +658,16 @@ export function main() {
describe('lifecycle', () => {
function createCompWithContentAndViewChild(): ComponentFixture<any> {
TestBed.overrideComponent(AnotherComponent, {
set: new ComponentMetadata({
selector: 'other-cmp',
template: '<div testDirective="viewChild"></div>',
})
});
return createCompFixture(
'<div testDirective="parent"><div *ngIf="true" testDirective="contentChild"></div><other-cmp></other-cmp></div>',
TestComponent,
tcb.overrideTemplate(AnotherComponent, '<div testDirective="viewChild"></div>'));
TestComponent);
}
describe('ngOnInit', () => {
@ -752,7 +770,6 @@ export function main() {
it('should be called after processing the content children but before the view children',
fakeAsync(() => {
var ctx = createCompWithContentAndViewChild();
ctx.detectChanges(false);
expect(directiveLog.filter(['ngDoCheck', 'ngAfterContentInit'])).toEqual([
@ -989,11 +1006,15 @@ export function main() {
}));
it('should be called after processing the content and view children', fakeAsync(() => {
TestBed.overrideComponent(AnotherComponent, {
set: new ComponentMetadata(
{selector: 'other-cmp', template: '<div testDirective="viewChild"></div>'})
});
var ctx = createCompFixture(
'<div testDirective="parent"><div *ngFor="let x of [0,1]" testDirective="contentChild{{x}}"></div>' +
'<other-cmp></other-cmp></div>',
TestComponent,
tcb.overrideTemplate(AnotherComponent, '<div testDirective="viewChild"></div>'));
TestComponent);
ctx.detectChanges(false);
ctx.destroy();
@ -1029,9 +1050,11 @@ export function main() {
}));
it('should call ngOnDestroy on an injectable class', fakeAsync(() => {
var ctx = createCompFixture(
'<div testDirective="dir"></div>', TestComponent,
tcb.overrideProviders(TestDirective, [InjectableWithLifecycle]));
TestBed.overrideDirective(
TestDirective, {set: {providers: [InjectableWithLifecycle]}});
var ctx = createCompFixture('<div testDirective="dir"></div>', TestComponent);
ctx.debugElement.children[0].injector.get(InjectableWithLifecycle);
ctx.detectChanges(false);
@ -1143,31 +1166,6 @@ export function main() {
});
}
const ALL_DIRECTIVES = [
forwardRef(() => TestDirective),
forwardRef(() => TestComponent),
forwardRef(() => AnotherComponent),
forwardRef(() => TestLocals),
forwardRef(() => CompWithRef),
forwardRef(() => EmitterDirective),
forwardRef(() => PushComp),
forwardRef(() => OrderCheckDirective2),
forwardRef(() => OrderCheckDirective0),
forwardRef(() => OrderCheckDirective1),
forwardRef(() => Gh9882),
NgFor,
];
const ALL_PIPES = [
forwardRef(() => CountingPipe),
forwardRef(() => CountingImpurePipe),
forwardRef(() => MultiArgPipe),
forwardRef(() => PipeWithOnDestroy),
forwardRef(() => IdentityPipe),
forwardRef(() => WrappedPipe),
AsyncPipe,
];
@Injectable()
class RenderLog {
log: string[] = [];
@ -1268,23 +1266,21 @@ class MultiArgPipe implements PipeTransform {
}
}
@Component({selector: 'test-cmp', template: '', directives: ALL_DIRECTIVES, pipes: ALL_PIPES})
@Component({selector: 'test-cmp', template: 'empty'})
class TestComponent {
value: any;
a: any;
b: any;
}
@Component({selector: 'other-cmp', directives: ALL_DIRECTIVES, pipes: ALL_PIPES, template: ''})
@Component({selector: 'other-cmp', template: 'empty'})
class AnotherComponent {
}
@Component({
selector: 'comp-with-ref',
template: '<div (event)="noop()" emitterDirective></div>{{value}}',
host: {'event': 'noop()'},
directives: ALL_DIRECTIVES,
pipes: ALL_PIPES
host: {'event': 'noop()'}
})
class CompWithRef {
@Input() public value: any;
@ -1298,8 +1294,6 @@ class CompWithRef {
selector: 'push-cmp',
template: '<div (event)="noop()" emitterDirective></div>{{value}}{{renderIncrement}}',
host: {'(event)': 'noop()'},
directives: ALL_DIRECTIVES,
pipes: ALL_PIPES,
changeDetection: ChangeDetectionStrategy.OnPush
})
class PushComp {
@ -1459,7 +1453,7 @@ class TestLocals {
}
}
@Component({selector: 'root'})
@Component({selector: 'root', template: 'emtpy'})
class Person {
age: number;
name: string;
@ -1504,17 +1498,17 @@ class Address {
toString(): string { return this.city || '-'; }
}
@Component({selector: 'root'})
@Component({selector: 'root', template: 'empty'})
class Uninitialized {
value: any = null;
}
@Component({selector: 'root'})
@Component({selector: 'root', template: 'empty'})
class TestData {
public a: any;
}
@Component({selector: 'root'})
@Component({selector: 'root', template: 'empty'})
class TestDataWithGetter {
public fn: Function;
@ -1525,10 +1519,10 @@ class Holder<T> {
value: T;
}
@Component({selector: 'root'})
@Component({selector: 'root', template: 'empty'})
class PersonHolder extends Holder<Person> {
}
@Component({selector: 'root'})
@Component({selector: 'root', template: 'empty'})
class PersonHolderHolder extends Holder<Holder<Person>> {
}