Skip to content

Commit

Permalink
fix(input-directive): non-date input did not set ng-valid after model…
Browse files Browse the repository at this point in the history
… is set to valid date

fix #448 edge case for non-date input
  • Loading branch information
dalelotts committed Nov 17, 2019
1 parent 6193c38 commit beb4308
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/dl-date-time-input/dl-date-time-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export class DlDateTimeInputDirective<D> implements ControlValueAccessor, Valida
* @internal
*/
writeValue(value: D): void {
this._isValid = true;
this.value = value;
this._setElementValue(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ describe('DlDateTimeInputDirective', () => {
});

it('should remove ng-invalid when model is updated with valid date', fakeAsync(() => {
// This is to fix #448, inputting a value that is a disallowed date (but a valid date)
// should change to ng-valid when the model is updated to an allowed date.

const allowedValue = moment('2019-10-29T17:00').valueOf();
spyOn(component, 'dateTimeFilter').and.callFake((date: number) => {
return date === allowedValue;
Expand All @@ -233,6 +236,35 @@ describe('DlDateTimeInputDirective', () => {
expect(inputElement.classList).toContain('ng-valid');
}));

it('should add ng-invalid for non-date input and remove ng-invalid after when model is updated with valid date', fakeAsync(() => {
// This is to fix #448, inputting a completely invalid date value (i.e not a date at all)
// should change to ng-valid when the model is updated to an allowed date.

const allowedValue = moment('2019-10-29T17:00').valueOf();
spyOn(component, 'dateTimeFilter').and.callFake((date: number) => {
return date === allowedValue;
});

const inputElement = debugElement.query(By.directive(DlDateTimeInputDirective)).nativeElement;
inputElement.value = 'very-invalid-date';
inputElement.dispatchEvent(new Event('input'));

fixture.detectChanges();

expect(inputElement.classList).toContain('ng-invalid');

inputElement.dispatchEvent(new Event('blur'));
fixture.detectChanges();

component.dateValue = allowedValue;

fixture.detectChanges();
tick();
fixture.detectChanges();

expect(inputElement.classList).toContain('ng-valid');
}));

it('should disable input when setDisabled is called', () => {
const inputElement = debugElement.query(By.directive(DlDateTimeInputDirective)).nativeElement;
expect(inputElement.disabled).toBe(false);
Expand Down

0 comments on commit beb4308

Please sign in to comment.