Skip to content

Commit

Permalink
Merge branch 'main' into ahe/no-unchecked-index
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewHEguardian authored Jan 7, 2025
2 parents 638e4c7 + 64b6e8f commit 6eacd2c
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 412 deletions.
96 changes: 1 addition & 95 deletions support-frontend/assets/helpers/__tests__/formValidation.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,9 @@
// ----- Imports ----- //
import {
amountOrOtherAmountIsValid,
maxTwoDecimals,
notLongerThan,
} from '../forms/formValidation';
import { UnitedStates } from '../internationalisation/countryGroup';
import { notLongerThan } from '../forms/formValidation';

// ----- Tests ----- //

describe('formValidation', () => {
describe('maxTwoDecimals', () => {
it('should return true for an int', () => {
expect(maxTwoDecimals('3')).toEqual(true);
});
it('should return true for one dp', () => {
expect(maxTwoDecimals('3.2')).toEqual(true);
});
it('should return true for decimal point and no decimal places', () => {
expect(maxTwoDecimals('3.')).toEqual(true);
});
it('should return true for two dp', () => {
expect(maxTwoDecimals('3.22')).toEqual(true);
});
it('should return false for three dp', () => {
expect(maxTwoDecimals('3.222')).toEqual(false);
});
it('should return false for invalid number', () => {
expect(maxTwoDecimals('3e22')).toEqual(false);
});
it('should return false for empty string', () => {
expect(maxTwoDecimals('')).toEqual(false);
});
it('should return false for empty string', () => {
expect(maxTwoDecimals('-12')).toEqual(false);
});
});
describe('amountOrOtherAmountIsValid', () => {
const defaultSelectedAmounts = {
ONE_OFF: 50,
MONTHLY: 15,
ANNUAL: 100,
};
const selectedAmountsWithOtherSelected = {
ONE_OFF: 'other',
MONTHLY: 'other',
ANNUAL: 'other',
} as const;
const defaultOtherAmounts = {
ONE_OFF: {
amount: null,
},
MONTHLY: {
amount: '2',
},
ANNUAL: {
amount: '0',
},
};
it('should return true if selected amount is not other and amount is valid', () => {
expect(
amountOrOtherAmountIsValid(
defaultSelectedAmounts,
defaultOtherAmounts,
'MONTHLY',
UnitedStates,
),
).toEqual(true);
});
it('should return true if other is selected and amount is valid', () => {
expect(
amountOrOtherAmountIsValid(
selectedAmountsWithOtherSelected,
defaultOtherAmounts,
'MONTHLY',
UnitedStates,
),
).toEqual(true);
});
it('should return false if other is selected and amount is empty', () => {
expect(
amountOrOtherAmountIsValid(
selectedAmountsWithOtherSelected,
defaultOtherAmounts,
'ONE_OFF',
UnitedStates,
),
).toEqual(false);
});
it('should return false if other is selected and amount is invalid', () => {
expect(
amountOrOtherAmountIsValid(
selectedAmountsWithOtherSelected,
defaultOtherAmounts,
'ANNUAL',
UnitedStates,
),
).toEqual(false);
});
});
describe('notLongerThan', () => {
it('should return false if string is too long', () => {
expect(notLongerThan('hello world', 10)).toBeFalsy();
Expand Down
64 changes: 0 additions & 64 deletions support-frontend/assets/helpers/forms/formValidation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import { config } from 'helpers/contributions';
import type {
ContributionType,
OtherAmounts,
SelectedAmounts,
} from 'helpers/contributions';
import type { CountryGroupId } from 'helpers/internationalisation/countryGroup';

// Copied from
// https://github.com/playframework/playframework/blob/master/framework/src/play/
// src/main/scala/play/api/data/validation/Validation.scala#L81
Expand All @@ -26,21 +18,6 @@ export const isValidEmail: (arg0: string | null) => boolean = (input) =>
export const isValidZipCode = (zipCode: string): boolean =>
/^\d{5}(-\d{4})?$/.test(zipCode);

export const isNotNaN = (value: string): boolean => !isNaN(parseFloat(value));

export const isLargerOrEqual: (arg0: number, arg1: string) => boolean = (
min,
input,
) => min <= parseFloat(input);

export const isSmallerOrEqual: (arg0: number, arg1: string) => boolean = (
max,
input,
) => parseFloat(input) <= max;

export const maxTwoDecimals: (arg0: string) => boolean = (input) =>
new RegExp('^\\d+\\.?\\d{0,2}$').test(input);

export const notLongerThan = (
value: string | null,
maxLength: number,
Expand All @@ -64,47 +41,6 @@ export const emailAddressesMatch: (
export const checkOptionalEmail: (arg0: string | null) => boolean = (input) =>
isEmpty(input) || isValidEmail(input);

export const amountIsValid = (
input: string,
countryGroupId: CountryGroupId,
contributionType: ContributionType,
): boolean => {
const min = config[countryGroupId][contributionType].min;
const max = config[countryGroupId][contributionType].max;
return (
isNotEmpty(input) &&
isNotNaN(input) &&
isLargerOrEqual(min, input) &&
isSmallerOrEqual(max, input) &&
maxTwoDecimals(input)
);
};

export const amountOrOtherAmountIsValid = (
selectedAmounts: SelectedAmounts,
otherAmounts: OtherAmounts,
contributionType: ContributionType,
countryGroupId: CountryGroupId,
): boolean => {
let amt = '';

if (
selectedAmounts[contributionType] &&
selectedAmounts[contributionType] === 'other'
) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- otherAmounts[contributionType] may be undefined
if (otherAmounts[contributionType]?.amount) {
if (typeof otherAmounts[contributionType].amount === 'string') {
amt = otherAmounts[contributionType].amount as string;
}
}
} else if (selectedAmounts[contributionType]) {
amt = selectedAmounts[contributionType].toString();
}

return amountIsValid(amt, countryGroupId, contributionType);
};

// regex from: https://gist.github.com/simonwhitaker/5748487?permalink_comment_id=4648104#gistcomment-4648104
// based on UK Gov logic: https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/488478/Bulk_Data_Transfer_-_additional_validation_valid_from_12_November_2015.pdf
export function isValidPostcode(postcode: string): boolean {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 6eacd2c

Please sign in to comment.