Skip to content

Commit

Permalink
feat(slider): add unit function
Browse files Browse the repository at this point in the history
feat(slider): add unit test function

fix(slider): reduce some code

feat(slider): add unit test function
  • Loading branch information
xiaoboRao committed Sep 23, 2021
1 parent 8b0511c commit 9a8879d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
58 changes: 56 additions & 2 deletions devui/slider/__tests__/slider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
import { mount } from '@vue/test-utils';
import DSlider from '../src/slider'
import { mount } from '@vue/test-utils'
import { nextTick,ref } from 'vue'
import DSlider from '../src/slider'

describe('d-slider', () => {
it('slider maxValue && minValue work', () => {
const wrapper = mount(DSlider, {
props: {
max: 50,
min: 2,
},
})
const max = wrapper.find('.devui-max_count')
const min = wrapper.find('.devui-min_count')
expect(min.text()).toBe('2')
expect(max.text()).toBe('50')
})
it('slider v-model work', async () => {
const value=ref(5);
const wrapper = mount({
components: { DSlider },
template: `
<d-slider v-model:modelValue="modelValue" showInput />
`,
setup() {
return {
modelValue:value,
}
},
})
const input = wrapper.find('input')
expect(input.element.value).toBe('5')
input.setValue(10)
await nextTick()
expect(value.value).toBe(10)
})
it('slider showInput work', () => {
const wrapper = mount(DSlider, {
props: {
showInput: true,
},
})
const dInput = wrapper.find('.devui-input__outWrap')
expect(dInput.exists()).toBeTruthy()
})

it('slider disabled work', () => {
const wrapper = mount(DSlider, {
props: {
disabled: true,
},
})
const slider = wrapper.find('.devui-slider__runway')
expect(slider.classes()).toContain('disabled')
})
})
3 changes: 2 additions & 1 deletion devui/slider/src/slider.scss
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
color: $devui-text;
}

.devui-input__wrap {
.devui-input__outWrap {
position: absolute;
right: -60px;
top: -12px;
Expand All @@ -99,6 +99,7 @@

input {
width: 40px;
text-align: center;
}
}
}
7 changes: 4 additions & 3 deletions devui/slider/src/slider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineComponent, ref, computed, onMounted } from 'vue';
import { sliderProps } from './slider-types';
import { Input } from '../../input';
import { Input } from '../../input';
import './slider.scss';

export default defineComponent({
Expand All @@ -22,7 +22,7 @@ export default defineComponent({
//当前的位置以百分比显示
const percentDispaly = ref<string>('');
const renderShowInput = () => {
return props.showInput ? <d-input onInput={handleOnInput} value={inputValue.value + ''}></d-input> : '';
return props.showInput ? <div class='devui-input__outWrap'><input onInput={handleOnInput} value={inputValue.value + ''}></input></div> : '';
};

//当传入modelValue时用以定位button的位置
Expand Down Expand Up @@ -128,7 +128,7 @@ export default defineComponent({
}
//输入框内的值
function handleOnInput(event) {
inputValue.value = parseInt(event.target.value);
inputValue.value=parseInt(event.target.value);
if (!inputValue.value) {
inputValue.value = props.min;
percentDispaly.value = '0%';
Expand All @@ -142,6 +142,7 @@ export default defineComponent({
const re = /^(?:[1-9]?\d|100)$/;
if (re.test(`${inputValue.value}`)) {
percentDispaly.value = ((inputValue.value - props.min) * 100) / (props.max - props.min) + '%';
ctx.emit('update:modelValue', inputValue.value);
}
}
}
Expand Down

0 comments on commit 9a8879d

Please sign in to comment.