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

View File

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