Skip to content

Commit 23ee4fb

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 23ee4fb

File tree

6 files changed

+25
-1
lines changed

6 files changed

+25
-1
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(`return 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/protractor.e2e.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ 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 input = await harness.input();
236+
await input.sendKeys('Hello');
237+
expect(await input.getPropertyValue('value')).toBe('Hello');
238+
});
233239
});
234240

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ 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 input = await harness.input();
253+
await input.sendKeys('Hello');
254+
expect(await input.getPropertyValue('value')).toBe('Hello');
255+
});
250256
});
251257

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

0 commit comments

Comments
 (0)