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 committed Aug 22, 2019
1 parent fa81811 commit 433cfe5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
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 @@ -136,4 +136,8 @@ export class ProtractorElement implements TestElement {
const {x: left, y: top} = await this.element.getLocation();
return {width, height, left, top};
}

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 @@ -101,4 +101,7 @@ export interface TestElement {

/** Gets the dimensions of the element. */
getDimensions(): Promise<ElementDimensions>;

/** Gets the value of a property of an element. */
getPropertyValue(name: string): Promise<any>;
}
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 @@ -139,4 +139,9 @@ export class UnitTestElement implements TestElement {
await this._stabilize();
return this.element.getBoundingClientRect();
}

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 @@ -254,6 +254,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 @@ -271,6 +271,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 433cfe5

Please sign in to comment.