mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
[ACS-5857] addressed review comments
This commit is contained in:
@@ -111,7 +111,10 @@ export class DateFnsUtils {
|
||||
*/
|
||||
static convertMomentToDateFnsFormat(dateDisplayFormat: string): string {
|
||||
// Check if 'A' is present in the format string
|
||||
const containsA = dateDisplayFormat.includes('A');
|
||||
let containsA;
|
||||
if (dateDisplayFormat) {
|
||||
containsA = dateDisplayFormat.includes('A');
|
||||
}
|
||||
|
||||
// Replace 'HH' with 'hh' if 'A' is also present
|
||||
if (containsA) {
|
||||
@@ -164,8 +167,11 @@ export class DateFnsUtils {
|
||||
* @param dateFormat - The date format string to use for parsing.
|
||||
* @returns The parsed Date object.
|
||||
*/
|
||||
static parseDate(value: string, dateFormat: string): Date {
|
||||
return parse(value, this.convertMomentToDateFnsFormat(dateFormat), new Date());
|
||||
static parseDate(date: Date | string, dateFormat: string): Date {
|
||||
if (date instanceof Date) {
|
||||
date = date.toISOString();
|
||||
}
|
||||
return parse(date, this.convertMomentToDateFnsFormat(dateFormat), new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -710,7 +710,7 @@ describe('FormFieldValidator', () => {
|
||||
const maxValueFromActivitiInput = '31-3-2018 12:00 AM';
|
||||
const maxValueSavedInForm = DateFnsUtils.parseDate(maxValueFromActivitiInput, 'DD-M-YYYY hh:mm A').toISOString();
|
||||
|
||||
const localValidValue = new Date('2018-3-30 11:59 PM').toISOString();
|
||||
const localValidValue = '2018-3-30 11:59 PM';
|
||||
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATETIME,
|
||||
@@ -725,7 +725,7 @@ describe('FormFieldValidator', () => {
|
||||
const maxValueFromActivitiInput = '31-3-2018 12:00 AM';
|
||||
const maxValueSavedInForm = DateFnsUtils.parseDate(maxValueFromActivitiInput, 'DD-M-YYYY hh:mm A').toISOString();
|
||||
|
||||
const localInvalidValue = new Date('2018-3-31 12:01 AM').toISOString();
|
||||
const localInvalidValue = '2018-3-31 12:01 AM';
|
||||
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATETIME,
|
||||
@@ -835,7 +835,7 @@ describe('FormFieldValidator', () => {
|
||||
const minValueFromActivitiInput = '02-3-2018 06:00 AM';
|
||||
const minValueSavedInForm = DateFnsUtils.parseDate(minValueFromActivitiInput, 'DD-M-YYYY hh:mm A').toISOString();
|
||||
|
||||
const localValidValue = new Date('2018-3-02 06:01 AM').toISOString();
|
||||
const localValidValue = '2018-3-02 06:01 AM';
|
||||
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATETIME,
|
||||
@@ -850,7 +850,7 @@ describe('FormFieldValidator', () => {
|
||||
const minValueFromActivitiInput = '02-3-2018 06:00 AM';
|
||||
const minValueSavedInForm = DateFnsUtils.parseDate(minValueFromActivitiInput, 'DD-M-YYYY hh:mm A').toISOString();
|
||||
|
||||
const localInvalidValue = new Date('2018-3-02 05:59 AM').toISOString();
|
||||
const localInvalidValue = '2018-3-02 05:59 AM';
|
||||
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATETIME,
|
||||
|
||||
@@ -28,7 +28,7 @@ import { ProcessFormModel } from './process-form-model.interface';
|
||||
import { isNumberValue } from './form-field-utils';
|
||||
import { VariableConfig } from './form-field-variable-options';
|
||||
import { DataColumn } from '../../../../datatable/data/data-column.model';
|
||||
import { isValid, parseISO } from 'date-fns';
|
||||
import { isValid } from 'date-fns';
|
||||
import { DateFnsUtils } from '../../../../common/utils/date-fns-utils';
|
||||
|
||||
// Maps to FormFieldRepresentation
|
||||
@@ -190,7 +190,7 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
this.visibilityCondition = json.visibilityCondition ? new WidgetVisibilityModel(json.visibilityCondition) : undefined;
|
||||
this.enableFractions = json.enableFractions;
|
||||
this.currency = json.currency;
|
||||
this.dateDisplayFormat = json.dateDisplayFormat || this.getDefaultDateFormat(json);
|
||||
this.dateDisplayFormat = DateFnsUtils.convertMomentToDateFnsFormat(json.dateDisplayFormat) || this.getDefaultDateFormat(json);
|
||||
this._value = this.parseValue(json);
|
||||
this.validationSummary = new ErrorMessageModel();
|
||||
this.tooltip = json.tooltip;
|
||||
@@ -354,7 +354,7 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
if (isNumberValue(value)) {
|
||||
dateValue = new Date(value);
|
||||
} else {
|
||||
dateValue = parseISO(value);
|
||||
dateValue = DateFnsUtils.parseDate(value, 'YYYY-MM-DD hh:mm A');
|
||||
}
|
||||
if (isValid(dateValue)) {
|
||||
value = DateFnsUtils.formatDate(dateValue, this.dateDisplayFormat);
|
||||
@@ -368,7 +368,7 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
if (isNumberValue(value)) {
|
||||
dateValue = new Date(value);
|
||||
} else {
|
||||
dateValue = parseISO(value);
|
||||
dateValue = DateFnsUtils.parseDate(value.split('T')[0], 'YYYY-M-D');
|
||||
}
|
||||
if (isValid(dateValue)) {
|
||||
value = DateFnsUtils.formatDate(dateValue, this.dateDisplayFormat);
|
||||
|
||||
@@ -165,7 +165,7 @@ describe('DateWidgetComponent', () => {
|
||||
readOnly: 'false'
|
||||
});
|
||||
widget.field.isVisible = true;
|
||||
widget.field.dateDisplayFormat = DateFnsUtils.convertMomentToDateFnsFormat('MM-DD-YYYY');
|
||||
widget.field.dateDisplayFormat = 'MM-DD-YYYY';
|
||||
|
||||
fixture.detectChanges();
|
||||
await fixture.whenStable();
|
||||
@@ -174,7 +174,7 @@ describe('DateWidgetComponent', () => {
|
||||
expect(dateElement?.value).toContain('12-30-9999');
|
||||
|
||||
widget.field.value = '05.06.2019';
|
||||
widget.field.dateDisplayFormat = DateFnsUtils.convertMomentToDateFnsFormat('DD.MM.YYYY');
|
||||
widget.field.dateDisplayFormat = 'DD.MM.YYYY';
|
||||
|
||||
fixture.componentInstance.ngOnInit();
|
||||
fixture.detectChanges();
|
||||
@@ -233,7 +233,7 @@ describe('DateWidgetComponent', () => {
|
||||
});
|
||||
|
||||
field.isVisible = true;
|
||||
field.dateDisplayFormat = DateFnsUtils.convertMomentToDateFnsFormat('MM-DD-YYYY');
|
||||
field.dateDisplayFormat = 'MM-DD-YYYY';
|
||||
widget.field = field;
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
@@ -21,6 +21,15 @@ import { ADFDatePipe } from './date.pipe';
|
||||
describe('ADFDatePipe', () => {
|
||||
let datePipe: ADFDatePipe;
|
||||
|
||||
const dateFormats = [
|
||||
{ dateFormat: 'yyyy-MM-dd' },
|
||||
{ dateFormat: 'yyyy-M-d' },
|
||||
{ dateFormat: 'dd-MM-yyyy' },
|
||||
{ dateFormat: 'd-M-yyyy' },
|
||||
{ dateFormat: 'M-d-yyyy' },
|
||||
{ dateFormat: 'MM-d-yyyy' }
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [ADFDatePipe]
|
||||
@@ -29,53 +38,22 @@ describe('ADFDatePipe', () => {
|
||||
});
|
||||
|
||||
it('should transform a valid date string into a Date object', () => {
|
||||
const testDates = [
|
||||
{ dateString: '2023-10-05', dateFormat: 'yyyy-MM-dd' },
|
||||
{ dateString: '2023-10-5', dateFormat: 'yyyy-M-d' },
|
||||
{ dateString: '05-10-2023', dateFormat: 'dd-MM-yyyy' },
|
||||
{ dateString: '5-10-2023', dateFormat: 'd-M-yyyy' },
|
||||
{ dateString: '10-5-2023', dateFormat: 'M-d-yyyy' },
|
||||
{ dateString: '10-05-2023', dateFormat: 'MM-d-yyyy' }
|
||||
];
|
||||
testDates.forEach(({ dateString, dateFormat }) => {
|
||||
const transformedDate = datePipe.transform(dateString, dateFormat);
|
||||
const inputDate = '2023-10-05';
|
||||
|
||||
dateFormats.forEach(({ dateFormat }) => {
|
||||
const transformedDate = datePipe.transform(inputDate, dateFormat);
|
||||
|
||||
expect(transformedDate instanceof Date).toBe(true);
|
||||
expect(transformedDate.getFullYear()).toBe(2023);
|
||||
expect(transformedDate.getMonth()).toBe(9); // October is 9 (0-based)
|
||||
expect(transformedDate.getDate()).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
it('should transform a valid date object into a Date object', () => {
|
||||
const testDates = [
|
||||
{ dateString: new Date(), dateFormat: 'yyyy-MM-dd' },
|
||||
{ dateString: new Date(), dateFormat: 'yyyy-M-d' },
|
||||
{ dateString: new Date(), dateFormat: 'dd-MM-yyyy' },
|
||||
{ dateString: new Date(), dateFormat: 'd-M-yyyy' },
|
||||
{ dateString: new Date(), dateFormat: 'M-d-yyyy' },
|
||||
{ dateString: new Date(), dateFormat: 'MM-d-yyyy' }
|
||||
];
|
||||
const inputDate = new Date('2023-10-05');
|
||||
|
||||
testDates.forEach(({ dateString, dateFormat }) => {
|
||||
const transformedDate = datePipe.transform(dateString, dateFormat);
|
||||
const today = new Date();
|
||||
dateFormats.forEach(({ dateFormat }) => {
|
||||
const transformedDate = datePipe.transform(inputDate, dateFormat);
|
||||
|
||||
expect(transformedDate instanceof Date).toBe(true);
|
||||
expect(transformedDate.getFullYear()).toBe(today.getFullYear());
|
||||
expect(transformedDate.getMonth()).toBe(today.getMonth()); // October is 9 (0-based)
|
||||
expect(transformedDate.getDate()).toBe(today.getDate());
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle undefined input by returning the current date', () => {
|
||||
const undefinedInput = undefined;
|
||||
const dateFormat = 'dd-MM-yyyy';
|
||||
|
||||
const transformedDate = datePipe.transform(undefinedInput, dateFormat);
|
||||
const today = new Date();
|
||||
|
||||
expect(transformedDate instanceof Date).toBe(true);
|
||||
expect(transformedDate).toEqual(today);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,17 +17,12 @@
|
||||
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { DateFnsUtils } from '../common/utils/date-fns-utils';
|
||||
import { isDate } from 'date-fns';
|
||||
import { isValid } from 'date-fns';
|
||||
|
||||
@Pipe({ name: 'adfDate' })
|
||||
export class ADFDatePipe implements PipeTransform {
|
||||
transform(value: Date | string, dateFormat: string): Date {
|
||||
if (value) {
|
||||
const date = typeof value === 'string' ? DateFnsUtils.parseDate(value, dateFormat) : value;
|
||||
if (isDate(date)) {
|
||||
return date;
|
||||
}
|
||||
}
|
||||
return new Date();
|
||||
const date = value instanceof Date ? value : new Date(value);
|
||||
return isValid(date) ? DateFnsUtils.parseDate(date, dateFormat) : date;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -119,7 +119,7 @@ describe('DateWidgetComponent', () => {
|
||||
expect(element.querySelector('#date-field-id')).toBeDefined();
|
||||
expect(element.querySelector('#date-field-id')).not.toBeNull();
|
||||
const dateElement = element.querySelector<HTMLInputElement>('#date-field-id');
|
||||
expect(dateElement.value).toContain('9999-9-9');
|
||||
expect(dateElement.value).toContain('9-9-9999');
|
||||
});
|
||||
|
||||
it('should show the correct format type', async () => {
|
||||
@@ -131,7 +131,7 @@ describe('DateWidgetComponent', () => {
|
||||
readOnly: 'false'
|
||||
});
|
||||
widget.field.isVisible = true;
|
||||
widget.field.dateDisplayFormat = DateFnsUtils.convertMomentToDateFnsFormat('YYYY-DD-MM');
|
||||
widget.field.dateDisplayFormat = 'YYYY-DD-MM';
|
||||
widget.ngOnInit();
|
||||
fixture.detectChanges();
|
||||
await fixture.whenStable();
|
||||
@@ -192,7 +192,7 @@ describe('DateWidgetComponent', () => {
|
||||
readOnly: 'false'
|
||||
});
|
||||
field.isVisible = true;
|
||||
field.dateDisplayFormat = DateFnsUtils.convertMomentToDateFnsFormat('MM-DD-YYYY');
|
||||
field.dateDisplayFormat = 'MM-DD-YYYY';
|
||||
widget.field = field;
|
||||
widget.ngOnInit();
|
||||
fixture.detectChanges();
|
||||
|
||||
Reference in New Issue
Block a user