Skip to content

Commit

Permalink
fix(datefield): parseError when input is empty (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopy-cat authored and GitHub Enterprise committed Jan 8, 2021
1 parent 079d64d commit d9be564
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions projects/ng-aquila/src/datefield/date-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export class NxDateValidators {
strict: boolean,
customParseFormat: string | string[] = null): ValidatorFn {
return (): ValidationErrors | null => {
if (!input.value) {
return null;
}
const parsedValue = dateAdapter.parse(input.value, customParseFormat || dateFormats.parse.dateInput, strict);
const valid = parsedValue && dateAdapter.isValid(parsedValue);
return valid ? null : {'nxDatefieldParse': {'text': input}};
Expand Down
19 changes: 18 additions & 1 deletion projects/ng-aquila/src/datefield/datefield.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ describe('NxDatefieldDirective with Moment', () => {
expect(nativeElement.value).toBe('2018/01/01');
}));

it('should have no error if input value is empty', () => {
createTestComponent(ReactiveDatefield);
nativeElement.value = '';
nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(testInstance.form.get('datefield').valid).toBe(true);
});

it('should have no error on custom date format', () => {
createTestComponent(ReactiveDatefield);
testInstance.displayFormat = 'MM--DD--YYYY';
Expand Down Expand Up @@ -378,12 +386,21 @@ describe('NxDatefieldDirective with IsoAdapter', () => {
it('has an parsing error for an incorrect date', () => {
createTestComponent(ReactiveIsoDatefield);
const datefield = testInstance.form.get('datefield');
datefield.patchValue('this is no date');
nativeElement.value = 'this is no date';
nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(datefield.valid).toBeFalse();
expect(datefield.errors['nxDatefieldParse']).toBeDefined();
});

it('has no error if input is empty', () => {
createTestComponent(ReactiveIsoDatefield);
nativeElement.value = '';
nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(testInstance.form.get('datefield').valid).toBeTrue();
});

});


Expand Down

0 comments on commit d9be564

Please sign in to comment.