[ACS-5857] modified code to reduce duplication

This commit is contained in:
SheenaMalhotra182
2023-10-09 23:48:36 +05:30
parent 6877adb5fb
commit 61f8e12e8e
6 changed files with 32 additions and 32 deletions
@@ -148,8 +148,8 @@ export class DateFieldValidator implements FormFieldValidator {
// Validates that the input string is a valid date formatted as <dateFormat> (default D-M-YYYY)
static isValidDate(inputDate: string, dateFormat: string = 'D-M-YYYY'): boolean {
if (inputDate) {
const d = DateFnsUtils.parseDate(inputDate, dateFormat);
return isValid(d);
const date = DateFnsUtils.parseDate(inputDate, dateFormat);
return isValid(date);
}
return false;
@@ -180,8 +180,8 @@ export class DateTimeFieldValidator implements FormFieldValidator {
// Validates that the input string is a valid date formatted as <dateFormat> (default D-M-YYYY)
static isValidDate(inputDate: string, dateFormat: string = 'YYYY-MM-DD HH:mm'): boolean {
if (inputDate) {
const d = DateFnsUtils.parseDate(inputDate, dateFormat);
return isValid(d);
const dateTime = DateFnsUtils.parseDate(inputDate, dateFormat);
return isValid(dateTime);
}
return false;
@@ -1,10 +1,10 @@
<div class="{{field.className}}" id="data-time-widget" [class.adf-invalid]="!field.isValid && isTouched()" [class.adf-left-label-input-container]="field.leftLabels">
<div *ngIf="field.leftLabels">
<label class="adf-label adf-left-label" [attr.for]="field.id">{{formatLabel(field)}}<span class="adf-asterisk" *ngIf="isRequired()">*</span></label>
<label class="adf-label adf-left-label" [attr.for]="field.id">{{formatDateTimeLabel(field)}}<span class="adf-asterisk" *ngIf="isRequired()">*</span></label>
</div>
<div>
<mat-form-field class="adf-date-time-widget" [class.adf-left-label-input-datepicker]="field.leftLabels" [hideRequiredMarker]="true">
<label class="adf-label" *ngIf="!field.leftLabels" [attr.for]="field.id">{{formatLabel(field)}}<span class="adf-asterisk" *ngIf="isRequired()">*</span></label>
<label class="adf-label" *ngIf="!field.leftLabels" [attr.for]="field.id">{{formatDateTimeLabel(field)}}<span class="adf-asterisk" *ngIf="isRequired()">*</span></label>
<input matInput
[id]="field.id"
[value]="field.value"
@@ -348,7 +348,7 @@ describe('DateTimeWidgetComponent', () => {
});
it('should format label correctly', () => {
const result = widget.formatLabel(widget.field);
const result = widget.formatDateTimeLabel(widget.field);
expect(result).toBe('Date Time (D-M-YYYY hh:mm A)');
});
@@ -95,17 +95,17 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit,
this.onDestroy$.complete();
}
formatLabel(field: FormFieldModel): string {
const displayName = this.translationService.instant(field.name);
const displayFormat = DateFnsUtils.convertDateFnsToMomentFormat(field.dateDisplayFormat);
formatDateTimeLabel(field: FormFieldModel): string {
const dateTimeDisplayName = this.translationService.instant(field.name);
const dateTimeDisplayFormat = DateFnsUtils.convertDateFnsToMomentFormat(field.dateDisplayFormat);
return `${displayName} (${displayFormat})`;
return `${dateTimeDisplayName} (${dateTimeDisplayFormat})`;
}
onDateChanged(newDateValue) {
const date = new Date(newDateValue);
if (isValid(date)) {
this.field.value = DateFnsUtils.formatDate(date, this.field.dateDisplayFormat);
const dateTimeValue = new Date(newDateValue);
if (isValid(dateTimeValue)) {
this.field.value = DateFnsUtils.formatDate(dateTimeValue, this.field.dateDisplayFormat);
} else {
this.field.value = newDateValue;
}
@@ -62,9 +62,9 @@ describe('DateWidgetComponent', () => {
widget.ngOnInit();
const expected = parse(minValue, widget.field.dateDisplayFormat, new Date());
const expectedMinDate = parse(minValue, widget.field.dateDisplayFormat, new Date());
const widgetDate = parse(widget.minDate, widget.field.dateDisplayFormat, new Date());
expect(isSameDay(widgetDate, expected)).toBeTruthy();
expect(isSameDay(widgetDate, expectedMinDate)).toBeTruthy();
});
it('should date field be present', () => {
@@ -86,9 +86,9 @@ describe('DateWidgetComponent', () => {
});
widget.ngOnInit();
const expected = parse(maxValue, widget.field.dateDisplayFormat, new Date());
const expectedMaxDate = parse(maxValue, widget.field.dateDisplayFormat, new Date());
const widgetDate = parse(widget.maxDate, widget.field.dateDisplayFormat, new Date());
expect(isSameDay(widgetDate, expected)).toBeTruthy();
expect(isSameDay(widgetDate, expectedMaxDate)).toBeTruthy();
});
it('should eval visibility on date changed', () => {
@@ -262,7 +262,7 @@ describe('DateWidgetComponent', () => {
});
it('should format label correctly', () => {
const result = widget.formatLabel(widget.field);
const result = widget.formatDateLabel(widget.field);
expect(result).toBe('Date (D-M-YYYY)');
});
+13 -13
View File
@@ -75,18 +75,13 @@ describe('ADFDatePipe', () => {
it('should return the formatted date string when given a valid date string', () => {
const inputDate = '2023-08-14';
let dateFormat = 'DD-MM-YYYY';
let expectedOutput = '14-08-2023';
let dateFormat = 'DD-MM-YY';
let expectedOutput = '14-08-23';
let result = datePipe.transform(inputDate, dateFormat);
expect(result).toBe(expectedOutput);
dateFormat = 'MM-DD-YYYY';
expectedOutput = '08-14-2023';
result = datePipe.transform(inputDate, dateFormat);
expect(result).toBe(expectedOutput);
dateFormat = 'YYYY-MM-DD';
expectedOutput = '2023-08-14';
dateFormat = 'MM-DD-YY';
expectedOutput = '08-14-23';
result = datePipe.transform(inputDate, dateFormat);
expect(result).toBe(expectedOutput);
@@ -95,13 +90,18 @@ describe('ADFDatePipe', () => {
result = datePipe.transform(inputDate, dateFormat);
expect(result).toBe(expectedOutput);
dateFormat = 'MM-DD-YY';
expectedOutput = '08-14-23';
dateFormat = 'YYYY-MM-DD';
expectedOutput = '2023-08-14';
result = datePipe.transform(inputDate, dateFormat);
expect(result).toBe(expectedOutput);
dateFormat = 'DD-MM-YY';
expectedOutput = '14-08-23';
dateFormat = 'MM-DD-YYYY';
expectedOutput = '08-14-2023';
result = datePipe.transform(inputDate, dateFormat);
expect(result).toBe(expectedOutput);
dateFormat = 'DD-MM-YYYY';
expectedOutput = '14-08-2023';
result = datePipe.transform(inputDate, dateFormat);
expect(result).toBe(expectedOutput);
});