-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
257 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,267 @@ | ||
import InputNumber from '../InputNumber' | ||
import { mount } from '@vue/test-utils' | ||
|
||
const globalOption = { | ||
config: { | ||
globalProperties: { | ||
$ELEMENT: { | ||
size: '', | ||
zIndex: 2000 | ||
} | ||
} | ||
} | ||
} | ||
import { nextTick, ref } from 'vue' | ||
|
||
describe('InputNumber.vue', () => { | ||
it('create ', async () => { | ||
const wrapper = mount( | ||
{ | ||
template: ` | ||
<el-input-number | ||
v-model="num" | ||
controls-position="right" | ||
@change="handleChange" | ||
:min="1" | ||
:max="10"> | ||
</el-input-number> | ||
`, | ||
data() { | ||
return { | ||
num: 1 | ||
const modelValue = ref(1) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
const input = wrapper.find('input') | ||
expect(wrapper.props('modelValue')).toEqual(1) | ||
expect(input.element.value).toEqual('1') | ||
}) | ||
|
||
it('decrease and increase', async () => { | ||
const modelValue = ref(5) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
|
||
const input = wrapper.find('input') | ||
const btnDecrease = wrapper.find('.el-input-number__decrease') | ||
await btnDecrease.trigger('keydown.enter') | ||
// await document.trigger('mouseup') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(4) | ||
expect(input.element.value).toEqual('4') | ||
|
||
const btnIncrease = wrapper.find('.el-input-number__increase') | ||
await btnIncrease.trigger('keydown.enter') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(5) | ||
expect(input.element.value).toEqual('5') | ||
}) | ||
|
||
it('disabled', async () => { | ||
const modelValue = ref(5) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
disabled: true, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
const btnDecrease = wrapper.find('.el-input-number__decrease') | ||
const btnIncrease = wrapper.find('.el-input-number__increase') | ||
const input = wrapper.find('input') | ||
|
||
await btnDecrease.trigger('keydown.enter') | ||
await nextTick() | ||
|
||
expect(modelValue.value).toEqual(5) | ||
expect(input.element.value).toEqual('5') | ||
|
||
await btnIncrease.trigger('keydown.enter') | ||
await nextTick() | ||
|
||
expect(modelValue.value).toEqual(5) | ||
expect(input.element.value).toEqual('5') | ||
}) | ||
|
||
it('step', async () => { | ||
const modelValue = ref(5) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
step: 3.2, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
|
||
const btnDecrease = wrapper.find('.el-input-number__decrease') | ||
const btnIncrease = wrapper.find('.el-input-number__increase') | ||
const input = wrapper.find('input') | ||
|
||
await btnIncrease.trigger('keydown.enter') | ||
await nextTick() | ||
|
||
expect(modelValue.value).toEqual(8.2) | ||
expect(input.element.value).toEqual('8.2') | ||
|
||
await btnDecrease.trigger('keydown.enter') | ||
await nextTick() | ||
|
||
expect(modelValue.value).toEqual(5) | ||
expect(input.element.value).toEqual('5') | ||
}) | ||
|
||
it('step strictly', async () => { | ||
const modelValue = ref(5) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
step: 2, | ||
stepStrictly: true, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
|
||
const input = wrapper.find('input') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(6) | ||
expect(input.element.value).toEqual('6') | ||
|
||
const btnDecrease = wrapper.find('.el-input-number__decrease') | ||
await btnDecrease.trigger('keydown.enter') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(4) | ||
expect(input.element.value).toEqual('4') | ||
}) | ||
|
||
it('min', async () => { | ||
const modelValue = ref(5) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
min: 6, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
|
||
const input = wrapper.find('input') | ||
const btnDecrease = wrapper.find('.el-input-number__decrease') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(6) | ||
expect(input.element.value).toEqual('6') | ||
|
||
await btnDecrease.trigger('keydown.enter') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(6) | ||
expect(input.element.value).toEqual('6') | ||
}) | ||
|
||
it('max', async () => { | ||
const modelValue = ref(100) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
max: 6, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
|
||
const input = wrapper.find('input') | ||
const btnIncrease = wrapper.find('.el-input-number__increase') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(6) | ||
expect(input.element.value).toEqual('6') | ||
|
||
await btnIncrease.trigger('keydown.enter') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(6) | ||
expect(input.element.value).toEqual('6') | ||
}) | ||
|
||
describe('precision', () => { | ||
it('precision is 2', async () => { | ||
const modelValue = ref(6.999) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
precision: 2, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
}, | ||
methods: { | ||
handleChange(value) { | ||
console.log(value) | ||
} | ||
}) | ||
|
||
const input = wrapper.find('input') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(7) | ||
expect(input.element.value).toEqual('7.00') | ||
}) | ||
|
||
it('precision greater than the precision of step', async () => { | ||
const modelValue = ref(6.999) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
precision: 2, | ||
step: 0.1, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
}, | ||
components: { | ||
[InputNumber.name]: InputNumber | ||
} | ||
}, | ||
{ | ||
global: globalOption | ||
}) | ||
|
||
const input = wrapper.find('input') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(7) | ||
expect(input.element.value).toEqual('7.00') | ||
|
||
const btnIncrease = wrapper.find('.el-input-number__increase') | ||
await btnIncrease.trigger('keydown.enter') | ||
await nextTick() | ||
expect(modelValue.value).toEqual(7.1) | ||
expect(input.element.value).toEqual('7.10') | ||
}) | ||
}) | ||
|
||
it('controls', async () => { | ||
const modelValue = ref(5) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
max: 6, | ||
controls: false, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
|
||
const btnDecrease = wrapper.find('.el-input-number__decrease') | ||
const btnIncrease = wrapper.find('.el-input-number__increase') | ||
|
||
expect(btnDecrease.exists()).toBe(false) | ||
expect(btnIncrease.exists()).toBe(false) | ||
}) | ||
|
||
it('invalid value reset', async () => { | ||
const modelValue = ref(100) | ||
const wrapper = mount(InputNumber, { | ||
props: { | ||
max: 10, | ||
min: 5, | ||
modelValue, | ||
'onUpdate:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
) | ||
const inputElm = wrapper.find('input') | ||
expect(inputElm.element.value).toEqual('1') | ||
}) | ||
|
||
const input = wrapper.find('input') | ||
|
||
await nextTick() | ||
expect(modelValue.value).toEqual(10) | ||
expect(input.element.value).toEqual('10') | ||
|
||
modelValue.value = 4 | ||
|
||
await nextTick() | ||
expect(modelValue.value).toEqual(5) | ||
expect(input.element.value).toEqual('5') | ||
}) | ||
}) |