Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 66842c8

Browse files
LeopoldthecoderQingWei-Li
authored andcommittedOct 19, 2016
Slider: add test
1 parent c640d1f commit 66842c8

File tree

7 files changed

+189
-54
lines changed

7 files changed

+189
-54
lines changed
 

‎.eslintrc

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
{
2+
"env": {
3+
"mocha": true
4+
},
5+
"globals": {
6+
"expect": true,
7+
"sinon": true
8+
},
29
"plugins": ['vue'],
310
"extends": 'elemefe',
411
"parserOptions": {

‎build/cooking.test.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
var path = require('path');
22
var cooking = require('cooking');
33
var config = require('./config');
4-
var projectRoot = path.resolve(__dirname, '../');
54
var ProgressBarPlugin = require('progress-bar-webpack-plugin');
65

76
cooking.set({
87
entry: './src/index.js',
9-
extends: ['vue2'],
8+
extends: ['vue2', 'lint'],
109
minimize: false,
1110
alias: config.alias,
1211
postcss: config.postcss,
1312
sourceMap: '#inline-source-map'
1413
});
1514

16-
cooking.add('vue.loaders.js', 'isparta');
15+
cooking.add('vue.loaders.js', 'isparta-loader!eslint-loader');
1716
cooking.add('loader.js.exclude', config.jsexclude);
1817
cooking.add('preLoader.js', {
1918
test: /\.js$/,
20-
loader: 'isparta-loader',
21-
include: path.resolve(projectRoot, 'src')
19+
loader: 'isparta-loader!eslint-loader',
20+
exclude: config.jsexclude
2221
});
2322

2423
cooking.add('plugins.process', new ProgressBarPlugin());

‎packages/slider/src/main.vue

+45-39
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
:class="{ 'show-input': showInput }"
1616
@click="onSliderClick" ref="slider">
1717
<div class="el-slider__bar" :style="{ width: currentPosition }"></div>
18-
<div class="el-slider__button-wrapper" @mouseenter="hovering = true" @mouseleave="hovering = false" :style="{left: currentPosition}" ref="button">
18+
<div
19+
class="el-slider__button-wrapper"
20+
@mouseenter="hovering = true"
21+
@mouseleave="hovering = false"
22+
@mousedown="onButtonDown"
23+
:style="{left: currentPosition}" ref="button">
1924
<div class="el-slider__button" :class="{ 'hover': hovering, 'dragging': dragging }"></div>
2025
</div>
2126
<transition name="popper-fade">
@@ -77,6 +82,9 @@
7782
showTip: false,
7883
hovering: false,
7984
dragging: false,
85+
startX: 0,
86+
currentX: 0,
87+
startPos: 0,
8088
popper: null,
8189
newPos: null,
8290
oldValue: this.value,
@@ -174,6 +182,36 @@
174182
if (!isNaN(this.value)) {
175183
this.setPosition((this.value - this.min) * 100 / (this.max - this.min));
176184
}
185+
},
186+
187+
onDragStart(event) {
188+
this.dragging = true;
189+
this.startX = event.clientX;
190+
this.startPos = parseInt(this.currentPosition, 10);
191+
},
192+
193+
onDragging(event) {
194+
if (this.dragging) {
195+
this.currentX = event.clientX;
196+
var diff = (this.currentX - this.startX) / this.$sliderWidth * 100;
197+
this.newPos = this.startPos + diff;
198+
this.setPosition(this.newPos);
199+
}
200+
},
201+
202+
onDragEnd() {
203+
if (this.dragging) {
204+
this.dragging = false;
205+
this.setPosition(this.newPos);
206+
window.removeEventListener('mousemove', this.onDragging);
207+
window.removeEventListener('mouseup', this.onDragEnd);
208+
}
209+
},
210+
211+
onButtonDown(event) {
212+
this.onDragStart(event);
213+
window.addEventListener('mousemove', this.onDragging);
214+
window.addEventListener('mouseup', this.onDragEnd);
177215
}
178216
},
179217
@@ -198,47 +236,15 @@
198236
}
199237
},
200238
201-
mounted() {
202-
var startX = 0;
203-
var currentX = 0;
204-
var startPos = 0;
205-
206-
var onDragStart = event => {
207-
this.dragging = true;
208-
startX = event.clientX;
209-
startPos = parseInt(this.currentPosition, 10);
210-
};
211-
212-
var onDragging = event => {
213-
if (this.dragging) {
214-
currentX = event.clientX;
215-
var diff = (currentX - startX) / this.$sliderWidth * 100;
216-
this.newPos = startPos + diff;
217-
this.setPosition(this.newPos);
218-
}
219-
};
220-
221-
var onDragEnd = () => {
222-
if (this.dragging) {
223-
this.dragging = false;
224-
this.setPosition(this.newPos);
225-
window.removeEventListener('mousemove', onDragging);
226-
window.removeEventListener('mouseup', onDragEnd);
227-
}
228-
};
229-
230-
this.$refs.button.addEventListener('mousedown', function(event) {
231-
onDragStart(event);
232-
window.addEventListener('mousemove', onDragging);
233-
window.addEventListener('mouseup', onDragEnd);
234-
});
235-
},
236-
237239
created() {
238-
if (typeof this.value !== 'number' || this.value < this.min || this.value > this.max) {
240+
if (typeof this.value !== 'number' || this.value < this.min) {
239241
this.$emit('input', this.min);
242+
} else if (this.value > this.max) {
243+
this.$emit('input', this.max);
240244
}
241-
this.inputValue = this.inputValue || this.value;
245+
this.$nextTick(() => {
246+
this.inputValue = this.inputValue || this.value;
247+
});
242248
},
243249
244250
beforeDestroy() {

‎packages/switch/src/component.vue

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
},
8181
computed: {
8282
hasText() {
83+
/* istanbul ignore next */
8384
return this.onText || this.offText;
8485
}
8586
},
@@ -107,6 +108,7 @@
107108
}
108109
},
109110
mounted() {
111+
/* istanbul ignore if */
110112
if (this.width === 0) {
111113
this.coreWidth = this.hasText ? 58 : 46;
112114
}

‎packages/upload/src/iframe-upload.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default {
7171
getIframeHTML(domain) {
7272
let domainScript = '';
7373
if (domain) {
74-
domainScript = `<script>document.domain="${domain}";<` + '/script>';
74+
domainScript = '<script' + `>document.domain="${domain}";<` + '/script>';
7575
}
7676
return `
7777
<!DOCTYPE html>

‎test/unit/.eslintrc

-9
This file was deleted.

‎test/unit/specs/slider.spec.js

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { createTest, createVue } from '../util';
2+
import Slider from 'packages/slider';
3+
import Vue from 'vue';
4+
5+
describe('Slider', () => {
6+
it('create', () => {
7+
const vm = createTest(Slider);
8+
const popup = vm.$el.querySelector('.el-slider__pop');
9+
expect(popup.textContent).to.equal('0');
10+
});
11+
12+
it('should not exceed min and max', done => {
13+
const vm = createVue({
14+
template: `
15+
<div>
16+
<el-slider v-model="value" :min="50">
17+
</el-slider>
18+
</div>
19+
`,
20+
21+
data() {
22+
return {
23+
value: 50
24+
};
25+
}
26+
}, true);
27+
setTimeout(() => {
28+
vm.value = 40;
29+
Vue.nextTick(() => {
30+
expect(vm.value).to.equal(50);
31+
vm.value = 120;
32+
Vue.nextTick(() => {
33+
expect(vm.value).to.equal(100);
34+
done();
35+
});
36+
});
37+
}, 100);
38+
});
39+
40+
it('show tooltip', () => {
41+
const vm = createTest(Slider);
42+
const popup = vm.$el.querySelector('.el-slider__pop');
43+
vm.onDragStart({ clientX: 0 });
44+
expect(getComputedStyle(popup).display).to.not.equal('none');
45+
vm.onDragEnd();
46+
expect(popup.style.display).to.equal('none');
47+
});
48+
49+
it('drag', done => {
50+
const vm = createVue({
51+
template: `
52+
<div>
53+
<el-slider v-model="value"></el-slider>
54+
</div>
55+
`,
56+
57+
data() {
58+
return {
59+
value: 0
60+
};
61+
}
62+
}, true);
63+
const slider = vm.$children[0];
64+
setTimeout(() => {
65+
slider.onButtonDown({ clientX: 0 });
66+
slider.onDragging({ clientX: 100 });
67+
slider.onDragEnd();
68+
expect(vm.value > 0).to.true;
69+
done();
70+
}, 150);
71+
});
72+
73+
it('click', done => {
74+
const vm = createVue({
75+
template: `
76+
<div>
77+
<el-slider v-model="value"></el-slider>
78+
</div>
79+
`,
80+
81+
data() {
82+
return {
83+
value: 0
84+
};
85+
}
86+
}, true);
87+
const slider = vm.$children[0];
88+
setTimeout(() => {
89+
slider.onSliderClick({ clientX: 100 });
90+
setTimeout(() => {
91+
expect(vm.value > 0).to.true;
92+
done();
93+
}, 150);
94+
}, 150);
95+
});
96+
97+
it('show input', done => {
98+
const vm = createVue({
99+
template: `
100+
<div>
101+
<el-slider v-model="value" show-input></el-slider>
102+
</div>
103+
`,
104+
105+
data() {
106+
return {
107+
value: 0
108+
};
109+
}
110+
}, true);
111+
setTimeout(() => {
112+
const inputNumber = vm.$el.querySelector('.el-input-number').__vue__;
113+
inputNumber.currentValue = 40;
114+
setTimeout(() => {
115+
expect(vm.value).to.equal(40);
116+
done();
117+
}, 150);
118+
}, 150);
119+
});
120+
121+
it('show stops', done => {
122+
const vm = createTest(Slider, {
123+
showStops: true,
124+
step: 10
125+
}, true);
126+
const stops = vm.$el.querySelectorAll('.el-slider__stop');
127+
expect(stops.length).to.equal(9);
128+
done();
129+
});
130+
});

0 commit comments

Comments
 (0)
Please sign in to comment.