Skip to content

Commit

Permalink
feat(cdk-experimental): expose root loader instance in testbed harnes…
Browse files Browse the repository at this point in the history
…s environment

Adds a new method that can be used by harness consumers to load a harness
through `HarnessLoader` from the document root. This is helpful for harnesses
which need to match elements outside of testbed fixtures (e.g. snack-bars, overlays etc.).

Needed for angular#16697 and angular#16709.
  • Loading branch information
devversion committed Aug 29, 2019
1 parent d3f63a3 commit 9a44d8a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
return new TestbedHarnessEnvironment(fixture.nativeElement, fixture);
}

/**
* Creates a `HarnessLoader` at the document root. This can be used if harnesses are
* located outside of a fixture (e.g. overlays appended to the document body).
*/
static documentRootLoader(fixture: ComponentFixture<unknown>): HarnessLoader {
return new TestbedHarnessEnvironment(document.body, fixture);
}

/**
* Creates an instance of the given harness type, using the fixture's root element as the
* harness's host element. This method should be used when creating a harness for the root element
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {ComponentHarness} from '@angular/cdk-experimental/testing';

export class FakeOverlayHarness extends ComponentHarness {
static readonly hostSelector = '.fake-overlay';

/** Gets the description of the fake overlay. */
async getDescription(): Promise<string> {
return (await this.host()).text();
}
}
14 changes: 13 additions & 1 deletion src/cdk-experimental/testing/tests/test-main-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ChangeDetectorRef,
Component,
ElementRef,
OnDestroy,
ViewChild,
ViewEncapsulation
} from '@angular/core';
Expand All @@ -29,7 +30,7 @@ import {
changeDetection: ChangeDetectionStrategy.OnPush,
})

export class TestMainComponent {
export class TestMainComponent implements OnDestroy {
username: string;
counter: number;
asyncCounter: number;
Expand All @@ -44,6 +45,8 @@ export class TestMainComponent {

@ViewChild('clickTestElement', {static: false}) clickTestElement: ElementRef<HTMLElement>;

private _fakeOverlayElement: HTMLElement;

onMouseOver() {
this._isHovering = true;
}
Expand All @@ -63,6 +66,15 @@ export class TestMainComponent {
this.asyncCounter = 5;
this._cdr.markForCheck();
}, 1000);

this._fakeOverlayElement = document.createElement('div');
this._fakeOverlayElement.classList.add('fake-overlay');
this._fakeOverlayElement.innerText = 'This is a fake overlay.';
document.body.appendChild(this._fakeOverlayElement);
}

ngOnDestroy() {
document.body.removeChild(this._fakeOverlayElement);
}

click() {
Expand Down
11 changes: 11 additions & 0 deletions src/cdk-experimental/testing/tests/testbed.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {HarnessLoader} from '@angular/cdk-experimental/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FakeOverlayHarness} from './harnesses/fake-overlay-harness';
import {MainComponentHarness} from './harnesses/main-component-harness';
import {SubComponentHarness} from './harnesses/sub-component-harness';
import {TestComponentsModule} from './test-components-module';
Expand Down Expand Up @@ -77,6 +78,16 @@ describe('TestbedHarnessEnvironment', () => {
const harnesses = await loader.getAllHarnesses(SubComponentHarness);
expect(harnesses.length).toBe(2);
});

it('should be able to load harness through document root loader', async () => {
const documentRootHarnesses =
await TestbedHarnessEnvironment.documentRootLoader(fixture).getAllHarnesses(
FakeOverlayHarness);
const fixtureHarnesses = await loader.getAllHarnesses(FakeOverlayHarness);
expect(fixtureHarnesses.length).toBe(0);
expect(documentRootHarnesses.length).toBe(1);
expect(await documentRootHarnesses[0].getDescription()).toBe('This is a fake overlay.');
});
});

describe('ComponentHarness', () => {
Expand Down

0 comments on commit 9a44d8a

Please sign in to comment.