Skip to content

Commit 64cd932

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 6fe8c06 commit 64cd932

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
@@ -126,4 +126,8 @@ export class ProtractorElement implements TestElement {
126126
const classes = (await this.getAttribute('class')) || '';
127127
return new Set(classes.split(/\s+/).filter(c => c)).has(name);
128128
}
129+
130+
async getPropertyValue(name: string): Promise<any> {
131+
return browser.executeScript(`return arguments[0]['${name}']`, this.element);
132+
}
129133
}

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

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

9595
/** Checks whether the element has the given class. */
9696
hasClass(name: string): Promise<boolean>;
97+
98+
/** Gets the value of a property of an element. */
99+
getPropertyValue(name: string): Promise<any>;
97100
}

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
@@ -126,4 +126,9 @@ export class UnitTestElement implements TestElement {
126126
await this._stabilize();
127127
return this.element.classList.contains(name);
128128
}
129+
130+
async getPropertyValue(name: string): Promise<any> {
131+
await this._stabilize();
132+
return (this.element as any)[name];
133+
}
129134
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ describe('ProtractorHarnessEnvironment', () => {
242242
expect(await (await browser.switchTo().activeElement()).getText())
243243
.not.toBe(await button.text());
244244
});
245+
246+
it('should be able to get the value of a property', async () => {
247+
const input = await harness.input();
248+
await input.sendKeys('Hello');
249+
expect(await input.getPropertyValue('value')).toBe('Hello');
250+
});
245251
});
246252

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ describe('TestbedHarnessEnvironment', () => {
259259
await button.blur();
260260
expect(activeElementText()).not.toBe(await button.text());
261261
});
262+
263+
it('should be able to get the value of a property', async () => {
264+
const input = await harness.input();
265+
await input.sendKeys('Hello');
266+
expect(await input.getPropertyValue('value')).toBe('Hello');
267+
});
262268
});
263269

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

0 commit comments

Comments
 (0)