From cc22b051e37584959161b033faff82ab142b625e Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 16 Aug 2016 20:20:09 -0700 Subject: [PATCH] refactor(core): update change_detection_integration_spec not to use TestComponentBuilder --- .../change_detection_integration_spec.ts | 138 +++++++++--------- 1 file changed, 66 insertions(+), 72 deletions(-) diff --git a/modules/@angular/core/test/linker/change_detection_integration_spec.ts b/modules/@angular/core/test/linker/change_detection_integration_spec.ts index dffd61006e..f305ba28fe 100644 --- a/modules/@angular/core/test/linker/change_detection_integration_spec.ts +++ b/modules/@angular/core/test/linker/change_detection_integration_spec.ts @@ -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(template: string): ComponentFixture; function createCompFixture(template: string, compType: Type): ComponentFixture; function createCompFixture( - template: string, compType: Type, _tcb: TestComponentBuilder): ComponentFixture; - function createCompFixture( - template: string, compType: Type = TestComponent, - _tcb: TestComponentBuilder = null): ComponentFixture { - if (isBlank(_tcb)) { - _tcb = tcb; - } - return _tcb - .overrideView( - compType, - new ViewMetadata({template: template, directives: ALL_DIRECTIVES, pipes: ALL_PIPES})) - .createFakeAsync(compType); + template: string, compType: Type = TestComponent): ComponentFixture { + TestBed.overrideComponent(compType, {set: new ComponentMetadata({template})}); + + initHelpers(); + + return TestBed.createComponent(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 { @@ -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 { + TestBed.overrideComponent(AnotherComponent, { + set: new ComponentMetadata({ + selector: 'other-cmp', + template: '
', + }) + }); + return createCompFixture( '
', - TestComponent, - tcb.overrideTemplate(AnotherComponent, '
')); + 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: '
'}) + }); + var ctx = createCompFixture( '
' + '
', - TestComponent, - tcb.overrideTemplate(AnotherComponent, '
')); + 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( - '
', TestComponent, - tcb.overrideProviders(TestDirective, [InjectableWithLifecycle])); + TestBed.overrideDirective( + TestDirective, {set: {providers: [InjectableWithLifecycle]}}); + + var ctx = createCompFixture('
', 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: '
{{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: '
{{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 { value: T; } -@Component({selector: 'root'}) +@Component({selector: 'root', template: 'empty'}) class PersonHolder extends Holder { } -@Component({selector: 'root'}) +@Component({selector: 'root', template: 'empty'}) class PersonHolderHolder extends Holder> { }