Skip to content

Commit b682f84

Browse files
authored
fix(cdk/testing): strongly type return value of TestElement.getProperty (#22918)
Allows for the return value of `TestElement.getProperty` to be typed strongly through a generic parameter.
1 parent 427bbdd commit b682f84

File tree

20 files changed

+55
-55
lines changed

20 files changed

+55
-55
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export class ProtractorElement implements TestElement {
206206
}
207207

208208
/** Gets the value of a property of an element. */
209-
async getProperty(name: string): Promise<any> {
209+
async getProperty<T = any>(name: string): Promise<T> {
210210
return browser.executeScript(`return arguments[0][arguments[1]]`, this.element, name);
211211
}
212212

src/cdk/testing/selenium-webdriver/selenium-web-driver-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class SeleniumWebDriverElement implements TestElement {
163163
}
164164

165165
/** Gets the value of a property of an element. */
166-
async getProperty(name: string): Promise<any> {
166+
async getProperty<T = any>(name: string): Promise<T> {
167167
await this._stabilize();
168168
return this._executeScript(
169169
(element: Element, property: keyof Element) => element[property],

src/cdk/testing/test-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export interface TestElement {
137137
getDimensions(): Promise<ElementDimensions>;
138138

139139
/** Gets the value of a property of an element. */
140-
getProperty(name: string): Promise<any>;
140+
getProperty<T = any>(name: string): Promise<T>;
141141

142142
/** Checks whether this element matches the given selector. */
143143
matchesSelector(selector: string): Promise<boolean>;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export class UnitTestElement implements TestElement {
191191
}
192192

193193
/** Gets the value of a property of an element. */
194-
async getProperty(name: string): Promise<any> {
194+
async getProperty<T = any>(name: string): Promise<T> {
195195
await this._stabilize();
196196
return (this.element as any)[name];
197197
}

src/cdk/testing/tests/cross-environment.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ export function crossEnvironmentSpecs(
323323
it('should be able to clear', async () => {
324324
const input = await harness.input();
325325
await input.sendKeys('Yi');
326-
expect(await input.getProperty('value')).toBe('Yi');
326+
expect(await input.getProperty<string>('value')).toBe('Yi');
327327

328328
await input.clear();
329-
expect(await input.getProperty('value')).toBe('');
329+
expect(await input.getProperty<string>('value')).toBe('');
330330
});
331331

332332
it('should be able to click', async () => {
@@ -401,7 +401,7 @@ export function crossEnvironmentSpecs(
401401
const value = await harness.value();
402402
await input.sendKeys('Yi');
403403

404-
expect(await input.getProperty('value')).toBe('Yi');
404+
expect(await input.getProperty<string>('value')).toBe('Yi');
405405
expect(await value.text()).toBe('Input: Yi');
406406
});
407407

@@ -416,7 +416,7 @@ export function crossEnvironmentSpecs(
416416
const value = await harness.numberInputValue();
417417
await input.sendKeys('123.456');
418418

419-
expect(await input.getProperty('value')).toBe('123.456');
419+
expect(await input.getProperty<string>('value')).toBe('123.456');
420420
expect(await value.text()).toBe('Number value: 123.456');
421421
});
422422

@@ -453,7 +453,7 @@ export function crossEnvironmentSpecs(
453453
`;
454454
const memo = await harness.memo();
455455
await memo.sendKeys(memoStr);
456-
expect(await memo.getProperty('value')).toBe(memoStr);
456+
expect(await memo.getProperty<string>('value')).toBe(memoStr);
457457
});
458458

459459
it('should be able to getCssValue', async () => {
@@ -474,14 +474,14 @@ export function crossEnvironmentSpecs(
474474
it('should be able to get the value of a property', async () => {
475475
const input = await harness.input();
476476
await input.sendKeys('Hello');
477-
expect(await input.getProperty('value')).toBe('Hello');
477+
expect(await input.getProperty<string>('value')).toBe('Hello');
478478
});
479479

480480
it('should be able to set the value of an input', async () => {
481481
const input = await harness.input();
482482

483483
await input.setInputValue('hello');
484-
expect(await input.getProperty('value')).toBe('hello');
484+
expect(await input.getProperty<string>('value')).toBe('hello');
485485
});
486486

487487
it('should be able to set the value of a select in single selection mode', async () => {

src/material-experimental/mdc-chips/testing/chip-input-harness.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ export class MatChipInputHarness extends ComponentHarness {
3131

3232
/** Whether the input is disabled. */
3333
async isDisabled(): Promise<boolean> {
34-
return (await this.host()).getProperty('disabled')!;
34+
return (await this.host()).getProperty<boolean>('disabled');
3535
}
3636

3737
/** Whether the input is required. */
3838
async isRequired(): Promise<boolean> {
39-
return (await this.host()).getProperty('required')!;
39+
return (await this.host()).getProperty<boolean>('required');
4040
}
4141

4242
/** Gets the value of the input. */
4343
async getValue(): Promise<string> {
4444
// The "value" property of the native input is never undefined.
45-
return (await (await this.host()).getProperty('value'))!;
45+
return (await (await this.host()).getProperty<string>('value'));
4646
}
4747

4848
/** Gets the placeholder of the input. */
4949
async getPlaceholder(): Promise<string> {
50-
return (await (await this.host()).getProperty('placeholder'));
50+
return (await (await this.host()).getProperty<string>('placeholder'));
5151
}
5252

5353
/**

src/material-experimental/mdc-slider/testing/slider-harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class MatSliderHarness extends ComponentHarness {
5252
async getStep(): Promise<number> {
5353
// The same step value is forwarded to both thumbs.
5454
const startHost = await (await this.getEndThumb()).host();
55-
return coerceNumberProperty(await startHost.getProperty('step'));
55+
return coerceNumberProperty(await startHost.getProperty<string>('step'));
5656
}
5757

5858
/** Gets the maximum value of the slider. */

src/material-experimental/mdc-slider/testing/slider-thumb-harness.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class MatSliderThumbHarness extends ComponentHarness {
3838

3939
/** Gets the value of the thumb. */
4040
async getValue(): Promise<number> {
41-
return (await (await this.host()).getProperty('valueAsNumber'));
41+
return (await (await this.host()).getProperty<number>('valueAsNumber'));
4242
}
4343

4444
/** Sets the value of the thumb. */
@@ -65,12 +65,12 @@ export class MatSliderThumbHarness extends ComponentHarness {
6565

6666
/** Gets the maximum value of the thumb. */
6767
async getMaxValue(): Promise<number> {
68-
return coerceNumberProperty(await (await this.host()).getProperty('max'));
68+
return coerceNumberProperty(await (await this.host()).getProperty<number>('max'));
6969
}
7070

7171
/** Gets the minimum value of the thumb. */
7272
async getMinValue(): Promise<number> {
73-
return coerceNumberProperty(await (await this.host()).getProperty('min'));
73+
return coerceNumberProperty(await (await this.host()).getProperty<number>('min'));
7474
}
7575

7676
/** Gets the text representation of the slider's value. */
@@ -80,17 +80,17 @@ export class MatSliderThumbHarness extends ComponentHarness {
8080

8181
/** Whether the thumb is disabled. */
8282
async isDisabled(): Promise<boolean> {
83-
return (await this.host()).getProperty('disabled');
83+
return (await this.host()).getProperty<boolean>('disabled');
8484
}
8585

8686
/** Gets the name of the thumb. */
8787
async getName(): Promise<string> {
88-
return (await (await this.host()).getProperty('name'));
88+
return (await (await this.host()).getProperty<string>('name'));
8989
}
9090

9191
/** Gets the id of the thumb. */
9292
async getId(): Promise<string> {
93-
return (await (await this.host()).getProperty('id'));
93+
return (await (await this.host()).getProperty<string>('id'));
9494
}
9595

9696
/**

src/material/autocomplete/testing/autocomplete-harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export abstract class _MatAutocompleteHarnessBase<
3838

3939
/** Gets the value of the autocomplete input. */
4040
async getValue(): Promise<string> {
41-
return (await this.host()).getProperty('value');
41+
return (await this.host()).getProperty<string>('value');
4242
}
4343

4444
/** Whether the autocomplete input is disabled. */

src/material/checkbox/testing/checkbox-harness.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ export abstract class _MatCheckboxHarnessBase extends ComponentHarness {
2121

2222
/** Whether the checkbox is checked. */
2323
async isChecked(): Promise<boolean> {
24-
const checked = (await this._input()).getProperty('checked');
24+
const checked = (await this._input()).getProperty<boolean>('checked');
2525
return coerceBooleanProperty(await checked);
2626
}
2727

2828
/** Whether the checkbox is in an indeterminate state. */
2929
async isIndeterminate(): Promise<boolean> {
30-
const indeterminate = (await this._input()).getProperty('indeterminate');
30+
const indeterminate = (await this._input()).getProperty<string>('indeterminate');
3131
return coerceBooleanProperty(await indeterminate);
3232
}
3333

@@ -39,7 +39,7 @@ export abstract class _MatCheckboxHarnessBase extends ComponentHarness {
3939

4040
/** Whether the checkbox is required. */
4141
async isRequired(): Promise<boolean> {
42-
const required = (await this._input()).getProperty('required');
42+
const required = (await this._input()).getProperty<boolean>('required');
4343
return coerceBooleanProperty(await required);
4444
}
4545

@@ -56,7 +56,7 @@ export abstract class _MatCheckboxHarnessBase extends ComponentHarness {
5656

5757
/** Gets the checkbox's value. */
5858
async getValue(): Promise<string|null> {
59-
return (await this._input()).getProperty('value');
59+
return (await this._input()).getProperty<string|null>('value');
6060
}
6161

6262
/** Gets the checkbox's aria-label. */

0 commit comments

Comments
 (0)