2
0
mirror of https://github.com/tenrok/maska.git synced 2026-06-14 18:42:27 +03:00

refactor: MaskInput rework

- abort controller for events
- refactor onInput
- remove beforeinputEvent
- new cursor position fix
This commit is contained in:
Alexander Shabunevich
2024-04-06 22:43:56 +03:00
parent 243b8445a2
commit 1e50a2a259
4 changed files with 83 additions and 72 deletions
+17 -9
View File
@@ -67,7 +67,7 @@ test('initial value', async () => {
const input1 = wrapper.get<HTMLInputElement>('#input1')
const input2 = wrapper.get<HTMLInputElement>('#input2')
await new Promise((r) => setTimeout(r))
await nextTick()
expect(input1.element.value).toBe('1-2')
expect(input2.element.value).toBe('3-4')
@@ -98,13 +98,11 @@ test('bind completed', async () => {
const input = wrapper.get('input')
await input.setValue('12')
await nextTick()
expect(input.element.value).toBe('1-2')
expect(wrapper.get('div').element.textContent).toBe('Uncompleted')
await input.setValue('123')
await nextTick()
expect(input.element.value).toBe('1-2-3')
expect(wrapper.get('div').element.textContent).toBe('Completed')
@@ -120,10 +118,12 @@ test('v-model', async () => {
expect(wrapper.get('div').element.textContent).toBe('1-2')
await input.setValue('1')
expect(input.element.value).toBe('1-')
expect(wrapper.get('div').element.textContent).toBe('1-')
await input.setValue('123')
expect(input.element.value).toBe('1-2')
expect(wrapper.get('div').element.textContent).toBe('1-2')
})
@@ -133,10 +133,12 @@ test('custom component', async () => {
const input = wrapper.get('input')
await input.setValue('1')
expect(input.element.value).toBe('1-')
expect(wrapper.get('div').element.textContent).toBe('1-')
await input.setValue('123')
expect(input.element.value).toBe('1-2')
expect(wrapper.get('div').element.textContent).toBe('1-2')
})
@@ -146,14 +148,12 @@ test('change value', async () => {
const wrapper = mount(ChangeValue)
const input = wrapper.get('input')
await new Promise((r) => setTimeout(r))
await nextTick()
expect(input.element.value).toBe('12-3')
await wrapper.get('button').trigger('click')
await new Promise((r) => setTimeout(r))
expect(input.element.value).toBe('56-7')
})
@@ -173,6 +173,7 @@ test('multiple inputs', async () => {
expect(wrapper.emitted('mask2')).toHaveLength(1)
await input.setValue('1')
expect(input.element.value).toBe('1')
expect(wrapper.get('#value1').element.textContent).toBe('1')
expect(wrapper.emitted()).toHaveProperty('mask1')
@@ -180,10 +181,8 @@ test('multiple inputs', async () => {
expect(wrapper.emitted('mask2')).toHaveLength(1)
await checkbox.setValue()
expect(checkbox.element).toBeChecked()
await new Promise((r) => setTimeout(r))
expect(input.element.value).toBe('1-')
expect(wrapper.emitted('mask1')).toHaveLength(3)
expect(wrapper.emitted('mask2')).toHaveLength(1)
@@ -194,18 +193,22 @@ test('config and bind', async () => {
const input = wrapper.get('input')
await input.setValue('1')
expect(input.element.value).toBe('')
expect(wrapper.get('div').element.textContent).toBe('')
await input.setValue('ab')
expect(input.element.value).toBe('AB')
expect(wrapper.get('div').element.textContent).toBe('AB')
await input.setValue('ab cd ')
expect(input.element.value).toBe('AB CD')
expect(wrapper.get('div').element.textContent).toBe('AB CD')
await input.setValue('ab cd1')
expect(input.element.value).toBe('AB CD')
expect(wrapper.get('div').element.textContent).toBe('AB CD')
})
@@ -247,15 +250,18 @@ test('callbacks', async () => {
const input2 = wrapper.get('#input2')
await input1.setValue('1')
expect(wrapper.emitted()).toHaveProperty('mask1')
expect(wrapper.emitted('mask1')).toHaveLength(1)
expect(wrapper.emitted('mask1')[0][0]).toHaveProperty('completed', false)
await input1.setValue('12')
expect(wrapper.emitted('mask1')).toHaveLength(2)
expect(wrapper.emitted('mask1')[1][0]).toHaveProperty('completed', true)
await input2.setValue('3')
expect(wrapper.emitted()).toHaveProperty('mask2')
expect(wrapper.emitted()).toHaveProperty('mask3')
expect(wrapper.emitted('mask2')).toHaveLength(1)
@@ -264,6 +270,7 @@ test('callbacks', async () => {
expect(wrapper.emitted('mask3')[0][0]).toHaveProperty('completed', false)
await input2.setValue('34')
expect(wrapper.emitted('mask2')).toHaveLength(2)
expect(wrapper.emitted('mask3')).toHaveLength(2)
expect(wrapper.emitted('mask2')[1][0]).toHaveProperty('completed', true)
@@ -278,6 +285,7 @@ test('options api component', async () => {
const input = wrapper.get('input')
await input.setValue('123')
expect(input.element.value).toBe('1-2')
expect(wrapper.get('div').element.textContent).toBe('1-2')
})
+24 -9
View File
@@ -305,10 +305,10 @@ describe('test hooks', () => {
await user.type(input, '1234.56{backspace}')
expect(input).toHaveValue('$1,234.5')
expect(context.preProcess).toHaveBeenCalledTimes(8)
expect(context.postProcess).toHaveBeenCalledTimes(8)
expect(context.preProcess).toHaveBeenCalled()
expect(context.postProcess).toHaveBeenCalled()
expect(context.onMaska).toHaveBeenCalledTimes(8)
expect(context.onMaska).toHaveBeenCalled()
expect(context.onMaska).toHaveBeenLastCalledWith({
completed: true,
masked: '$1,234.5',
@@ -320,10 +320,10 @@ describe('test hooks', () => {
await user.type(input, '1234.56{backspace}{backspace}')
expect(input).toHaveValue('$1,234.')
expect(context.preProcess).toHaveBeenCalledTimes(9)
expect(context.postProcess).toHaveBeenCalledTimes(9)
expect(context.preProcess).toHaveBeenCalled()
expect(context.postProcess).toHaveBeenCalled()
expect(context.onMaska).toHaveBeenCalledTimes(9)
expect(context.onMaska).toHaveBeenCalled()
expect(context.onMaska).toHaveBeenLastCalledWith({
completed: true,
masked: '$1,234.',
@@ -335,10 +335,10 @@ describe('test hooks', () => {
await user.type(input, '1234.56{backspace}{backspace}{backspace}')
expect(input).toHaveValue('$1,234')
expect(context.preProcess).toHaveBeenCalledTimes(10)
expect(context.postProcess).toHaveBeenCalledTimes(10)
expect(context.preProcess).toHaveBeenCalled()
expect(context.postProcess).toHaveBeenCalled()
expect(context.onMaska).toHaveBeenCalledTimes(10)
expect(context.onMaska).toHaveBeenCalled()
expect(context.onMaska).toHaveBeenLastCalledWith({
completed: true,
masked: '$1,234',
@@ -750,6 +750,11 @@ describe('#-# eager mask', () => {
test('input 12{backspace}×2', async () => {
await user.type(input, '12{backspace}{backspace}')
expect(input).toHaveValue('1-')
})
test('input 12{backspace}×3', async () => {
await user.type(input, '12{backspace}{backspace}{backspace}')
expect(input).toHaveValue('')
})
@@ -863,6 +868,16 @@ describe('+1 (#) #-# eager mask', () => {
test('input 234{backspace}×4', async () => {
await user.type(input, '234{backspace}{backspace}{backspace}{backspace}')
expect(input).toHaveValue('+1 (2) ')
})
test('input 234{backspace}×5', async () => {
await user.type(input, '234{backspace}{backspace}{backspace}{backspace}{backspace}')
expect(input).toHaveValue('+1 (2) ')
})
test('input 234{backspace}×6', async () => {
await user.type(input, '234{backspace}{backspace}{backspace}{backspace}{backspace}{backspace}')
expect(input).toHaveValue('')
})
})