From 78b6f88cb396748985eedc4847b3855707ac6140 Mon Sep 17 00:00:00 2001 From: Kara Erickson Date: Thu, 8 Nov 2018 15:57:17 -0800 Subject: [PATCH] fix(ivy): TestBed should use an NgZone (#27015) PR Close #27015 --- packages/core/testing/src/r3_test_bed.ts | 8 ++-- .../forms/test/template_integration_spec.ts | 37 +++++++++--------- .../test/value_accessor_integration_spec.ts | 38 +++++++++---------- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/packages/core/testing/src/r3_test_bed.ts b/packages/core/testing/src/r3_test_bed.ts index caf0deec71..886c10835f 100644 --- a/packages/core/testing/src/r3_test_bed.ts +++ b/packages/core/testing/src/r3_test_bed.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Component, Directive, Injector, NgModule, Pipe, PlatformRef, Provider, RendererFactory2, SchemaMetadata, Type, ɵInjectableDef as InjectableDef, ɵNgModuleDef as NgModuleDef, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵRender3ComponentFactory as ComponentFactory, ɵRender3DebugRendererFactory2 as Render3DebugRendererFactory2, ɵRender3NgModuleRef as NgModuleRef, ɵWRAP_RENDERER_FACTORY2 as WRAP_RENDERER_FACTORY2, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵgetInjectableDef as getInjectableDef, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵstringify as stringify} from '@angular/core'; +import {Component, Directive, Injector, NgModule, NgZone, Pipe, PlatformRef, Provider, RendererFactory2, SchemaMetadata, Type, ɵInjectableDef as InjectableDef, ɵNgModuleDef as NgModuleDef, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵRender3ComponentFactory as ComponentFactory, ɵRender3DebugRendererFactory2 as Render3DebugRendererFactory2, ɵRender3NgModuleRef as NgModuleRef, ɵWRAP_RENDERER_FACTORY2 as WRAP_RENDERER_FACTORY2, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵgetInjectableDef as getInjectableDef, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵstringify as stringify} from '@angular/core'; import {ComponentFixture} from './component_fixture'; import {MetadataOverride} from './metadata_override'; @@ -361,7 +361,7 @@ export class TestBedRender3 implements Injector, TestBed { const componentRef = componentFactory.create(Injector.NULL, [], `#${rootElId}`, this._moduleRef); const autoDetect: boolean = this.get(ComponentFixtureAutoDetect, false); - const fixture = new ComponentFixture(componentRef, null, autoDetect); + const fixture = new ComponentFixture(componentRef, this.get(NgZone), autoDetect); this._activeFixtures.push(fixture); return fixture; } @@ -424,7 +424,9 @@ export class TestBedRender3 implements Injector, TestBed { class RootScopeModule { } - const providers = [...this._providers, ...this._providerOverrides]; + const ngZone = new NgZone({enableLongStackTrace: true}); + const providers = + [{provide: NgZone, useValue: ngZone}, ...this._providers, ...this._providerOverrides]; const declarations = this._declarations; const imports = [RootScopeModule, this.ngModule, this._imports]; diff --git a/packages/forms/test/template_integration_spec.ts b/packages/forms/test/template_integration_spec.ts index db62d4cc15..f000b718ab 100644 --- a/packages/forms/test/template_integration_spec.ts +++ b/packages/forms/test/template_integration_spec.ts @@ -149,7 +149,7 @@ import {NgModelCustomComp, NgModelCustomWrapper} from './value_accessor_integrat expect(form.value).toEqual({}); })); - fixmeIvy('whenStable not working') && + fixmeIvy('host bindings do not yet work with classes or styles') && it('should set status classes with ngModel', async(() => { const fixture = initTest(NgModelForm); fixture.componentInstance.name = 'aa'; @@ -176,7 +176,7 @@ import {NgModelCustomComp, NgModelCustomWrapper} from './value_accessor_integrat }); })); - fixmeIvy('whenStable not working') && + fixmeIvy('host bindings do not yet work with classes or styles') && it('should set status classes with ngModel and async validators', fakeAsync(() => { const fixture = initTest(NgModelAsyncValidation, NgAsyncValidator); @@ -204,7 +204,7 @@ import {NgModelCustomComp, NgModelCustomWrapper} from './value_accessor_integrat }); })); - fixmeIvy('whenStable not working') && + fixmeIvy('host bindings do not yet work with classes or styles') && it('should set status classes with ngModelGroup and ngForm', async(() => { const fixture = initTest(NgModelGroupForm); fixture.componentInstance.first = ''; @@ -1184,23 +1184,22 @@ import {NgModelCustomComp, NgModelCustomWrapper} from './value_accessor_integrat expect(input.nativeElement.disabled).toBe(true); })); - fixmeIvy('whenStable not working') && - it('should disable a custom control if disabled attr is added', async(() => { - const fixture = initTest(NgModelCustomWrapper, NgModelCustomComp); - fixture.componentInstance.name = 'Nancy'; - fixture.componentInstance.isDisabled = true; - fixture.detectChanges(); - fixture.whenStable().then(() => { - fixture.detectChanges(); - fixture.whenStable().then(() => { - const form = fixture.debugElement.children[0].injector.get(NgForm); - expect(form.control.get('name') !.disabled).toBe(true); + it('should disable a custom control if disabled attr is added', async(() => { + const fixture = initTest(NgModelCustomWrapper, NgModelCustomComp); + fixture.componentInstance.name = 'Nancy'; + fixture.componentInstance.isDisabled = true; + fixture.detectChanges(); + fixture.whenStable().then(() => { + fixture.detectChanges(); + fixture.whenStable().then(() => { + const form = fixture.debugElement.children[0].injector.get(NgForm); + expect(form.control.get('name') !.disabled).toBe(true); - const customInput = fixture.debugElement.query(By.css('[name="custom"]')); - expect(customInput.nativeElement.disabled).toEqual(true); - }); - }); - })); + const customInput = fixture.debugElement.query(By.css('[name="custom"]')); + expect(customInput.nativeElement.disabled).toEqual(true); + }); + }); + })); it('should disable a control with unbound disabled attr', fakeAsync(() => { TestBed.overrideComponent(NgModelForm, { diff --git a/packages/forms/test/value_accessor_integration_spec.ts b/packages/forms/test/value_accessor_integration_spec.ts index a10a531808..59b533c24f 100644 --- a/packages/forms/test/value_accessor_integration_spec.ts +++ b/packages/forms/test/value_accessor_integration_spec.ts @@ -1059,28 +1059,26 @@ import {fixmeIvy} from '@angular/private/testing'; }); describe('in template-driven forms', () => { - fixmeIvy('whenStable not working') && - it('should support standard writing to view and model', async(() => { - const fixture = initTest(NgModelCustomWrapper, NgModelCustomComp); - fixture.componentInstance.name = 'Nancy'; + it('should support standard writing to view and model', async(() => { + const fixture = initTest(NgModelCustomWrapper, NgModelCustomComp); + fixture.componentInstance.name = 'Nancy'; + fixture.detectChanges(); + fixture.whenStable().then(() => { + fixture.detectChanges(); + fixture.whenStable().then(() => { + // model -> view + const customInput = fixture.debugElement.query(By.css('[name="custom"]')); + expect(customInput.nativeElement.value).toEqual('Nancy'); + + customInput.nativeElement.value = 'Carson'; + dispatchEvent(customInput.nativeElement, 'input'); fixture.detectChanges(); - fixture.whenStable().then(() => { - fixture.detectChanges(); - fixture.whenStable().then(() => { - // model -> view - const customInput = fixture.debugElement.query(By.css('[name="custom"]')); - expect(customInput.nativeElement.value).toEqual('Nancy'); - - customInput.nativeElement.value = 'Carson'; - dispatchEvent(customInput.nativeElement, 'input'); - fixture.detectChanges(); - - // view -> model - expect(fixture.componentInstance.name).toEqual('Carson'); - }); - }); - })); + // view -> model + expect(fixture.componentInstance.name).toEqual('Carson'); + }); + }); + })); }); });