Skip to content

Commit

Permalink
[APPS-2108] migrate unit tests to date-fns (#8991)
Browse files Browse the repository at this point in the history
* migrate unit tests

* [ci:force] cleanup
  • Loading branch information
DenysVuika authored Oct 11, 2023
1 parent 03a52dc commit 96d0a61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
} from './form-field-validator';
import { FormFieldModel } from './form-field.model';
import { FormModel } from './form.model';
declare let moment: any;

describe('FormFieldValidator', () => {
describe('RequiredFieldValidator', () => {
Expand Down Expand Up @@ -707,30 +706,24 @@ describe('FormFieldValidator', () => {
});

it('should take into account that max value is in UTC and NOT fail validating value checking the time', () => {
const maxValueFromActivitiInput = '31-3-2018 12:00 AM';
const maxValueSavedInForm = moment(maxValueFromActivitiInput, 'DD-M-YYYY hh:mm A').utc().format();

const localValidValue = '2018-3-30 11:59 PM';

const field = new FormFieldModel(new FormModel(), {
type: FormFieldTypes.DATETIME,
value: localValidValue,
maxValue: maxValueSavedInForm
maxValue: '2018-03-31T23:00:00.000Z'
});

expect(validator.validate(field)).toBeTruthy();
});

it('should take into account that max value is in UTC and fail validating value checking the time', () => {
const maxValueFromActivitiInput = '31-3-2018 12:00 AM';
const maxValueSavedInForm = moment(maxValueFromActivitiInput, 'DD-M-YYYY hh:mm A').utc().format();

const localInvalidValue = '2018-3-31 12:01 AM';

const field = new FormFieldModel(new FormModel(), {
type: FormFieldTypes.DATETIME,
value: localInvalidValue,
maxValue: maxValueSavedInForm
maxValue: `2018-03-30T23:00:00.000Z`
});

field.validationSummary = new ErrorMessageModel();
Expand Down Expand Up @@ -832,30 +825,24 @@ describe('FormFieldValidator', () => {
});

it('should take into account that min value is in UTC and NOT fail validating value checking the time', () => {
const minValueFromActivitiInput = '02-3-2018 06:00 AM';
const minValueSavedInForm = moment(minValueFromActivitiInput, 'DD-M-YYYY hh:mm A').utc().format();

const localValidValue = '2018-3-02 06:01 AM';

const field = new FormFieldModel(new FormModel(), {
type: FormFieldTypes.DATETIME,
value: localValidValue,
minValue: minValueSavedInForm
minValue: '2018-03-02T06:00:00+00:00'
});

expect(validator.validate(field)).toBeTruthy();
});

it('should take into account that min value is in UTC and fail validating value checking the time', () => {
const minValueFromActivitiInput = '02-3-2018 06:00 AM';
const minValueSavedInForm = moment(minValueFromActivitiInput, 'DD-M-YYYY hh:mm A').utc().format();

const localInvalidValue = '2018-3-02 05:59 AM';

const field = new FormFieldModel(new FormModel(), {
type: FormFieldTypes.DATETIME,
value: localInvalidValue,
minValue: minValueSavedInForm
minValue: '2018-03-02T06:00:00+00:00'
});

field.validationSummary = new ErrorMessageModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import moment from 'moment';
import { DateFnsUtils } from '../../../../common';
import { FormFieldTypes } from './form-field-types';
import { FormFieldModel } from './form-field.model';
import { FormModel } from './form.model';
Expand Down Expand Up @@ -273,9 +273,9 @@ describe('FormFieldModel', () => {
dateDisplayFormat: 'DD-MM-YYYY'
});

const currentDate = moment(new Date());
const expectedDate = moment(currentDate).format('DD-MM-YYYY');
const expectedDateFormat = `${currentDate.format('YYYY-MM-DD')}T00:00:00.000Z`;
const currentDate = new Date();
const expectedDate = DateFnsUtils.formatDate(currentDate, 'dd-MM-yyyy');
const expectedDateFormat = `${DateFnsUtils.formatDate(currentDate, 'yyyy-MM-dd')}T00:00:00.000Z`;

expect(field.value).toBe(expectedDate);
expect(form.values['ddmmyyy']).toEqual(expectedDateFormat);
Expand Down Expand Up @@ -304,9 +304,9 @@ describe('FormFieldModel', () => {
dateDisplayFormat: 'YYYY-MM-DD HH:mm'
});

const currentDateTime = moment(new Date());
const expectedDateTime = moment.utc(currentDateTime).format('YYYY-MM-DD HH:mm');
const expectedDateTimeFormat = `${currentDateTime.utc().format('YYYY-MM-DDTHH:mm:00')}.000Z`;
const currentDateTime = new Date();
const expectedDateTime = DateFnsUtils.formatDate(currentDateTime, 'YYYY-MM-DD HH:mm');
const expectedDateTimeFormat = `${DateFnsUtils.formatDate(currentDateTime, 'YYYY-MM-DDTHH:mm:00')}.000Z`;

expect(field.value).toBe(expectedDateTime);
expect(form.values['datetime']).toEqual(expectedDateTimeFormat);
Expand Down

0 comments on commit 96d0a61

Please sign in to comment.