Skip to content

Commit

Permalink
fix(material-experimental): unit tests getting wrong root element and…
Browse files Browse the repository at this point in the history
… add API for getting property value

Based on the discussion in angular#16697 (comment), these changes fix a couple of things that I ran into while doing the `mat-autocomplete` test harness in angular#16620.

* Fixes querying for elements outside the harness not working, because the wrong root node was set.
* Adds an API to retrieve the value of a property on a DOM node.
  • Loading branch information
crisbeto authored and devversion committed Aug 15, 2019
1 parent 6fe8c06 commit 64cd932
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/cdk-experimental/testing/protractor/protractor-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,8 @@ export class ProtractorElement implements TestElement {
const classes = (await this.getAttribute('class')) || '';
return new Set(classes.split(/\s+/).filter(c => c)).has(name);
}

async getPropertyValue(name: string): Promise<any> {
return browser.executeScript(`return arguments[0]['${name}']`, this.element);
}
}
3 changes: 3 additions & 0 deletions src/cdk-experimental/testing/test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ export interface TestElement {

/** Checks whether the element has the given class. */
hasClass(name: string): Promise<boolean>;

/** Gets the value of a property of an element. */
getPropertyValue(name: string): Promise<any>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
}

protected getDocumentRoot(): Element {
return this._fixture.nativeElement;
return document.body;
}

protected createTestElement(element: Element): TestElement {
Expand Down
5 changes: 5 additions & 0 deletions src/cdk-experimental/testing/testbed/unit-test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,9 @@ export class UnitTestElement implements TestElement {
await this._stabilize();
return this.element.classList.contains(name);
}

async getPropertyValue(name: string): Promise<any> {
await this._stabilize();
return (this.element as any)[name];
}
}
6 changes: 6 additions & 0 deletions src/cdk-experimental/testing/tests/protractor.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ describe('ProtractorHarnessEnvironment', () => {
expect(await (await browser.switchTo().activeElement()).getText())
.not.toBe(await button.text());
});

it('should be able to get the value of a property', async () => {
const input = await harness.input();
await input.sendKeys('Hello');
expect(await input.getPropertyValue('value')).toBe('Hello');
});
});

describe('HarnessPredicate', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/cdk-experimental/testing/tests/testbed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ describe('TestbedHarnessEnvironment', () => {
await button.blur();
expect(activeElementText()).not.toBe(await button.text());
});

it('should be able to get the value of a property', async () => {
const input = await harness.input();
await input.sendKeys('Hello');
expect(await input.getPropertyValue('value')).toBe('Hello');
});
});

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

0 comments on commit 64cd932

Please sign in to comment.