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 7, 2019
1 parent 8e321ae commit a451039
Show file tree
Hide file tree
Showing 7 changed files with 24 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 @@ -55,4 +55,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(`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 @@ -46,4 +46,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 @@ -105,4 +105,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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class MainComponentHarness extends ComponentHarness {
readonly counter = this.locatorFor('#counter');
readonly input = this.locatorFor('#input');
readonly value = this.locatorFor('#value');
readonly username = this.locatorFor('#username');
readonly allLabels = this.locatorForAll('label');
readonly allLists = this.locatorForAll(SubComponentHarness);
readonly memo = this.locatorFor('textarea');
Expand Down
5 changes: 5 additions & 0 deletions src/cdk-experimental/testing/tests/protractor.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ 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 element = await harness.username();
expect(await element.getPropertyValue('id')).toBe('username');
});
});

describe('HarnessPredicate', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/cdk-experimental/testing/tests/testbed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ 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 element = await harness.username();
expect(await element.getPropertyValue('id')).toBe('username');
});
});

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

0 comments on commit a451039

Please sign in to comment.