Skip to content

Commit 1630129

Browse files
crisbetoannieyw
authored andcommitted
fix(material/datepicker): input harness not dispatching dateChange event (#20877)
When the datepicker input harnesses were first introduced, they didn't dispatch the `dateChange` event, because we didn't have a way of triggering the `change` handler. Now that we have the necessary API, these changes make it so the event is dispatched. Fixes #20480. (cherry picked from commit 513bfa4)
1 parent 7226f21 commit 1630129

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/material/datepicker/testing/date-range-input-harness-shared.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ export function runDateRangeInputHarnessTests(
195195

196196
expect(await Promise.all([start.getMax(), end.getMax()])).toEqual(['2020-01-01', '2020-01-01']);
197197
});
198+
199+
it('should dispatch the dateChange event when the inner input values have changed', async () => {
200+
const input = await loader.getHarness(dateRangeInputHarness.with({selector: '[basic]'}));
201+
const [start, end] = await Promise.all([input.getStartInput(), input.getEndInput()]);
202+
203+
expect(fixture.componentInstance.startDateChangeCount).toBe(0);
204+
expect(fixture.componentInstance.endDateChangeCount).toBe(0);
205+
206+
await Promise.all([start.setValue('1/1/2020'), end.setValue('2/2/2020')]);
207+
208+
expect(fixture.componentInstance.startDateChangeCount).toBe(1);
209+
expect(fixture.componentInstance.endDateChangeCount).toBe(1);
210+
});
198211
}
199212

200213
@Component({
@@ -209,12 +222,14 @@ export function runDateRangeInputHarnessTests(
209222
<input
210223
matStartDate
211224
[(ngModel)]="startDate"
225+
(dateChange)="startDateChangeCount = startDateChangeCount + 1"
212226
[disabled]="subInputsDisabled"
213227
[required]="subInputsRequired"
214228
placeholder="Start date">
215229
<input
216230
matEndDate
217231
[(ngModel)]="endDate"
232+
(dateChange)="endDateChangeCount = endDateChangeCount + 1"
218233
[disabled]="subInputsDisabled"
219234
[required]="subInputsRequired"
220235
placeholder="End date">
@@ -237,4 +252,6 @@ class DateRangeInputHarnessTest {
237252
required = false;
238253
subInputsDisabled = false;
239254
subInputsRequired = false;
255+
startDateChangeCount = 0;
256+
endDateChangeCount = 0;
240257
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export abstract class MatDatepickerInputHarnessBase extends ComponentHarness {
5959
if (newValue) {
6060
await inputEl.sendKeys(newValue);
6161
}
62+
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+
}
6267
}
6368

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

src/material/datepicker/testing/datepicker-input-harness-shared.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ export function runDatepickerInputHarnessTests(
141141
await input.openCalendar();
142142
expect(await input.getCalendar()).toBeInstanceOf(calendarHarness);
143143
});
144+
145+
it('should emit the `dateChange` event when the value is changed', async () => {
146+
const input = await loader.getHarness(datepickerInputHarness.with({selector: '#basic'}));
147+
expect(fixture.componentInstance.dateChangeCount).toBe(0);
148+
149+
await input.setValue('1/1/2020');
150+
expect(fixture.componentInstance.dateChangeCount).toBe(1);
151+
});
144152
}
145153

146154
@Component({
@@ -149,6 +157,7 @@ export function runDatepickerInputHarnessTests(
149157
id="basic"
150158
matInput
151159
[matDatepicker]="picker"
160+
(dateChange)="dateChangeCount = dateChangeCount + 1"
152161
[(ngModel)]="date"
153162
[min]="minDate"
154163
[max]="maxDate"
@@ -166,4 +175,5 @@ class DatepickerInputHarnessTest {
166175
touchUi = false;
167176
disabled = false;
168177
required = false;
178+
dateChangeCount = 0;
169179
}

0 commit comments

Comments
 (0)