Skip to content

Commit

Permalink
donation amount changed to integer to avoid language specific decimal…
Browse files Browse the repository at this point in the history
… separators
  • Loading branch information
quantum-grit committed Oct 24, 2023
1 parent e04fb65 commit 276ad75
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
18 changes: 9 additions & 9 deletions e2e/tests/regression/donation-flow/anon-donation-custom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,29 @@ test.describe.serial(
.soft(await donationPage.isSelectAmountStepActive(), 'Select Amount step is not active.')
.toBeTruthy()
await donationPage.selectRadioButtonByLabelText([otherAmountText])
await donationPage.fillOtherAmountInputField('7,50')
await donationPage.fillOtherAmountInputField('75')
await donationPage.setDonationRegionFromTheDropdown(bgDonationRegions.EUROPE)
await donationPage.selectCheckboxByLabelText([bgCardIncludeFeesText])
// Expected pattern:
// За вашия превод от {totalChargedAmountText} лв., таксата на Stripe ще е {feeAmountText} лв., а кампанията ще получи {donationAmountText} лв.
const totalChargedAmountText = await donationPage.getTotalChargedAmountsAsText()
const feeAmountText = await donationPage.getFeeAmountsAsText()
const donationAmountText = await donationPage.getDonationAmountsAsText()
expect.soft(totalChargedAmountText).toEqual('8,10 лв.')
expect.soft(feeAmountText).toEqual('0,60 лв.')
expect(donationAmountText).toEqual('7,50 лв.')
expect.soft(totalChargedAmountText).toEqual('76,42 лв.')
expect.soft(feeAmountText).toEqual('1,42 лв.')
expect(donationAmountText).toEqual('75,00 лв.')
})

test('The total charge, fee tax and donation amount are recalculated correctly when the donation amount is changed', async () => {
await donationPage.fillOtherAmountInputField('12,90')
await donationPage.fillOtherAmountInputField('120')
// Expected pattern:
// За вашия превод от {totalChargedAmountText} лв., таксата на Stripe ще е {feeAmountText} лв., а кампанията ще получи {donationAmountText} лв.
const totalChargedAmountText = await donationPage.getTotalChargedAmountsAsText()
const feeAmountText = await donationPage.getFeeAmountsAsText()
const donationAmountText = await donationPage.getDonationAmountsAsText()
expect.soft(totalChargedAmountText).toEqual('13,56 лв.')
expect.soft(feeAmountText).toEqual('0,66 лв.')
expect(donationAmountText).toEqual('12,90 лв.')
expect.soft(totalChargedAmountText).toEqual('121,96 лв.')
expect.soft(feeAmountText).toEqual('1,96 лв.')
expect(donationAmountText).toEqual('120,00 лв.')
})

test('The user is able to fill in e-mail for anonymous donation', async () => {
Expand All @@ -111,7 +111,7 @@ test.describe.serial(
const actualStripeEmail = await stripeCheckoutPage.getReadonlyEmailText()
expect
.soft(stripeTotalAmount, 'The Stripe total donation amount is not correct.')
.toContain('13,56')
.toContain('121,96')
expect(actualStripeEmail, 'The user e-mail is not sent correctly to Stripe.').toEqual(
testEmail,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export const validateFirst: yup.SchemaOf<FirstStep> = yup
// Here we should fetch the possible payments to put into the oneOf, but it's not that important
then: yup.string().required(),
}),
otherAmount: yup.number().when('amount', {
is: 'other',
then: yup.number().min(1, 'one-time-donation:errors-fields.other-amount').required(),
}),
otherAmount: yup
.number()
.integer()
.when('amount', {
is: 'other',
then: yup.number().min(1, 'one-time-donation:errors-fields.other-amount').required(),
}),
})

export const validateSecond: yup.SchemaOf<SecondStep> = yup.object().defined().shape({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export default function FirstStep() {
options={
oneTimePrices
.map((v) => ({
label: moneyPublic(Number(v)),
label: moneyPublic(Number(v), undefined, undefined, 0, 0), //show amounts as integer
value: String(Number(v)),
}))
.concat({
Expand Down
9 changes: 6 additions & 3 deletions src/components/common/form/NumberInputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default function NumberInputField({
}: Props) {
const { t, i18n } = useTranslation('one-time-donation')
const [, meta, { setValue, setError }] = useField(name)
const decimalSeparator = (1.1).toLocaleString(i18n.language).charAt(1)

useEffect(() => {
setValue(1)
Expand All @@ -36,14 +35,15 @@ export default function NumberInputField({
e.preventDefault()
return
}
if (decimalSeparator !== e.key && (e.key === '.' || e.key === ',')) {

//prevent decimal amounts
if (e.key === '.' || e.key === ',') {
e.preventDefault()
return
}

if (
(e.key.charCodeAt(0) >= 48 && e.key.charCodeAt(0) <= 57) ||
(isInteger(meta.value) && e.key === decimalSeparator) ||
(e.ctrlKey && e.key === 'v') ||
(e.ctrlKey && e.key === 'c') ||
(e.ctrlKey && e.key === 'a') ||
Expand All @@ -66,7 +66,10 @@ export default function NumberInputField({
onPaste={async (e) => {
e.preventDefault()
const value = e.clipboardData.getData('Text')
if (value.includes('.') || value.includes(',')) return

const transformedValue: string = value.replace(/ /g, '') as string

if (!isNaN(Number(transformedValue))) {
setValue(transformedValue)
return
Expand Down

0 comments on commit 276ad75

Please sign in to comment.