Skip to content

fix(material/datepicker): input harness not dispatching dateChange event #20877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ export function runDateRangeInputHarnessTests(

expect(await Promise.all([start.getMax(), end.getMax()])).toEqual(['2020-01-01', '2020-01-01']);
});

it('should dispatch the dateChange event when the inner input values have changed', async () => {
const input = await loader.getHarness(dateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await Promise.all([input.getStartInput(), input.getEndInput()]);

expect(fixture.componentInstance.startDateChangeCount).toBe(0);
expect(fixture.componentInstance.endDateChangeCount).toBe(0);

await Promise.all([start.setValue('1/1/2020'), end.setValue('2/2/2020')]);

expect(fixture.componentInstance.startDateChangeCount).toBe(1);
expect(fixture.componentInstance.endDateChangeCount).toBe(1);
});
}

@Component({
Expand All @@ -209,12 +222,14 @@ export function runDateRangeInputHarnessTests(
<input
matStartDate
[(ngModel)]="startDate"
(dateChange)="startDateChangeCount = startDateChangeCount + 1"
[disabled]="subInputsDisabled"
[required]="subInputsRequired"
placeholder="Start date">
<input
matEndDate
[(ngModel)]="endDate"
(dateChange)="endDateChangeCount = endDateChangeCount + 1"
[disabled]="subInputsDisabled"
[required]="subInputsRequired"
placeholder="End date">
Expand All @@ -237,4 +252,6 @@ class DateRangeInputHarnessTest {
required = false;
subInputsDisabled = false;
subInputsRequired = false;
startDateChangeCount = 0;
endDateChangeCount = 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export abstract class MatDatepickerInputHarnessBase extends ComponentHarness {
if (newValue) {
await inputEl.sendKeys(newValue);
}

// @breaking-change 12.0.0 Remove null check once `dispatchEvent` is a required method.
if (inputEl.dispatchEvent) {
await inputEl.dispatchEvent('change');
}
}

/** Gets the placeholder of the input. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ export function runDatepickerInputHarnessTests(
await input.openCalendar();
expect(await input.getCalendar()).toBeInstanceOf(calendarHarness);
});

it('should emit the `dateChange` event when the value is changed', async () => {
const input = await loader.getHarness(datepickerInputHarness.with({selector: '#basic'}));
expect(fixture.componentInstance.dateChangeCount).toBe(0);

await input.setValue('1/1/2020');
expect(fixture.componentInstance.dateChangeCount).toBe(1);
});
}

@Component({
Expand All @@ -149,6 +157,7 @@ export function runDatepickerInputHarnessTests(
id="basic"
matInput
[matDatepicker]="picker"
(dateChange)="dateChangeCount = dateChangeCount + 1"
[(ngModel)]="date"
[min]="minDate"
[max]="maxDate"
Expand All @@ -166,4 +175,5 @@ class DatepickerInputHarnessTest {
touchUi = false;
disabled = false;
required = false;
dateChangeCount = 0;
}