Skip to content

Commit 1992e16

Browse files
authored
refactor(cdk/testing): change deprecated APIs for v12 (#21809)
Changes the APIs that were marked as deprecated for v12.0.0. BREAKING CHANGES: * `TestElement.rightClick` is now a required method. * `TestElement.setInputValue` is now a required method. * `TestElement.selectOptions` is now a required method. * `TestElement.dispatchEvent` is now a required method.
1 parent 62d2e37 commit 1992e16

File tree

6 files changed

+17
-38
lines changed

6 files changed

+17
-38
lines changed

src/cdk/testing/test-element.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ export interface TestElement {
9494
* @param relativeX Coordinate within the element, along the X-axis at which to click.
9595
* @param relativeY Coordinate within the element, along the Y-axis at which to click.
9696
* @param modifiers Modifier keys held while clicking
97-
* @breaking-change 11.0.0 To become a required method.
9897
*/
99-
rightClick?(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
98+
rightClick(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
10099

101100
/** Focus the element. */
102101
focus(): Promise<void>;
@@ -148,26 +147,23 @@ export interface TestElement {
148147

149148
/**
150149
* Sets the value of a property of an input.
151-
* @breaking-change 11.0.0 To become a required method.
152150
*/
153-
setInputValue?(value: string): Promise<void>;
151+
setInputValue(value: string): Promise<void>;
154152

155153
// Note that ideally here we'd be selecting options based on their value, rather than their
156154
// index, but we're limited by `@angular/forms` which will modify the option value in some cases.
157155
// Since the value will be truncated, we can't rely on it to do the lookup in the DOM. See:
158156
// https://github.com/angular/angular/blob/master/packages/forms/src/directives/select_control_value_accessor.ts#L19
159157
/**
160158
* Selects the options at the specified indexes inside of a native `select` element.
161-
* @breaking-change 12.0.0 To become a required method.
162159
*/
163-
selectOptions?(...optionIndexes: number[]): Promise<void>;
160+
selectOptions(...optionIndexes: number[]): Promise<void>;
164161

165162
/**
166163
* Dispatches an event with a particular name.
167164
* @param name Name of the event to be dispatched.
168-
* @breaking-change 12.0.0 To be a required method.
169165
*/
170-
dispatchEvent?(name: string, data?: Record<string, EventData>): Promise<void>;
166+
dispatchEvent(name: string, data?: Record<string, EventData>): Promise<void>;
171167
}
172168

173169
export interface TextOptions {

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,14 @@ export function crossEnvironmentSpecs(
369369
it('should be able to right click at a specific position within an element', async () => {
370370
const clickTest = await harness.clickTest();
371371
const contextmenuTestResult = await harness.contextmenuTestResult();
372-
await clickTest.rightClick!(50, 50);
372+
await clickTest.rightClick(50, 50);
373373
expect(await contextmenuTestResult.text()).toBe('50-50-2');
374374
});
375375

376376
it('should be able to right click with modifiers', async () => {
377377
const clickTest = await harness.clickTest();
378378
const modifiersResult = await harness.clickModifiersResult();
379-
await clickTest.rightClick!(50, 50, {alt: true, control: true});
379+
await clickTest.rightClick(50, 50, {alt: true, control: true});
380380
expect(await modifiersResult.text()).toBe('-alt-control-');
381381
});
382382

@@ -455,8 +455,7 @@ export function crossEnvironmentSpecs(
455455
it('should be able to set the value of an input', async () => {
456456
const input = await harness.input();
457457

458-
// @breaking-change 11.0.0 Remove non-null assertion once `setInputValue` is required.
459-
await input.setInputValue!('hello');
458+
await input.setInputValue('hello');
460459
expect(await input.getProperty('value')).toBe('hello');
461460
});
462461

@@ -467,8 +466,7 @@ export function crossEnvironmentSpecs(
467466
harness.singleSelectChangeEventCounter()
468467
]);
469468

470-
// @breaking-change 12.0.0 Remove non-null assertion once `setSelectValue` is required.
471-
await select.selectOptions!(2);
469+
await select.selectOptions(2);
472470
expect(await value.text()).toBe('Select: three');
473471
expect(await changeEventCounter.text()).toBe('Change events: 1');
474472
});
@@ -480,8 +478,7 @@ export function crossEnvironmentSpecs(
480478
harness.multiSelectChangeEventCounter()
481479
]);
482480

483-
// @breaking-change 12.0.0 Remove non-null assertion once `setSelectValue` is required.
484-
await select.selectOptions!(0, 2);
481+
await select.selectOptions(0, 2);
485482
expect(await value.text()).toBe('Multi-select: one,three');
486483
expect(await changeEventCounter.text()).toBe('Change events: 2');
487484
});
@@ -503,16 +500,14 @@ export function crossEnvironmentSpecs(
503500
it('should dispatch a basic custom event', async () => {
504501
const target = await harness.customEventBasic();
505502

506-
// @breaking-change 12.0.0 Remove non-null assertion once `dispatchEvent` is required.
507-
await target.dispatchEvent!('myCustomEvent');
503+
await target.dispatchEvent('myCustomEvent');
508504
expect(await target.text()).toBe('Basic event: 1');
509505
});
510506

511507
it('should dispatch a custom event with attached data', async () => {
512508
const target = await harness.customEventObject();
513509

514-
// @breaking-change 12.0.0 Remove non-null assertion once `dispatchEvent` is required.
515-
await target.dispatchEvent!('myCustomEvent', {message: 'Hello', value: 1337});
510+
await target.dispatchEvent('myCustomEvent', {message: 'Hello', value: 1337});
516511
expect(await target.text()).toBe('Event with object: {"message":"Hello","value":1337}');
517512
});
518513

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ export abstract class MatDatepickerInputHarnessBase extends ComponentHarness {
6060
await inputEl.sendKeys(newValue);
6161
}
6262

63-
// @breaking-change 12.0.0 Remove null check once `dispatchEvent` is a required method.
64-
if (inputEl.dispatchEvent) {
65-
await inputEl.dispatchEvent('change');
66-
}
63+
await inputEl.dispatchEvent('change');
6764
}
6865

6966
/** Gets the placeholder of the input. */

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ export class MatInputHarness extends MatFormFieldControlHarness {
124124
// Some input types won't respond to key presses (e.g. `color`) so to be sure that the
125125
// value is set, we also set the property after the keyboard sequence. Note that we don't
126126
// want to do it before, because it can cause the value to be entered twice.
127-
// @breaking-change 11.0.0 Remove non-null assertion once `setInputValue` is required.
128-
if (inputEl.setInputValue) {
129-
await inputEl.setInputValue(newValue);
130-
}
127+
await inputEl.setInputValue(newValue);
131128
}
132129
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ export class MatNativeSelectHarness extends MatFormFieldControlHarness {
9595
parallel(() => options.slice(0, isMultiple ? undefined : 1).map(option => option.getIndex()))
9696
]);
9797

98-
// @breaking-change 12.0.0 Error can be removed once `selectOptions` is a required method.
99-
if (!host.selectOptions) {
100-
throw Error('TestElement implementation does not support the selectOptions ' +
101-
'method which is required for this function.');
102-
}
103-
10498
await host.selectOptions(...optionIndexes);
10599
}
106100
}

tools/public_api_guard/cdk/testing.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export interface TestElement {
154154
click(modifiers?: ModifierKeys): Promise<void>;
155155
click(location: 'center', modifiers?: ModifierKeys): Promise<void>;
156156
click(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
157-
dispatchEvent?(name: string, data?: Record<string, EventData>): Promise<void>;
157+
dispatchEvent(name: string, data?: Record<string, EventData>): Promise<void>;
158158
focus(): Promise<void>;
159159
getAttribute(name: string): Promise<string | null>;
160160
getCssValue(property: string): Promise<string>;
@@ -165,11 +165,11 @@ export interface TestElement {
165165
isFocused(): Promise<boolean>;
166166
matchesSelector(selector: string): Promise<boolean>;
167167
mouseAway(): Promise<void>;
168-
rightClick?(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
169-
selectOptions?(...optionIndexes: number[]): Promise<void>;
168+
rightClick(relativeX: number, relativeY: number, modifiers?: ModifierKeys): Promise<void>;
169+
selectOptions(...optionIndexes: number[]): Promise<void>;
170170
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
171171
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
172-
setInputValue?(value: string): Promise<void>;
172+
setInputValue(value: string): Promise<void>;
173173
text(options?: TextOptions): Promise<string>;
174174
}
175175

0 commit comments

Comments
 (0)