From 66842c80e8a027b93bd020a408011e928f3ccfc2 Mon Sep 17 00:00:00 2001 From: Leopoldthecoder Date: Wed, 19 Oct 2016 14:53:32 +0800 Subject: [PATCH] Slider: add test --- .eslintrc | 7 ++ build/cooking.test.js | 9 +- packages/slider/src/main.vue | 84 +++++++++-------- packages/switch/src/component.vue | 2 + packages/upload/src/iframe-upload.vue | 2 +- test/unit/.eslintrc | 9 -- test/unit/specs/slider.spec.js | 130 ++++++++++++++++++++++++++ 7 files changed, 189 insertions(+), 54 deletions(-) delete mode 100644 test/unit/.eslintrc create mode 100644 test/unit/specs/slider.spec.js diff --git a/.eslintrc b/.eslintrc index 4bcff8304..ba8a1b188 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,11 @@ { + "env": { + "mocha": true + }, + "globals": { + "expect": true, + "sinon": true + }, "plugins": ['vue'], "extends": 'elemefe', "parserOptions": { diff --git a/build/cooking.test.js b/build/cooking.test.js index cdac935d7..f11d542fc 100644 --- a/build/cooking.test.js +++ b/build/cooking.test.js @@ -1,24 +1,23 @@ var path = require('path'); var cooking = require('cooking'); var config = require('./config'); -var projectRoot = path.resolve(__dirname, '../'); var ProgressBarPlugin = require('progress-bar-webpack-plugin'); cooking.set({ entry: './src/index.js', - extends: ['vue2'], + extends: ['vue2', 'lint'], minimize: false, alias: config.alias, postcss: config.postcss, sourceMap: '#inline-source-map' }); -cooking.add('vue.loaders.js', 'isparta'); +cooking.add('vue.loaders.js', 'isparta-loader!eslint-loader'); cooking.add('loader.js.exclude', config.jsexclude); cooking.add('preLoader.js', { test: /\.js$/, - loader: 'isparta-loader', - include: path.resolve(projectRoot, 'src') + loader: 'isparta-loader!eslint-loader', + exclude: config.jsexclude }); cooking.add('plugins.process', new ProgressBarPlugin()); diff --git a/packages/slider/src/main.vue b/packages/slider/src/main.vue index 576efa5d8..d5569af23 100644 --- a/packages/slider/src/main.vue +++ b/packages/slider/src/main.vue @@ -15,7 +15,12 @@ :class="{ 'show-input': showInput }" @click="onSliderClick" ref="slider">
-
+
@@ -77,6 +82,9 @@ showTip: false, hovering: false, dragging: false, + startX: 0, + currentX: 0, + startPos: 0, popper: null, newPos: null, oldValue: this.value, @@ -174,6 +182,36 @@ if (!isNaN(this.value)) { this.setPosition((this.value - this.min) * 100 / (this.max - this.min)); } + }, + + onDragStart(event) { + this.dragging = true; + this.startX = event.clientX; + this.startPos = parseInt(this.currentPosition, 10); + }, + + onDragging(event) { + if (this.dragging) { + this.currentX = event.clientX; + var diff = (this.currentX - this.startX) / this.$sliderWidth * 100; + this.newPos = this.startPos + diff; + this.setPosition(this.newPos); + } + }, + + onDragEnd() { + if (this.dragging) { + this.dragging = false; + this.setPosition(this.newPos); + window.removeEventListener('mousemove', this.onDragging); + window.removeEventListener('mouseup', this.onDragEnd); + } + }, + + onButtonDown(event) { + this.onDragStart(event); + window.addEventListener('mousemove', this.onDragging); + window.addEventListener('mouseup', this.onDragEnd); } }, @@ -198,47 +236,15 @@ } }, - mounted() { - var startX = 0; - var currentX = 0; - var startPos = 0; - - var onDragStart = event => { - this.dragging = true; - startX = event.clientX; - startPos = parseInt(this.currentPosition, 10); - }; - - var onDragging = event => { - if (this.dragging) { - currentX = event.clientX; - var diff = (currentX - startX) / this.$sliderWidth * 100; - this.newPos = startPos + diff; - this.setPosition(this.newPos); - } - }; - - var onDragEnd = () => { - if (this.dragging) { - this.dragging = false; - this.setPosition(this.newPos); - window.removeEventListener('mousemove', onDragging); - window.removeEventListener('mouseup', onDragEnd); - } - }; - - this.$refs.button.addEventListener('mousedown', function(event) { - onDragStart(event); - window.addEventListener('mousemove', onDragging); - window.addEventListener('mouseup', onDragEnd); - }); - }, - created() { - if (typeof this.value !== 'number' || this.value < this.min || this.value > this.max) { + if (typeof this.value !== 'number' || this.value < this.min) { this.$emit('input', this.min); + } else if (this.value > this.max) { + this.$emit('input', this.max); } - this.inputValue = this.inputValue || this.value; + this.$nextTick(() => { + this.inputValue = this.inputValue || this.value; + }); }, beforeDestroy() { diff --git a/packages/switch/src/component.vue b/packages/switch/src/component.vue index 5a2617136..b8d813ed5 100644 --- a/packages/switch/src/component.vue +++ b/packages/switch/src/component.vue @@ -80,6 +80,7 @@ }, computed: { hasText() { + /* istanbul ignore next */ return this.onText || this.offText; } }, @@ -107,6 +108,7 @@ } }, mounted() { + /* istanbul ignore if */ if (this.width === 0) { this.coreWidth = this.hasText ? 58 : 46; } diff --git a/packages/upload/src/iframe-upload.vue b/packages/upload/src/iframe-upload.vue index 236d41e09..013642686 100644 --- a/packages/upload/src/iframe-upload.vue +++ b/packages/upload/src/iframe-upload.vue @@ -71,7 +71,7 @@ export default { getIframeHTML(domain) { let domainScript = ''; if (domain) { - domainScript = `