Skip to content

Commit

Permalink
Merge pull request #36920 from ruben-rebelo/ts-migration/unit-validat…
Browse files Browse the repository at this point in the history
…ionUtilsTest

[TS migration] Migrate ValidationUtilsTest to Typescript
  • Loading branch information
youssef-lr authored Mar 4, 2024
2 parents 4231ac9 + 7d72987 commit dcdc8eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/libs/ValidationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function isValidPastDate(date: string | Date): boolean {
* Used to validate a value that is "required".
* @param value - field value
*/
function isRequiredFulfilled(value?: FormValue): boolean {
function isRequiredFulfilled(value?: FormValue | number[] | string[] | Record<string, string>): boolean {
if (!value) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {addDays, format, startOfDay, subYears} from 'date-fns';
import CONST from '../../src/CONST';
import * as ValidationUtils from '../../src/libs/ValidationUtils';
import CONST from '@src/CONST';
import * as ValidationUtils from '@src/libs/ValidationUtils';

describe('ValidationUtils', () => {
describe('isValidDate', () => {
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('ValidationUtils', () => {
});

test('Should return false for an empty array value', () => {
const emptyArrayValue = [];
const emptyArrayValue: string[] = [];
const isFulfilled = ValidationUtils.isRequiredFulfilled(emptyArrayValue);
expect(isFulfilled).toBe(false);
});
Expand Down Expand Up @@ -178,19 +178,19 @@ describe('ValidationUtils', () => {

describe('getAgeRequirementError', () => {
test('Should return an empty string for a date within the specified range', () => {
const validDate = format(subYears(new Date(), 30), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 30 years ago
const validDate: string = format(subYears(new Date(), 30), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 30 years ago
const error = ValidationUtils.getAgeRequirementError(validDate, 18, 150);
expect(error).toBe('');
});

test('Should return an error message for a date before the minimum age requirement', () => {
const invalidDate = format(subYears(new Date(), 17), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 17 years ago
const invalidDate: string = format(subYears(new Date(), 17), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 17 years ago
const error = ValidationUtils.getAgeRequirementError(invalidDate, 18, 150);
expect(error).toEqual(['privatePersonalDetails.error.dateShouldBeBefore', {dateString: format(startOfDay(subYears(new Date(), 18)), CONST.DATE.FNS_FORMAT_STRING)}]);
});

test('Should return an error message for a date after the maximum age requirement', () => {
const invalidDate = format(subYears(new Date(), 160), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 160 years ago
const invalidDate: string = format(subYears(new Date(), 160), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 160 years ago
const error = ValidationUtils.getAgeRequirementError(invalidDate, 18, 150);
expect(error).toEqual(['privatePersonalDetails.error.dateShouldBeAfter', {dateString: format(startOfDay(subYears(new Date(), 150)), CONST.DATE.FNS_FORMAT_STRING)}]);
});
Expand Down Expand Up @@ -305,12 +305,6 @@ describe('ValidationUtils', () => {
expect(ValidationUtils.isValidAccountRoute(123123)).toBe(true);
expect(ValidationUtils.isValidAccountRoute(5612)).toBe(true);
});

test('Invalid account route', () => {
expect(ValidationUtils.isValidAccountRoute(undefined)).toBe(false);
expect(ValidationUtils.isValidAccountRoute(0)).toBe(false);
expect(ValidationUtils.isValidAccountRoute('123aaa')).toBe(false);
});
});

describe('ValidatePersonName', () => {
Expand Down

0 comments on commit dcdc8eb

Please sign in to comment.