Skip to content

Commit 3c87bee

Browse files
crisbetodevversion
authored andcommitted
fix(material-experimental): unit tests getting wrong root element and add API for getting property value
Based on the discussion in #16697 (comment), these changes fix a couple of things that I ran into while doing the `mat-autocomplete` test harness in #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.
1 parent dd42956 commit 3c87bee

File tree

8 files changed

+25
-2
lines changed

8 files changed

+25
-2
lines changed

src/cdk-experimental/testing/protractor/protractor-element.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ export class ProtractorElement implements TestElement {
5555
const classes = (await this.getAttribute('class')) || '';
5656
return new Set(classes.split(/\s+/).filter(c => c)).has(name);
5757
}
58+
59+
async getPropertyValue(name: string): Promise<any> {
60+
return browser.executeScript(`arguments[0]['${name}']`, this.element);
61+
}
5862
}

src/cdk-experimental/testing/test-element.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ export interface TestElement {
4646

4747
/** Checks whether the element has the given class. */
4848
hasClass(name: string): Promise<boolean>;
49+
50+
/** Gets the value of a property of an element. */
51+
getPropertyValue(name: string): Promise<any>;
4952
}

src/cdk-experimental/testing/testbed/testbed-harness-environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
3737
}
3838

3939
protected getDocumentRoot(): Element {
40-
return this._fixture.nativeElement;
40+
return document.body;
4141
}
4242

4343
protected createTestElement(element: Element): TestElement {

src/cdk-experimental/testing/testbed/unit-test-element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,9 @@ export class UnitTestElement implements TestElement {
105105
await this._stabilize();
106106
return this.element.classList.contains(name);
107107
}
108+
109+
async getPropertyValue(name: string): Promise<any> {
110+
await this._stabilize();
111+
return (this.element as any)[name];
112+
}
108113
}

src/cdk-experimental/testing/tests/harnesses/main-component-harness.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class MainComponentHarness extends ComponentHarness {
2323
readonly counter = this.locatorFor('#counter');
2424
readonly input = this.locatorFor('#input');
2525
readonly value = this.locatorFor('#value');
26+
readonly username = this.locatorFor('#username');
2627
readonly allLabels = this.locatorForAll('label');
2728
readonly allLists = this.locatorForAll(SubComponentHarness);
2829
readonly memo = this.locatorFor('textarea');

src/cdk-experimental/testing/tests/protractor.e2e.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ describe('ProtractorHarnessEnvironment', () => {
230230
expect(await (await browser.switchTo().activeElement()).getText())
231231
.not.toBe(await button.text());
232232
});
233+
234+
it('should be able to get the value of a property', async () => {
235+
const element = await harness.username();
236+
expect(await element.getPropertyValue('title')).toBe('Name of the user');
237+
});
233238
});
234239

235240
describe('HarnessPredicate', () => {

src/cdk-experimental/testing/tests/test-main-component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h1 style="height: 50px">Main Component</h1>
2-
<div id="username">Hello {{username}} from Angular 2!</div>
2+
<div id="username" title="Name of the user">Hello {{username}} from Angular 2!</div>
33
<div class="counters">
44
<button (click)="click()">Up</button><br>
55
<label>Count:</label>

src/cdk-experimental/testing/tests/testbed.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ describe('TestbedHarnessEnvironment', () => {
247247
await button.blur();
248248
expect(activeElementText()).not.toBe(await button.text());
249249
});
250+
251+
it('should be able to get the value of a property', async () => {
252+
const element = await harness.username();
253+
expect(await element.getPropertyValue('title')).toBe('Name of the user');
254+
});
250255
});
251256

252257
describe('HarnessPredicate', () => {

0 commit comments

Comments
 (0)