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. */

src/material/datepicker/testing/datepicker-input-harness-base.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ export function getInputPredicate<T extends MatDatepickerInputHarnessBase>(
3131
export abstract class MatDatepickerInputHarnessBase extends MatFormFieldControlHarness {
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 always defined.
45-
return (await (await this.host()).getProperty('value'))!;
45+
return (await (await this.host()).getProperty<string>('value'));
4646
}
4747

4848
/**
@@ -65,7 +65,7 @@ export abstract class MatDatepickerInputHarnessBase extends MatFormFieldControlH
6565

6666
/** Gets the placeholder of the input. */
6767
async getPlaceholder(): Promise<string> {
68-
return (await (await this.host()).getProperty('placeholder'));
68+
return (await (await this.host()).getProperty<string>('placeholder'));
6969
}
7070

7171
/**

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@ export class MatInputHarness extends MatFormFieldControlHarness {
3535

3636
/** Whether the input is disabled. */
3737
async isDisabled(): Promise<boolean> {
38-
return (await this.host()).getProperty('disabled')!;
38+
return (await this.host()).getProperty<boolean>('disabled');
3939
}
4040

4141
/** Whether the input is required. */
4242
async isRequired(): Promise<boolean> {
43-
return (await this.host()).getProperty('required')!;
43+
return (await this.host()).getProperty<boolean>('required');
4444
}
4545

4646
/** Whether the input is readonly. */
4747
async isReadonly(): Promise<boolean> {
48-
return (await this.host()).getProperty('readOnly')!;
48+
return (await this.host()).getProperty<boolean>('readOnly');
4949
}
5050

5151
/** Gets the value of the input. */
5252
async getValue(): Promise<string> {
5353
// The "value" property of the native input is never undefined.
54-
return (await (await this.host()).getProperty('value'))!;
54+
return (await (await this.host()).getProperty<string>('value'));
5555
}
5656

5757
/** Gets the name of the input. */
5858
async getName(): Promise<string> {
5959
// The "name" property of the native input is never undefined.
60-
return (await (await this.host()).getProperty('name'))!;
60+
return (await (await this.host()).getProperty<string>('name'));
6161
}
6262

6363
/**
@@ -66,7 +66,7 @@ export class MatInputHarness extends MatFormFieldControlHarness {
6666
*/
6767
async getType(): Promise<string> {
6868
// The "type" property of the native input is never undefined.
69-
return (await (await this.host()).getProperty('type'))!;
69+
return (await (await this.host()).getProperty<string>('type'));
7070
}
7171

7272
/** Gets the placeholder of the input. */
@@ -83,7 +83,7 @@ export class MatInputHarness extends MatFormFieldControlHarness {
8383
async getId(): Promise<string> {
8484
// The input directive always assigns a unique id to the input in
8585
// case no id has been explicitly specified.
86-
return (await (await this.host()).getProperty('id'))!;
86+
return (await (await this.host()).getProperty<string>('id'));
8787
}
8888

8989
/**

src/material/input/testing/native-option-harness.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ export class MatNativeOptionHarness extends ComponentHarness {
3434

3535
/** Gets the option's label text. */
3636
async getText(): Promise<string> {
37-
return (await this.host()).getProperty('label');
37+
return (await this.host()).getProperty<string>('label');
3838
}
3939

4040
/** Index of the option within the native `select` element. */
4141
async getIndex(): Promise<number> {
42-
return (await this.host()).getProperty('index');
42+
return (await this.host()).getProperty<number>('index');
4343
}
4444

4545
/** Gets whether the option is disabled. */
4646
async isDisabled(): Promise<boolean> {
47-
return (await this.host()).getProperty('disabled');
47+
return (await this.host()).getProperty<boolean>('disabled');
4848
}
4949

5050
/** Gets whether the option is selected. */
5151
async isSelected(): Promise<boolean> {
52-
return (await this.host()).getProperty('selected');
52+
return (await this.host()).getProperty<boolean>('selected');
5353
}
5454
}

src/material/input/testing/native-select-harness.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,29 @@ export class MatNativeSelectHarness extends MatFormFieldControlHarness {
3131

3232
/** Gets a boolean promise indicating if the select 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
/** Gets a boolean promise indicating if the select 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 a boolean promise indicating if the select is in multi-selection mode. */
4343
async isMultiple(): Promise<boolean> {
44-
return (await this.host()).getProperty('multiple');
44+
return (await this.host()).getProperty<boolean>('multiple');
4545
}
4646

4747
/** Gets the name of the select. */
4848
async getName(): Promise<string> {
4949
// The "name" property of the native select is never undefined.
50-
return (await (await this.host()).getProperty('name'))!;
50+
return (await (await this.host()).getProperty<string>('name'));
5151
}
5252

5353
/** Gets the id of the select. */
5454
async getId(): Promise<string> {
5555
// We're guaranteed to have an id, because the `matNativeControl` always assigns one.
56-
return (await (await this.host()).getProperty('id'))!;
56+
return (await (await this.host()).getProperty<string>('id'));
5757
}
5858

5959
/** Focuses the select and returns a void promise that indicates when the action is complete. */

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export abstract class _MatRadioGroupHarnessBase<
5252

5353
/** Gets the id of the radio-group. */
5454
async getId(): Promise<string|null> {
55-
return (await this.host()).getProperty('id');
55+
return (await this.host()).getProperty<string|null>('id');
5656
}
5757

5858
/** Gets the checked radio-button in a radio-group. */
@@ -185,7 +185,7 @@ export abstract class _MatRadioButtonHarnessBase extends ComponentHarness {
185185

186186
/** Whether the radio-button is checked. */
187187
async isChecked(): Promise<boolean> {
188-
const checked = (await this._input()).getProperty('checked');
188+
const checked = (await this._input()).getProperty<boolean>('checked');
189189
return coerceBooleanProperty(await checked);
190190
}
191191

@@ -208,7 +208,7 @@ export abstract class _MatRadioButtonHarnessBase extends ComponentHarness {
208208

209209
/** Gets the radio-button's id. */
210210
async getId(): Promise<string|null> {
211-
return (await this.host()).getProperty('id');
211+
return (await this.host()).getProperty<string>('id');
212212
}
213213

214214
/**

src/material/slide-toggle/testing/slide-toggle-harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export abstract class _MatSlideToggleHarnessBase extends ComponentHarness {
1919

2020
/** Whether the slide-toggle is checked. */
2121
async isChecked(): Promise<boolean> {
22-
const checked = (await this._input()).getProperty('checked');
22+
const checked = (await this._input()).getProperty<boolean>('checked');
2323
return coerceBooleanProperty(await checked);
2424
}
2525

0 commit comments

Comments
 (0)