123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- import { nextTick, ref } from 'vue'
- import { mount } from '@vue/test-utils'
- import { describe, expect, it, test, vi } from 'vitest'
- import { ArrowDown, ArrowUp } from '@element-plus/icons-vue'
- import { ElFormItem } from '@element-plus/components/form'
- import InputNumber from '../src/input-number.vue'
- const mouseup = new Event('mouseup')
- describe('InputNumber.vue', () => {
- test('create', async () => {
- const num = ref(1)
- const wrapper = mount(() => (
- <InputNumber label="描述文字" v-model={num.value} />
- ))
- expect(wrapper.find('input').exists()).toBe(true)
- })
- test('modelValue', () => {
- const inputText = ref(1)
- const wrapper = mount(() => <InputNumber modelValue={inputText.value} />)
- expect(wrapper.find('input').element.value).toEqual('1')
- })
- test('set modelValue undefined to form validate', async () => {
- const num = ref(undefined)
- const wrapper = mount(() => (
- <>
- <InputNumber modelValue={num.value} placeholder="input number" />
- <p>{num.value}</p>
- </>
- ))
- await nextTick()
- expect(wrapper.find('p').element.innerText).toBeUndefined()
- })
- test('set modelValue undefined to display placeholder', async () => {
- const inputText = ref<number | undefined>(1)
- const wrapper = mount(() => (
- <InputNumber modelValue={inputText.value} placeholder="input number" />
- ))
- expect(wrapper.find('input').element.value).toEqual('1')
- inputText.value = undefined
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('')
- expect(wrapper.find('input').element.getAttribute('aria-valuenow')).toEqual(
- 'null'
- )
- })
- // fix: #10328
- test('Make sure the input action can trigger the modelValue update', async () => {
- const num = ref<number>(0)
- const handleUpdate = (data: number | undefined) => {
- num.value = data!
- }
- const wrapper = mount(() => (
- <InputNumber modelValue={num.value} onUpdate:modelValue={handleUpdate} />
- ))
- const el = wrapper.find('input').element
- const simulateEvent = (text: string, event: string) => {
- el.value = text
- el.dispatchEvent(new Event(event))
- }
- simulateEvent('3', 'input')
- await nextTick()
- expect(num.value).toEqual(3)
- })
- test('min', async () => {
- const num = ref(1)
- const wrapper = mount(() => <InputNumber min={3} v-model={num.value} />)
- 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 num = ref(5)
- const wrapper = mount(() => <InputNumber max={3} v-model={num.value} />)
- 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 () => {
- const num = ref(0)
- const wrapper = mount(() => <InputNumber v-model={num.value} step={2} />)
- wrapper.find('.el-input-number__decrease').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('-2')
- wrapper.find('.el-input-number__increase').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('0')
- wrapper.find('.el-input-number__increase').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('2')
- })
- test('step-strictly', async () => {
- const num = ref(0)
- const wrapper = mount(() => (
- <InputNumber step-strictly={true} step={2} v-model={num.value} />
- ))
- await wrapper.find('input').setValue(3)
- expect(wrapper.find('input').element.value).toEqual('4')
- })
- test('value decimals miss prop precision', async () => {
- const num = ref(0.2)
- const wrapper = mount(() => (
- <InputNumber step-strictly={true} step={0.1} v-model={num.value} />
- ))
- const elInputNumber = wrapper.findComponent({ name: 'ElInputNumber' }).vm
- elInputNumber.increase()
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('0.3')
- num.value = 0.4
- await nextTick()
- elInputNumber.decrease()
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('0.3')
- })
- describe('precision accuracy 2', () => {
- const num = ref(0)
- const wrapper = mount(() => (
- <InputNumber precision={2} v-model={num.value} />
- ))
- it.each([
- [1.1111111111, '1.11'],
- [17.275, '17.28'],
- [17.2745, '17.27'],
- [1.09, '1.09'],
- [1.009, '1.01'],
- [10.999, '11.00'],
- [15, '15.00'],
- [15.5, '15.50'],
- [15.555, '15.56'],
- ])(
- 'each precision accuracy test: $input $output',
- async (input, output) => {
- await wrapper.find('input').setValue(input)
- expect(wrapper.find('input').element.value).toEqual(`${output}`)
- }
- )
- })
- describe('precision accuracy 3', () => {
- const num = ref(0)
- const wrapper = mount(() => (
- <InputNumber precision={3} v-model={num.value} />
- ))
- it.each([
- [1.1111111111, '1.111'],
- [17.275, '17.275'],
- [17.2745, '17.275'],
- [1.09, '1.090'],
- [10.999, '10.999'],
- [10.9999, '11.000'],
- [15.555, '15.555'],
- [1.3335, '1.334'],
- ])(
- 'each precision accuracy test: $input $output',
- async (input, output) => {
- await wrapper.find('input').setValue(input)
- expect(wrapper.find('input').element.value).toEqual(`${output}`)
- }
- )
- })
- test('readonly', async () => {
- const num = ref(0)
- const handleFocus = vi.fn()
- const wrapper = mount(() => (
- <InputNumber readonly v-model={num.value} onFocus={handleFocus} />
- ))
- wrapper.find('.el-input__inner').trigger('focus')
- await nextTick()
- expect(handleFocus).toHaveBeenCalledTimes(1)
- wrapper.find('.el-input-number__decrease').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('0')
- wrapper.find('.el-input-number__increase').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('0')
- })
- test('disabled', async () => {
- const num = ref(0)
- const wrapper = mount(() => (
- <InputNumber disabled={true} v-model={num.value} />
- ))
- wrapper.find('.el-input-number__decrease').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('0')
- wrapper.find('.el-input-number__increase').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.find('input').element.value).toEqual('0')
- })
- test('controls', async () => {
- const num = ref(0)
- const wrapper = mount(() => (
- <InputNumber controls={false} v-model={num.value} />
- ))
- 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 num = ref(0)
- const wrapper = mount(() => (
- <InputNumber controls-position="right" v-model={num.value} />
- ))
- expect(wrapper.findComponent(ArrowDown).exists()).toBe(true)
- expect(wrapper.findComponent(ArrowUp).exists()).toBe(true)
- })
- test('input-event', async () => {
- const handleInput = vi.fn()
- const num = ref(0)
- const wrapper = mount(() => (
- <InputNumber v-model={num.value} onInput={handleInput} />
- ))
- const inputWrapper = wrapper.find('input')
- const nativeInput = inputWrapper.element
- nativeInput.value = '0'
- await inputWrapper.trigger('input')
- expect(handleInput).toBeCalledTimes(0)
- nativeInput.value = '1'
- await inputWrapper.trigger('input')
- expect(handleInput).toBeCalledTimes(1)
- expect(handleInput).toHaveBeenCalledWith(1)
- nativeInput.value = '2'
- await inputWrapper.trigger('input')
- expect(handleInput).toBeCalledTimes(2)
- expect(handleInput).toHaveBeenCalledWith(2)
- nativeInput.value = ''
- await inputWrapper.trigger('input')
- expect(handleInput).toBeCalledTimes(3)
- expect(handleInput).toHaveBeenCalledWith(null)
- })
- test('change-event', async () => {
- const num = ref(0)
- const wrapper = mount(() => <InputNumber v-model={num.value} />)
- wrapper.find('.el-input-number__increase').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.getComponent(InputNumber).emitted('change')).toHaveLength(1)
- expect(wrapper.getComponent(InputNumber).emitted().change[0]).toEqual([
- 1, 0,
- ])
- expect(
- wrapper.getComponent(InputNumber).emitted('update:modelValue')
- ).toHaveLength(1)
- wrapper.find('.el-input-number__increase').trigger('mousedown')
- document.dispatchEvent(mouseup)
- await nextTick()
- expect(wrapper.getComponent(InputNumber).emitted('change')).toHaveLength(2)
- expect(wrapper.getComponent(InputNumber).emitted().change[1]).toEqual([
- 2, 1,
- ])
- expect(
- wrapper.getComponent(InputNumber).emitted('update:modelValue')
- ).toHaveLength(2)
- await wrapper.find('input').setValue(0)
- expect(wrapper.getComponent(InputNumber).emitted('change')).toHaveLength(3)
- expect(wrapper.getComponent(InputNumber).emitted().change[2]).toEqual([
- 0, 2,
- ])
- expect(
- wrapper.getComponent(InputNumber).emitted('update:modelValue')
- ).toHaveLength(4)
- })
- test('blur-event', async () => {
- const num = ref(0)
- const wrapper = mount(() => <InputNumber v-model={num.value} />)
- await wrapper.find('input').trigger('blur')
- expect(wrapper.getComponent(InputNumber).emitted('blur')).toHaveLength(1)
- })
- test('focus-event', async () => {
- const num = ref(0)
- const wrapper = mount(() => <InputNumber v-model={num.value} />)
- await wrapper.find('input').trigger('focus')
- expect(wrapper.getComponent(InputNumber).emitted('focus')).toHaveLength(1)
- })
- test('clear with :value-on-clear="null"', async () => {
- const num = ref(2)
- const wrapper = mount(() => (
- <InputNumber v-model={num.value} min={1} max={10} />
- ))
- const elInput = wrapper.findComponent({ name: 'ElInputNumber' }).vm
- elInput.handleInputChange('')
- await nextTick()
- expect(num.value).toBe(null)
- elInput.increase()
- await nextTick()
- expect(num.value).toBe(1)
- elInput.increase()
- await nextTick()
- expect(num.value).toBe(2)
- elInput.handleInputChange('')
- await nextTick()
- expect(num.value).toBe(null)
- elInput.decrease()
- await nextTick()
- expect(num.value).toBe(1)
- elInput.decrease()
- await nextTick()
- expect(num.value).toBe(1)
- })
- test('clear with value-on-clear="min"', async () => {
- const num = ref(2)
- const wrapper = mount(() => (
- <InputNumber value-on-clear="min" v-model={num.value} min={1} max={10} />
- ))
- const elInput = wrapper.findComponent({ name: 'ElInputNumber' }).vm
- elInput.handleInputChange('')
- await nextTick()
- expect(num.value).toBe(1)
- elInput.increase()
- await nextTick()
- expect(num.value).toBe(2)
- elInput.handleInputChange('')
- await nextTick()
- expect(num.value).toBe(1)
- elInput.decrease()
- await nextTick()
- expect(num.value).toBe(1)
- })
- test('clear with value-on-clear="max"', async () => {
- const num = ref(2)
- const wrapper = mount(() => (
- <InputNumber value-on-clear="max" v-model={num.value} min={1} max={10} />
- ))
- const elInput = wrapper.findComponent({ name: 'ElInputNumber' }).vm
- elInput.handleInputChange('')
- await nextTick()
- expect(num.value).toBe(10)
- elInput.increase()
- await nextTick()
- expect(num.value).toBe(10)
- elInput.handleInputChange('')
- await nextTick()
- expect(num.value).toBe(10)
- elInput.decrease()
- await nextTick()
- expect(num.value).toBe(9)
- })
- test('clear with :value-on-clear="5"', async () => {
- const num = ref(2)
- const wrapper = mount(() => (
- <InputNumber value-on-clear={5} v-model={num.value} min={1} max={10} />
- ))
- const elInput = wrapper.findComponent({ name: 'ElInputNumber' }).vm
- elInput.handleInputChange('')
- await nextTick()
- expect(num.value).toBe(5)
- elInput.increase()
- await nextTick()
- expect(num.value).toBe(6)
- elInput.handleInputChange('')
- await nextTick()
- expect(num.value).toBe(5)
- elInput.decrease()
- await nextTick()
- expect(num.value).toBe(4)
- })
- test('check increase and decrease button when modelValue not in [min, max]', async () => {
- const num1 = ref(-5)
- const num2 = ref(15)
- const wrapper = mount({
- setup() {
- return () => (
- <>
- <InputNumber
- ref="inputNumber1"
- v-model={num1.value}
- min={1}
- max={10}
- />
- <InputNumber
- ref="inputNumber2"
- v-model={num2.value}
- min={1}
- max={10}
- />
- </>
- )
- },
- })
- const inputNumber1 = wrapper.findComponent({ ref: 'inputNumber1' }).vm
- const inputNumber2 = wrapper.findComponent({ ref: 'inputNumber2' }).vm
- expect(num1.value).toBe(1)
- expect(num2.value).toBe(10)
- inputNumber1.decrease()
- await nextTick()
- expect(num1.value).toBe(1)
- inputNumber1.increase()
- await nextTick()
- expect(num1.value).toBe(2)
- inputNumber1.increase()
- await nextTick()
- expect(num1.value).toBe(3)
- inputNumber2.increase()
- await nextTick()
- expect(num2.value).toBe(10)
- inputNumber2.decrease()
- await nextTick()
- expect(num2.value).toBe(9)
- inputNumber2.decrease()
- await nextTick()
- expect(num2.value).toBe(8)
- })
- describe('form item accessibility integration', () => {
- test('automatic id attachment', async () => {
- const wrapper = mount(() => (
- <ElFormItem label="Foobar" data-test-ref="item">
- <InputNumber />
- </ElFormItem>
- ))
- await nextTick()
- const formItem = wrapper.find('[data-test-ref="item"]')
- const formItemLabel = formItem.find('.el-form-item__label')
- const innerInput = wrapper.find('.el-input__inner')
- expect(formItem.attributes().role).toBeFalsy()
- expect(formItemLabel.attributes().for).toBe(innerInput.attributes().id)
- })
- test('specified id attachment', async () => {
- const wrapper = mount(() => (
- <ElFormItem label="Foobar" data-test-ref="item">
- <InputNumber id="foobar" />
- </ElFormItem>
- ))
- await nextTick()
- const formItem = wrapper.find('[data-test-ref="item"]')
- const formItemLabel = formItem.find('.el-form-item__label')
- const innerInput = wrapper.find('.el-input__inner')
- expect(formItem.attributes().role).toBeFalsy()
- expect(innerInput.attributes().id).toBe('foobar')
- expect(formItemLabel.attributes().for).toBe(innerInput.attributes().id)
- })
- test('form item role is group when multiple inputs', async () => {
- const wrapper = mount(() => (
- <ElFormItem label="Foobar" data-test-ref="item">
- <InputNumber />
- <InputNumber />
- </ElFormItem>
- ))
- await nextTick()
- const formItem = wrapper.find('[data-test-ref="item"]')
- expect(formItem.attributes().role).toBe('group')
- })
- })
- })
|