diff --git a/packages/components/input-number/__tests__/input-number.test.ts b/packages/components/input-number/__tests__/input-number.test.tsx similarity index 55% rename from packages/components/input-number/__tests__/input-number.test.ts rename to packages/components/input-number/__tests__/input-number.test.tsx index 6dc9a4ca19..c5a5f6214e 100644 --- a/packages/components/input-number/__tests__/input-number.test.ts +++ b/packages/components/input-number/__tests__/input-number.test.tsx @@ -4,122 +4,74 @@ import { describe, expect, it, test } from 'vitest' import { ArrowDown, ArrowUp } from '@element-plus/icons-vue' import { ElFormItem } from '@element-plus/components/form' import InputNumber from '../src/input-number.vue' +import type { InputNumberInstance } from '../src/input-number' const mouseup = new Event('mouseup') -const _mount = (options) => - mount({ - components: { - 'el-input-number': InputNumber, - 'el-form-item': ElFormItem, - }, - ...options, - }) + describe('InputNumber.vue', () => { test('create', async () => { - const wrapper = _mount({ - template: ` - - - `, - setup() { - const num = ref(1) - return { - num, - } - }, - }) + const num = ref(1) + const wrapper = mount(() => ( + + )) expect(wrapper.find('input').exists()).toBe(true) }) + test('modelValue', () => { - const wrapper = _mount({ - template: '', - setup() { - const inputText = ref(1) - return { - inputText, - } - }, - }) + const inputText = ref(1) + const wrapper = mount(() => ) expect(wrapper.find('input').element.value).toEqual('1') }) + test('set modelValue undefined to form validate', async () => { - const wrapper = _mount({ - template: - '

{{num}}

', - setup() { - const num = ref(undefined) - return { - num, - } - }, - }) + const num = ref(undefined) + const wrapper = mount(() => ( + <> + +

{num.value}

+ + )) await nextTick() expect(wrapper.find('p').element.innerText).toBeUndefined() }) + test('set modelValue undefined to display placeholder', async () => { - const wrapper = _mount({ - template: - '', - setup() { - const inputText = ref(1) - return { - inputText, - } - }, - }) + const inputText = ref(1) + const wrapper = mount(() => ( + + )) expect(wrapper.find('input').element.value).toEqual('1') - wrapper.vm.inputText = undefined + inputText.value = undefined await nextTick() expect(wrapper.find('input').element.value).toEqual('') expect(wrapper.find('input').element.getAttribute('aria-valuenow')).toEqual( 'null' ) }) + test('min', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(1) - return { - num, - } - }, - }) + const num = ref(1) + const wrapper = mount(() => ) expect(wrapper.find('input').element.value).toEqual('3') wrapper.find('.el-input-number__decrease').trigger('mousedown') document.dispatchEvent(mouseup) await nextTick() expect(wrapper.find('input').element.value).toEqual('3') }) + test('max', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(5) - return { - num, - } - }, - }) + const num = ref(5) + const wrapper = mount(() => ) expect(wrapper.find('input').element.value).toEqual('3') wrapper.find('.el-input-number__increase').trigger('mousedown') document.dispatchEvent(mouseup) await nextTick() expect(wrapper.find('input').element.value).toEqual('3') }) + test('step, increase and decrease', async () => { - document.body.innerHTML = '
' - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ) wrapper.find('.el-input-number__decrease').trigger('mousedown') document.dispatchEvent(mouseup) await nextTick() @@ -133,30 +85,21 @@ describe('InputNumber.vue', () => { await nextTick() expect(wrapper.find('input').element.value).toEqual('2') }) + test('step-strictly', async () => { - const wrapper = _mount({ - template: - '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ( + + )) await wrapper.find('input').setValue(3) expect(wrapper.find('input').element.value).toEqual('4') }) + describe('precision accuracy 2', () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ( + + )) it.each([ [1.1111111111, '1.11'], [17.275, '17.28'], @@ -175,16 +118,12 @@ describe('InputNumber.vue', () => { } ) }) + describe('precision accuracy 3', () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ( + + )) it.each([ [1.1111111111, '1.111'], [17.275, '17.275'], @@ -202,16 +141,12 @@ describe('InputNumber.vue', () => { } ) }) + test('disabled', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ( + + )) wrapper.find('.el-input-number__decrease').trigger('mousedown') document.dispatchEvent(mouseup) await nextTick() @@ -221,42 +156,28 @@ describe('InputNumber.vue', () => { await nextTick() expect(wrapper.find('input').element.value).toEqual('0') }) + test('controls', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ( + + )) expect(wrapper.find('.el-input-number__increase').exists()).toBe(false) expect(wrapper.find('.el-input-number__decrease').exists()).toBe(false) }) + test('controls-position', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ( + + )) expect(wrapper.findComponent(ArrowDown).exists()).toBe(true) expect(wrapper.findComponent(ArrowUp).exists()).toBe(true) }) + test('change-event', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ) wrapper.find('.el-input-number__increase').trigger('mousedown') document.dispatchEvent(mouseup) await nextTick() @@ -280,203 +201,151 @@ describe('InputNumber.vue', () => { wrapper.getComponent(InputNumber).emitted('update:modelValue') ).toHaveLength(2) }) + test('blur-event', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ) await wrapper.find('input').trigger('blur') expect(wrapper.getComponent(InputNumber).emitted('blur')).toHaveLength(1) }) + test('focus-event', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(0) - return { - num, - } - }, - }) + const num = ref(0) + const wrapper = mount(() => ) await wrapper.find('input').trigger('focus') expect(wrapper.getComponent(InputNumber).emitted('focus')).toHaveLength(1) }) test('clear with :value-on-clear="null"', async () => { - const wrapper = _mount({ - template: '', - setup() { - const num = ref(2) - return { - num, - } - }, - }) + const num = ref(2) + const wrapper = mount(() => ( + + )) const elInput = wrapper.findComponent({ name: 'ElInputNumber' }).vm elInput.handleInputChange('') await nextTick() - expect(wrapper.vm.num).toBe(null) + expect(num.value).toBe(null) elInput.increase() await nextTick() - expect(wrapper.vm.num).toBe(1) + expect(num.value).toBe(1) elInput.increase() await nextTick() - expect(wrapper.vm.num).toBe(2) + expect(num.value).toBe(2) elInput.handleInputChange('') await nextTick() - expect(wrapper.vm.num).toBe(null) + expect(num.value).toBe(null) elInput.decrease() await nextTick() - expect(wrapper.vm.num).toBe(1) + expect(num.value).toBe(1) elInput.decrease() await nextTick() - expect(wrapper.vm.num).toBe(1) + expect(num.value).toBe(1) }) test('clear with value-on-clear="min"', async () => { - const wrapper = _mount({ - template: - '', - setup() { - const num = ref(2) - return { - num, - } - }, - }) + const num = ref(2) + const wrapper = mount(() => ( + + )) const elInput = wrapper.findComponent({ name: 'ElInputNumber' }).vm elInput.handleInputChange('') await nextTick() - expect(wrapper.vm.num).toBe(1) + expect(num.value).toBe(1) elInput.increase() await nextTick() - expect(wrapper.vm.num).toBe(2) + expect(num.value).toBe(2) elInput.handleInputChange('') await nextTick() - expect(wrapper.vm.num).toBe(1) + expect(num.value).toBe(1) elInput.decrease() await nextTick() - expect(wrapper.vm.num).toBe(1) + expect(num.value).toBe(1) }) test('clear with value-on-clear="max"', async () => { - const wrapper = _mount({ - template: - '', - setup() { - const num = ref(2) - return { - num, - } - }, - }) + const num = ref(2) + const wrapper = mount(() => ( + + )) const elInput = wrapper.findComponent({ name: 'ElInputNumber' }).vm elInput.handleInputChange('') await nextTick() - expect(wrapper.vm.num).toBe(10) + expect(num.value).toBe(10) elInput.increase() await nextTick() - expect(wrapper.vm.num).toBe(10) + expect(num.value).toBe(10) elInput.handleInputChange('') await nextTick() - expect(wrapper.vm.num).toBe(10) + expect(num.value).toBe(10) elInput.decrease() await nextTick() - expect(wrapper.vm.num).toBe(9) + expect(num.value).toBe(9) }) test('clear with :value-on-clear="5"', async () => { - const wrapper = _mount({ - template: - '', - setup() { - const num = ref(2) - return { - num, - } - }, - }) + const num = ref(2) + const wrapper = mount(() => ( + + )) const elInput = wrapper.findComponent({ name: 'ElInputNumber' }).vm elInput.handleInputChange('') await nextTick() - expect(wrapper.vm.num).toBe(5) + expect(num.value).toBe(5) elInput.increase() await nextTick() - expect(wrapper.vm.num).toBe(6) + expect(num.value).toBe(6) elInput.handleInputChange('') await nextTick() - expect(wrapper.vm.num).toBe(5) + expect(num.value).toBe(5) elInput.decrease() await nextTick() - expect(wrapper.vm.num).toBe(4) + expect(num.value).toBe(4) }) test('check increase and decrease button when modelValue not in [min, max]', async () => { - const wrapper = _mount({ - template: ` - - `, - setup() { - const num1 = ref(-5) - const num2 = ref(15) - const inputNumber1 = ref(null) - const inputNumber2 = ref(null) - return { - num1, - num2, - inputNumber1, - inputNumber2, - } - }, - }) - const elInputNumber1 = wrapper.vm.inputNumber1 - const elInputNumber2 = wrapper.vm.inputNumber2 - expect(wrapper.vm.num1).toBe(1) - expect(wrapper.vm.num2).toBe(10) + const num1 = ref(-5) + const num2 = ref(15) + const inputNumber1 = ref() + const inputNumber2 = ref() - elInputNumber1.decrease() - await nextTick() - expect(wrapper.vm.num1).toBe(1) - elInputNumber1.increase() - await nextTick() - expect(wrapper.vm.num1).toBe(2) - elInputNumber1.increase() - await nextTick() - expect(wrapper.vm.num1).toBe(3) + mount(() => ( + <> + + + + )) - elInputNumber2.increase() + expect(num1.value).toBe(1) + expect(num2.value).toBe(10) + + inputNumber1.value!.decrease() await nextTick() - expect(wrapper.vm.num2).toBe(10) - elInputNumber2.decrease() + expect(num1.value).toBe(1) + inputNumber1.value!.increase() await nextTick() - expect(wrapper.vm.num2).toBe(9) - elInputNumber2.decrease() + expect(num1.value).toBe(2) + inputNumber1.value!.increase() await nextTick() - expect(wrapper.vm.num2).toBe(8) + expect(num1.value).toBe(3) + + inputNumber2.value!.increase() + await nextTick() + expect(num2.value).toBe(10) + inputNumber2.value!.decrease() + await nextTick() + expect(num2.value).toBe(9) + inputNumber2.value!.decrease() + await nextTick() + expect(num2.value).toBe(8) }) describe('form item accessibility integration', () => { test('automatic id attachment', async () => { - const wrapper = _mount({ - template: ` - - `, - }) + const wrapper = mount(() => ( + + + + )) await nextTick() const formItem = wrapper.find('[data-test-ref="item"]') @@ -487,11 +356,11 @@ describe('InputNumber.vue', () => { }) test('specified id attachment', async () => { - const wrapper = _mount({ - template: ` - - `, - }) + const wrapper = mount(() => ( + + + + )) await nextTick() const formItem = wrapper.find('[data-test-ref="item"]') @@ -503,12 +372,12 @@ describe('InputNumber.vue', () => { }) test('form item role is group when multiple inputs', async () => { - const wrapper = _mount({ - template: ` - - - `, - }) + const wrapper = mount(() => ( + + + + + )) await nextTick() const formItem = wrapper.find('[data-test-ref="item"]')