From 38c524d655fe152e44e9665c5be48410dff98376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matias=20Niemel=C3=A4?= Date: Fri, 12 May 2017 16:49:16 -0400 Subject: [PATCH] feat(core): introduce fixture.whenRenderingDone for testing (#16732) --- packages/core/test/component_fixture_spec.ts | 19 +++++++++++++++++++ .../core/testing/src/component_fixture.ts | 8 ++++++++ tools/public_api_guard/core/testing.d.ts | 1 + 3 files changed, 28 insertions(+) diff --git a/packages/core/test/component_fixture_spec.ts b/packages/core/test/component_fixture_spec.ts index 97abe805e6..8b78fe270c 100644 --- a/packages/core/test/component_fixture_spec.ts +++ b/packages/core/test/component_fixture_spec.ts @@ -158,6 +158,25 @@ export function main() { }); })); + it('should signal through whenRenderingDone when the fixture is stable', async(() => { + const componentFixture = TestBed.createComponent(AsyncComp); + + componentFixture.detectChanges(); + expect(componentFixture.nativeElement).toHaveText('1'); + + const element = componentFixture.debugElement.children[0]; + dispatchEvent(element.nativeElement, 'click'); + expect(componentFixture.nativeElement).toHaveText('1'); + + // Component is updated asynchronously. Wait for the fixture to become stable + // before checking. + componentFixture.whenRenderingDone().then((waited) => { + expect(waited).toBe(true); + componentFixture.detectChanges(); + expect(componentFixture.nativeElement).toHaveText('11'); + }); + })); + it('should wait for macroTask(setTimeout) while checking for whenStable ' + '(autoDetectChanges)', async(() => { diff --git a/packages/core/testing/src/component_fixture.ts b/packages/core/testing/src/component_fixture.ts index 23335a71b4..ba7bc98314 100644 --- a/packages/core/testing/src/component_fixture.ts +++ b/packages/core/testing/src/component_fixture.ts @@ -160,6 +160,14 @@ export class ComponentFixture { } } + /** + * Get a promise that resolves when the ui state is stable following animations. + */ + whenRenderingDone(): Promise { + // this is temporary until this is functional + return this.whenStable(); + } + /** * Trigger component destruction. */ diff --git a/tools/public_api_guard/core/testing.d.ts b/tools/public_api_guard/core/testing.d.ts index 314d138cc9..b5768129db 100644 --- a/tools/public_api_guard/core/testing.d.ts +++ b/tools/public_api_guard/core/testing.d.ts @@ -16,6 +16,7 @@ export declare class ComponentFixture { destroy(): void; detectChanges(checkNoChanges?: boolean): void; isStable(): boolean; + whenRenderingDone(): Promise; whenStable(): Promise; }