diff --git a/frontend/src/components/payment/AmountInput.vue b/frontend/src/components/payment/AmountInput.vue index c3d9aa85e7..0e0c9d339a 100644 --- a/frontend/src/components/payment/AmountInput.vue +++ b/frontend/src/components/payment/AmountInput.vue @@ -88,8 +88,12 @@ function selectAmount(amt: number) { } function handleInput(e: Event) { - const val = (e.target as HTMLInputElement).value - if (!AMOUNT_PATTERN.test(val)) return + const input = e.target as HTMLInputElement + const val = input.value + if (!AMOUNT_PATTERN.test(val)) { + input.value = customText.value + return + } customText.value = val if (val === '') { emit('update:modelValue', null) diff --git a/frontend/src/components/payment/__tests__/AmountInput.spec.ts b/frontend/src/components/payment/__tests__/AmountInput.spec.ts new file mode 100644 index 0000000000..314cc08d3c --- /dev/null +++ b/frontend/src/components/payment/__tests__/AmountInput.spec.ts @@ -0,0 +1,37 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { enableAutoUnmount, mount } from '@vue/test-utils' +import AmountInput from '../AmountInput.vue' + +vi.mock('vue-i18n', () => ({ useI18n: () => ({ t: (key: string) => key }) })) +enableAutoUnmount(afterEach) + +function mountInput(value: number | null = null) { + return mount(AmountInput, { props: { modelValue: value } }) +} + +describe('recharge amount input', () => { + it.each(['10abc', '10.555', '-10', '1e2'])('restores the accepted amount after rejecting %s', async (value) => { + const wrapper = mountInput(10) + const input = wrapper.get('input') + await input.setValue(value) + expect((input.element as HTMLInputElement).value).toBe('10') + expect(wrapper.emitted('update:modelValue')).toBeUndefined() + }) + + it('restores the last typed amount rather than a stale prop', async () => { + const wrapper = mountInput() + const input = wrapper.get('input') + await input.setValue('12.50') + await input.setValue('12.500') + expect((input.element as HTMLInputElement).value).toBe('12.50') + expect(wrapper.emitted('update:modelValue')).toEqual([[12.5]]) + }) + + it('preserves decimal editing and allows clearing the amount', async () => { + const wrapper = mountInput() + const input = wrapper.get('input') + for (const value of ['0', '0.', '0.5', '0.50', '']) await input.setValue(value) + expect(wrapper.emitted('update:modelValue')).toEqual([[null], [null], [0.5], [0.5], [null]]) + expect((input.element as HTMLInputElement).value).toBe('') + }) +})