Skip to content

Commit

Permalink
fixup! feat(cdk/testing): add method to wait for async tasks to complete
Browse files Browse the repository at this point in the history
Change name
  • Loading branch information
devversion committed Oct 18, 2019
1 parent ee7c989 commit d5409f3
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/cdk/testing/component-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export interface LocatorFactory {
* Waits for all scheduled or running async tasks to complete. This allows harness
* authors to wait for async tasks outside of the Angular zone.
*/
waitForAsyncTasksToComplete(): Promise<void>;
waitForTasksOutsideAngular(): Promise<void>;
}

/**
Expand Down Expand Up @@ -295,8 +295,8 @@ export abstract class ComponentHarness {
* Waits for all scheduled or running async tasks to complete. This allows harness
* authors to wait for async tasks outside of the Angular zone.
*/
protected async waitForAsyncTasksToComplete() {
return this.locatorFactory.waitForAsyncTasksToComplete();
protected async waitForTasksOutsideAngular() {
return this.locatorFactory.waitForTasksOutsideAngular();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/testing/harness-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
abstract forceStabilize(): Promise<void>;

// Part of LocatorFactory interface, subclasses will implement.
abstract waitForAsyncTasksToComplete(): Promise<void>;
abstract waitForTasksOutsideAngular(): Promise<void>;

/** Gets the root element for the document. */
protected abstract getDocumentRoot(): E;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ProtractorHarnessEnvironment extends HarnessEnvironment<ElementFind

async forceStabilize(): Promise<void> {}

async waitForAsyncTasksToComplete(): Promise<void> {
async waitForTasksOutsideAngular(): Promise<void> {
// TODO: figure out how we can do this for the protractor environment.
// https://github.com/angular/components/issues/17412
}
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/testing/testbed/testbed-harness-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
await this._fixture.whenStable();
}

async waitForAsyncTasksToComplete(): Promise<void> {
async waitForTasksOutsideAngular(): Promise<void> {
// If we run in the fake async zone, we run "flush" to run any scheduled tasks. This
// ensures that the harnesses behave inside of the FakeAsyncTestZone similar to the
// "AsyncTestZone" and the root zone (i.e. neither fakeAsync or async). Note that we
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/testing/tests/harnesses/main-component-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class MainComponentHarness extends ComponentHarness {
await (await this.taskStateTestTrigger()).click();
// Wait for async tasks to complete since the click caused a
// timeout to be scheduled outside of the NgZone.
await this.waitForAsyncTasksToComplete();
await this.waitForTasksOutsideAngular();
return (await this.taskStateTestResult()).text();
}
}
6 changes: 3 additions & 3 deletions src/cdk/testing/tests/testbed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,17 @@ describe('TestbedHarnessEnvironment', () => {
expect(subcomps.length).toBe(2);
});

it('should respect tasks outside of NgZone when stabilizing within native async/await',
it('should be able to wait for tasks outside of Angular within native async/await',
async () => {
expect(await harness.getTaskStateResult()).toBe('result');
});

it('should respect tasks outside of NgZone when stabilizing within async test zone',
it('should be able to wait for tasks outside of Angular within async test zone',
async (() => {
harness.getTaskStateResult().then(res => expect(res).toBe('result'));
}));

it('should respect tasks outside of NgZone when stabilizing within fakeAsync test zone',
it('should be able to wait for tasks outside of Angular within fakeAsync test zone',
fakeAsync(async () => {
expect(await harness.getTaskStateResult()).toBe('result');
}));
Expand Down
6 changes: 3 additions & 3 deletions tools/public_api_guard/cdk/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export declare abstract class ComponentHarness {
protected locatorForAll<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T> | HarnessPredicate<T>): AsyncFactoryFn<T[]>;
protected locatorForOptional<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T> | HarnessPredicate<T>): AsyncFactoryFn<T | null>;
protected locatorForOptional(selector: string): AsyncFactoryFn<TestElement | null>;
protected waitForAsyncTasksToComplete(): Promise<void>;
protected waitForTasksOutsideAngular(): Promise<void>;
}

export interface ComponentHarnessConstructor<T extends ComponentHarness> {
Expand Down Expand Up @@ -73,7 +73,7 @@ export declare abstract class HarnessEnvironment<E> implements HarnessLoader, Lo
locatorForAll(selector: string): AsyncFactoryFn<TestElement[]>;
locatorForOptional<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T> | HarnessPredicate<T>): AsyncFactoryFn<T | null>;
locatorForOptional(selector: string): AsyncFactoryFn<TestElement | null>;
abstract waitForAsyncTasksToComplete(): Promise<void>;
abstract waitForTasksOutsideAngular(): Promise<void>;
}

export interface HarnessLoader {
Expand Down Expand Up @@ -110,7 +110,7 @@ export interface LocatorFactory {
locatorForAll<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T> | HarnessPredicate<T>): AsyncFactoryFn<T[]>;
locatorForOptional<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T> | HarnessPredicate<T>): AsyncFactoryFn<T | null>;
locatorForOptional(selector: string): AsyncFactoryFn<TestElement | null>;
waitForAsyncTasksToComplete(): Promise<void>;
waitForTasksOutsideAngular(): Promise<void>;
}

export interface ModifierKeys {
Expand Down

0 comments on commit d5409f3

Please sign in to comment.