mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-20 00:18:55 +00:00
fix(payment): restore accepted text after invalid amount input
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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('')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user