mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
Merge pull request #91 from chenjiahan/dev
CellSwipe: improve test coverage && code review
This commit is contained in:
commit
28c3354419
@ -26,6 +26,7 @@
|
||||
"test": "karma start test/unit/karma.conf.js --single-run",
|
||||
"test:coverage": "open test/unit/coverage/lcov-report/index.html",
|
||||
"test:watch": "karma start test/unit/karma.conf.js",
|
||||
"test:single": "node ./test/unit/selector.js",
|
||||
"release": "npm run bootstrap && sh build/release.sh"
|
||||
},
|
||||
"repository": {
|
||||
|
@ -1,15 +1,19 @@
|
||||
<template>
|
||||
<div v-clickoutside:touchstart="swipeMove" @click="swipeMove()" @touchstart="startDrag" @touchmove="onDrag" @touchend="endDrag" class="van-cell-swipe" ref="cell">
|
||||
<div class="van-cell-wrapper">
|
||||
<slot>单元格内容</slot>
|
||||
</div>
|
||||
<div class="van-cell-left">
|
||||
<div ref="left">
|
||||
<div
|
||||
v-clickoutside:touchstart="swipeMove"
|
||||
class="van-cell-swipe"
|
||||
@click="swipeMove()"
|
||||
@touchstart="startDrag"
|
||||
@touchmove="onDrag"
|
||||
@touchend="endDrag"
|
||||
@touchcancel="endDrag"
|
||||
>
|
||||
<div class="van-cell-swipe__wrapper" :style="wrapperStyle" @transitionend="swipe = false">
|
||||
<div class="van-cell-swipe__left" v-if="leftWidth">
|
||||
<slot name="left"></slot>
|
||||
</div>
|
||||
</div>
|
||||
<div class="van-cell-right">
|
||||
<div ref="right">
|
||||
<slot></slot>
|
||||
<div class="van-cell-swipe__right" v-if="rightWidth">
|
||||
<slot name="right"></slot>
|
||||
</div>
|
||||
</div>
|
||||
@ -17,112 +21,101 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { once } from '../utils/dom';
|
||||
import Clickoutside from '../utils/clickoutside';
|
||||
|
||||
export default {
|
||||
name: 'van-cell-swipe',
|
||||
|
||||
props: {
|
||||
'leftWidth': { type: Number, default: 0 },
|
||||
'rightWidth': { type: Number, default: 0 }
|
||||
},
|
||||
directives: { Clickoutside },
|
||||
data() {
|
||||
return {
|
||||
start: { x: 0, y: 0 }
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
leftDefaultTransform() {
|
||||
return this.translate3d(-this.leftWidth - 1);
|
||||
leftWidth: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
rightDefaultTransform() {
|
||||
return this.translate3d(this.rightWidth);
|
||||
rightWidth: {
|
||||
type: Number,
|
||||
default: 0
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.wrap = this.$refs.cell.querySelector('.van-cell-wrapper');
|
||||
this.leftElm = this.$refs.left;
|
||||
this.leftWrapElm = this.leftElm.parentNode;
|
||||
this.leftWrapElm.style.webkitTransform = this.leftDefaultTransform;
|
||||
|
||||
this.rightElm = this.$refs.right;
|
||||
this.rightWrapElm = this.rightElm.parentNode;
|
||||
this.rightWrapElm.style.webkitTransform = this.rightDefaultTransform;
|
||||
directives: {
|
||||
Clickoutside
|
||||
},
|
||||
|
||||
data() {
|
||||
return {
|
||||
offset: 0
|
||||
};
|
||||
},
|
||||
|
||||
computed: {
|
||||
wrapperStyle() {
|
||||
return {
|
||||
transform: `translate3d(${this.offset}px, 0, 0)`
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
resetSwipeStatus() {
|
||||
this.swiping = false; // 是否正在拖动
|
||||
this.opened = true; // 记录是否滑动左右 或者 注册
|
||||
this.offsetLeft = 0; // 记录单次拖动的拖动距离
|
||||
},
|
||||
translate3d(offset) {
|
||||
return `translate3d(${offset}px, 0, 0)`;
|
||||
},
|
||||
|
||||
swipeMove(offset = 0) {
|
||||
this.wrap.style.webkitTransform = this.translate3d(offset);
|
||||
this.rightWrapElm.style.webkitTransform = this.translate3d(this.rightWidth + offset);
|
||||
this.leftWrapElm.style.webkitTransform = this.translate3d(-this.leftWidth + offset);
|
||||
this.offset = offset;
|
||||
offset && (this.swiping = true);
|
||||
},
|
||||
|
||||
swipeLeaveTransition(direction) {
|
||||
setTimeout(() => {
|
||||
this.swipeLeave = true;
|
||||
// left
|
||||
if (direction > 0 && -this.offsetLeft > this.rightWidth * 0.4 && this.rightWidth > 0) {
|
||||
this.swipeMove(-this.rightWidth);
|
||||
this.resetSwipeStatus();
|
||||
return;
|
||||
// right
|
||||
} else if (direction < 0 && this.offsetLeft > this.leftWidth * 0.4 && this.leftWidth > 0) {
|
||||
this.swipeMove(this.leftWidth);
|
||||
this.resetSwipeStatus();
|
||||
return;
|
||||
} else {
|
||||
this.swipeMove(0);
|
||||
once(this.wrap, 'webkitTransitionEnd', () => {
|
||||
this.wrap.style.webkitTransform = '';
|
||||
this.rightWrapElm.style.webkitTransform = this.rightDefaultTransform;
|
||||
this.leftWrapElm.style.webkitTransform = this.leftDefaultTransform;
|
||||
this.swipeLeave = false;
|
||||
this.swiping = false;
|
||||
});
|
||||
}
|
||||
}, 0);
|
||||
const { offset, leftWidth, rightWidth } = this;
|
||||
// right
|
||||
if (direction > 0 && -offset > rightWidth * 0.4 && rightWidth > 0) {
|
||||
this.swipeMove(-rightWidth);
|
||||
this.resetSwipeStatus();
|
||||
}
|
||||
// left
|
||||
else if (direction < 0 && offset >leftWidth * 0.4 && leftWidth > 0) {
|
||||
this.swipeMove(leftWidth);
|
||||
this.resetSwipeStatus();
|
||||
} else {
|
||||
this.swipeMove();
|
||||
}
|
||||
},
|
||||
startDrag(evt) {
|
||||
evt = evt.changedTouches ? evt.changedTouches[0] : evt;
|
||||
this.dragging = true;
|
||||
this.start.x = evt.pageX;
|
||||
this.start.y = evt.pageY;
|
||||
|
||||
startDrag(event) {
|
||||
this.startX = event.changedTouches[0].pageX;
|
||||
this.startY = event.changedTouches[0].pageY;
|
||||
},
|
||||
onDrag(evt) {
|
||||
|
||||
onDrag(event) {
|
||||
if (this.opened) {
|
||||
!this.swiping && this.swipeMove(0);
|
||||
!this.swiping && this.swipeMove();
|
||||
this.opened = false;
|
||||
return;
|
||||
}
|
||||
if (!this.dragging) return;
|
||||
let swiping;
|
||||
const e = evt.changedTouches ? evt.changedTouches[0] : evt;
|
||||
const offsetTop = e.pageY - this.start.y;
|
||||
const offsetLeft = this.offsetLeft = e.pageX - this.start.x;
|
||||
|
||||
const offsetTop = event.changedTouches[0].pageY - this.startY;
|
||||
const offsetLeft = event.changedTouches[0].pageX - this.startX;
|
||||
if ((offsetLeft < 0 && -offsetLeft > this.rightWidth) ||
|
||||
(offsetLeft > 0 && offsetLeft > this.leftWidth) ||
|
||||
(offsetLeft > 0 && !this.leftWidth) ||
|
||||
(offsetLeft < 0 && !this.rightWidth)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const y = Math.abs(offsetTop);
|
||||
const x = Math.abs(offsetLeft);
|
||||
swiping = !(x < 5 || (x >= 5 && y >= x * 1.73));
|
||||
if (!swiping) return;
|
||||
evt.preventDefault();
|
||||
this.swipeMove(offsetLeft);
|
||||
const swiping = !(x < 5 || (x >= 5 && y >= x * 1.73));
|
||||
if (swiping) {
|
||||
event.preventDefault();
|
||||
this.swipeMove(offsetLeft);
|
||||
};
|
||||
},
|
||||
|
||||
endDrag() {
|
||||
if (!this.swiping) return;
|
||||
this.swipeLeaveTransition(this.offsetLeft > 0 ? -1 : 1);
|
||||
if (this.swiping) {
|
||||
this.swipeLeaveTransition(this.offset > 0 ? -1 : 1);
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1,11 +0,0 @@
|
||||
import Vue from 'vue';
|
||||
|
||||
export const once = function(el, event, fn) {
|
||||
const listener = function() {
|
||||
if (fn) {
|
||||
fn.apply(this, arguments);
|
||||
}
|
||||
el.removeEventListener(event, listener);
|
||||
};
|
||||
el.addEventListener(event, listener);
|
||||
};
|
@ -1,29 +1,26 @@
|
||||
.van-cell {
|
||||
&-swipe {
|
||||
position: relative;
|
||||
min-height: 48px;
|
||||
overflow: hidden;
|
||||
.van-cell-swipe {
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
|
||||
.van-cell-wrapper,
|
||||
.van-cell-left,
|
||||
.van-cell-right {
|
||||
transition: transform 150ms ease-in-out;
|
||||
}
|
||||
&__wrapper,
|
||||
&__left,
|
||||
&__right {
|
||||
transition: transform .15s ease-in-out;
|
||||
}
|
||||
|
||||
&-left,
|
||||
&-right {
|
||||
position: absolute;
|
||||
height: 100%;
|
||||
&__left,
|
||||
&__right {
|
||||
top: 0;
|
||||
height: 100%;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
&-left {
|
||||
&__left {
|
||||
left: 0;
|
||||
transform: translate3d(-100%, 0, 0);
|
||||
}
|
||||
|
||||
&-right {
|
||||
&__right {
|
||||
right: 0;
|
||||
transform: translate3d(100%, 0, 0);
|
||||
}
|
||||
|
@ -2,96 +2,96 @@ const path = require('path');
|
||||
const webpack = require('webpack');
|
||||
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
|
||||
|
||||
const webpackConfig = {
|
||||
output: {
|
||||
path: path.resolve(process.cwd(), 'dist'),
|
||||
publicPath: '/dist/',
|
||||
filename: '[name].js',
|
||||
chunkFilename: '[id].js',
|
||||
libraryTarget: 'umd'
|
||||
},
|
||||
plugins: [
|
||||
new ProgressBarPlugin(),
|
||||
new webpack.LoaderOptionsPlugin({
|
||||
minimize: true,
|
||||
options: {
|
||||
babel: {
|
||||
presets: ['env'],
|
||||
plugins: ['transform-runtime', 'transform-vue-jsx']
|
||||
},
|
||||
vue: {
|
||||
autoprefixer: false,
|
||||
preserveWhitespace: false
|
||||
function getWebpackConfig(testFileName) {
|
||||
return {
|
||||
output: {
|
||||
path: path.resolve(process.cwd(), 'dist'),
|
||||
publicPath: '/dist/',
|
||||
filename: '[name].js',
|
||||
chunkFilename: '[id].js',
|
||||
libraryTarget: 'umd'
|
||||
},
|
||||
plugins: [
|
||||
new ProgressBarPlugin(),
|
||||
new webpack.LoaderOptionsPlugin({
|
||||
minimize: true,
|
||||
options: {
|
||||
babel: {
|
||||
presets: ['env'],
|
||||
plugins: ['transform-runtime', 'transform-vue-jsx']
|
||||
},
|
||||
vue: {
|
||||
autoprefixer: false,
|
||||
preserveWhitespace: false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
],
|
||||
stats: 'errors-only',
|
||||
resolve: {
|
||||
modules: [
|
||||
path.resolve(process.cwd(), 'node_modules'),
|
||||
'node_modules'
|
||||
}),
|
||||
new webpack.DefinePlugin({
|
||||
'process.env': {
|
||||
TEST_FILE: `"${testFileName}"`
|
||||
}
|
||||
})
|
||||
],
|
||||
extensions: ['.js', '.json', '.vue'],
|
||||
alias: {
|
||||
src: path.resolve(process.cwd(), 'src'),
|
||||
packages: path.resolve(process.cwd(), 'packages'),
|
||||
examples: path.resolve(process.cwd(), 'examples'),
|
||||
vue$: 'vue/dist/vue.common.js'
|
||||
}
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
enforce: 'pre',
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules|vue-router\/|vue-loader\/|docs|test|src\/index|src\/utils|src\/mixins|packages\/swipe/,
|
||||
use: ['isparta-loader']
|
||||
},
|
||||
{
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules|vue-router\/|vue-loader\//,
|
||||
use: ['babel-loader']
|
||||
},
|
||||
{
|
||||
test: /\.(css|pcss)$/,
|
||||
use: [
|
||||
'style-loader',
|
||||
'css-loader',
|
||||
'postcss-loader'
|
||||
]
|
||||
},
|
||||
{
|
||||
test: /\.(gif|png|jpe?g)(\?\S*)?$/,
|
||||
use: [{
|
||||
loader: 'url-loader',
|
||||
options: {
|
||||
query: {
|
||||
limit: 10000,
|
||||
name: 'static/[name].[hash:7].[ext]'
|
||||
}
|
||||
}
|
||||
}]
|
||||
},
|
||||
{
|
||||
test: /\.vue$/,
|
||||
use: [{
|
||||
loader: 'vue-loader',
|
||||
options: {
|
||||
loaders: {
|
||||
css: [
|
||||
'style-loader',
|
||||
'css-loader',
|
||||
'postcss-loader'
|
||||
],
|
||||
js: ['isparta-loader']
|
||||
}
|
||||
}
|
||||
}]
|
||||
stats: 'errors-only',
|
||||
resolve: {
|
||||
modules: [path.resolve(process.cwd(), 'node_modules'), 'node_modules'],
|
||||
extensions: ['.js', '.json', '.vue'],
|
||||
alias: {
|
||||
src: path.resolve(process.cwd(), 'src'),
|
||||
packages: path.resolve(process.cwd(), 'packages'),
|
||||
examples: path.resolve(process.cwd(), 'examples'),
|
||||
vue$: 'vue/dist/vue.common.js'
|
||||
}
|
||||
]
|
||||
},
|
||||
devtool: '#inline-source-map'
|
||||
};
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
enforce: 'pre',
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules|vue-router\/|vue-loader\/|docs|test|src\/index|src\/utils|src\/mixins|packages\/swipe/,
|
||||
use: ['isparta-loader']
|
||||
},
|
||||
{
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules|vue-router\/|vue-loader\//,
|
||||
use: ['babel-loader']
|
||||
},
|
||||
{
|
||||
test: /\.(css|pcss)$/,
|
||||
use: ['style-loader', 'css-loader', 'postcss-loader']
|
||||
},
|
||||
{
|
||||
test: /\.(gif|png|jpe?g)(\?\S*)?$/,
|
||||
use: [
|
||||
{
|
||||
loader: 'url-loader',
|
||||
options: {
|
||||
query: {
|
||||
limit: 10000,
|
||||
name: 'static/[name].[hash:7].[ext]'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
test: /\.vue$/,
|
||||
use: [
|
||||
{
|
||||
loader: 'vue-loader',
|
||||
options: {
|
||||
loaders: {
|
||||
css: ['style-loader', 'css-loader', 'postcss-loader'],
|
||||
js: ['isparta-loader']
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
devtool: '#inline-source-map'
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = webpackConfig;
|
||||
module.exports = getWebpackConfig;
|
||||
|
@ -1,5 +1,13 @@
|
||||
require('packages/vant-css/src/index.css');
|
||||
|
||||
// require all test files (files that ends with .spec.js)
|
||||
// 读取配置文件,判断运行单个测试文件还是所有测试文件
|
||||
const testsReq = require.context('./specs', true, /\.spec$/);
|
||||
testsReq.keys().forEach(testsReq);
|
||||
if (process.env.TEST_FILE) {
|
||||
testsReq.keys().forEach((file) => {
|
||||
if (file.indexOf(process.env.TEST_FILE) !== -1) {
|
||||
testsReq(file);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
testsReq.keys().forEach(testsReq);
|
||||
}
|
@ -4,7 +4,7 @@ require('babel-core/register')({
|
||||
presets: [require('babel-preset-env')]
|
||||
});
|
||||
|
||||
var webpackConfig = require('./get-webpack-conf');
|
||||
var getWebpackConfig = require('./get-webpack-conf');
|
||||
var travis = process.env.TRAVIS;
|
||||
|
||||
module.exports = function(config) {
|
||||
@ -16,7 +16,7 @@ module.exports = function(config) {
|
||||
preprocessors: {
|
||||
'./index.js': ['webpack', 'sourcemap']
|
||||
},
|
||||
webpack: webpackConfig,
|
||||
webpack: getWebpackConfig(getTestFileName()),
|
||||
webpackMiddleware: {
|
||||
noInfo: true
|
||||
},
|
||||
@ -30,3 +30,8 @@ module.exports = function(config) {
|
||||
singleRun: false
|
||||
});
|
||||
};
|
||||
|
||||
function getTestFileName() {
|
||||
const flagIndex = process.argv.indexOf('--file');
|
||||
return flagIndex !== -1 ? process.argv[flagIndex + 1] : '';
|
||||
}
|
||||
|
19
test/unit/selector.js
Normal file
19
test/unit/selector.js
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* 运行单个测试文件
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const inquirer = require('inquirer');
|
||||
const path = require('path');
|
||||
const shell = require('shelljs');
|
||||
const files = fs.readdirSync(path.resolve(__dirname, './specs'));
|
||||
|
||||
inquirer.prompt([{
|
||||
type: 'list',
|
||||
name: 'select',
|
||||
message: '请选择要运行的测试文件:',
|
||||
choices: files
|
||||
}], (result) => {
|
||||
const file = result.select.replace('.spec.js', '');
|
||||
shell.exec('karma start test/unit/karma.conf.js --color alway --file ' + file);
|
||||
});
|
@ -1,141 +1,117 @@
|
||||
import CellSwipe from 'packages/cell-swipe';
|
||||
import { mount } from 'avoriaz';
|
||||
import { triggerTouch } from '../utils';
|
||||
|
||||
const defaultProps = {
|
||||
propsData: {
|
||||
leftWidth: 100,
|
||||
rightWidth: 100
|
||||
}
|
||||
}
|
||||
|
||||
describe('CellSwipe', () => {
|
||||
let wrapper;
|
||||
afterEach(() => {
|
||||
wrapper && wrapper.destroy();
|
||||
});
|
||||
|
||||
it('render left or right part when has width', () => {
|
||||
wrapper = mount(CellSwipe, defaultProps);
|
||||
expect(wrapper.find('.van-cell-swipe__left').length).to.equal(1);
|
||||
expect(wrapper.find('.van-cell-swipe__right').length).to.equal(1);
|
||||
});
|
||||
|
||||
it('create a CellSwipe', () => {
|
||||
wrapper = mount(CellSwipe, {
|
||||
propsData: {
|
||||
leftWidth: 2,
|
||||
rightWidth: 2
|
||||
}
|
||||
it('not render left or right part when width is 0', () => {
|
||||
wrapper = mount(CellSwipe);
|
||||
expect(wrapper.find('.van-cell-swipe__left').length).to.equal(0);
|
||||
expect(wrapper.find('.van-cell-swipe__right').length).to.equal(0);
|
||||
});
|
||||
|
||||
it('drag and show left part', () => {
|
||||
wrapper = mount(CellSwipe, defaultProps);
|
||||
|
||||
triggerTouch(wrapper, 'touchstart', 0, 0);
|
||||
expect(wrapper.vm.startX).to.equal(0);
|
||||
expect(wrapper.vm.startY).to.equal(0);
|
||||
|
||||
triggerTouch(wrapper, 'touchmove', 50, 0);
|
||||
expect(wrapper.vm.offset).to.equal(50);
|
||||
|
||||
triggerTouch(wrapper, 'touchend', 50, 0);
|
||||
expect(wrapper.vm.offset).to.equal(100);
|
||||
wrapper.vm.$nextTick(() => {
|
||||
expect(wrapper.vm.opened).to.be.true;
|
||||
});
|
||||
wrapper.vm.startDrag({
|
||||
pageX: 0,
|
||||
pageY: 0
|
||||
});
|
||||
|
||||
it('drag and show right part', () => {
|
||||
wrapper = mount(CellSwipe, defaultProps);
|
||||
|
||||
triggerTouch(wrapper, 'touchstart', 0, 0);
|
||||
triggerTouch(wrapper, 'touchmove', -50, 0);
|
||||
triggerTouch(wrapper, 'touchend', -50, 0);
|
||||
expect(wrapper.vm.offset).to.equal(-100);
|
||||
wrapper.vm.$nextTick(() => {
|
||||
expect(wrapper.vm.opened).to.be.true;
|
||||
});
|
||||
wrapper.vm.onDrag({
|
||||
preventDefault() {},
|
||||
pageY: 0,
|
||||
pageX: 50
|
||||
});
|
||||
|
||||
it('drag and reset left part', () => {
|
||||
wrapper = mount(CellSwipe, defaultProps);
|
||||
|
||||
triggerTouch(wrapper, 'touchstart', 0, 0);
|
||||
triggerTouch(wrapper, 'touchmove', 10, 0);
|
||||
triggerTouch(wrapper, 'touchmove', 30, 0);
|
||||
triggerTouch(wrapper, 'touchend', 30, 0);
|
||||
expect(wrapper.vm.offset).to.equal(0);
|
||||
});
|
||||
|
||||
it('drag and reset right part', () => {
|
||||
wrapper = mount(CellSwipe, defaultProps);
|
||||
|
||||
triggerTouch(wrapper, 'touchstart', 0, 0);
|
||||
triggerTouch(wrapper, 'touchmove', -10, 0);
|
||||
triggerTouch(wrapper, 'touchmove', -30, 0);
|
||||
triggerTouch(wrapper, 'touchend', -30, 0);
|
||||
expect(wrapper.vm.offset).to.equal(0);
|
||||
});
|
||||
|
||||
it('drag distance out of ranges', () => {
|
||||
wrapper = mount(CellSwipe, defaultProps);
|
||||
|
||||
triggerTouch(wrapper, 'touchstart', 0, 0);
|
||||
triggerTouch(wrapper, 'touchmove', 1000, 0);
|
||||
expect(wrapper.vm.offset).to.equal(0);
|
||||
});
|
||||
|
||||
it('drag and hide left part', (done) => {
|
||||
wrapper = mount(CellSwipe, defaultProps);
|
||||
|
||||
triggerTouch(wrapper, 'touchstart', 0, 0);
|
||||
triggerTouch(wrapper, 'touchmove', 20, 0);
|
||||
triggerTouch(wrapper, 'touchmove', 50, 0);
|
||||
triggerTouch(wrapper, 'touchend', 50, 0);
|
||||
expect(wrapper.vm.offset).to.equal(100);
|
||||
wrapper.vm.$nextTick(() => {
|
||||
expect(wrapper.vm.opened).to.be.true;
|
||||
|
||||
triggerTouch(wrapper, 'touchstart', 0, 0);
|
||||
triggerTouch(wrapper, 'touchmove', 1, 0);
|
||||
|
||||
wrapper.vm.$nextTick(() => {
|
||||
expect(wrapper.vm.offset).to.equal(0);
|
||||
expect(wrapper.vm.opened).to.be.false;
|
||||
done();
|
||||
});
|
||||
});
|
||||
wrapper.vm.offsetLeft = -20;
|
||||
wrapper.vm.rightWidth = 10;
|
||||
wrapper.vm.swipeLeaveTransition(1);
|
||||
wrapper.vm.endDrag();
|
||||
expect(wrapper.hasClass('van-cell-swipe')).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('CellSwipe-left', () => {
|
||||
let wrapper;
|
||||
afterEach(() => {
|
||||
wrapper && wrapper.destroy();
|
||||
});
|
||||
|
||||
it('create a CellSwipe left', () => {
|
||||
wrapper = mount(CellSwipe, {
|
||||
propsData: {
|
||||
leftWidth: 2,
|
||||
rightWidth: 2
|
||||
}
|
||||
});
|
||||
wrapper.vm.startDrag({
|
||||
changedTouches: [{
|
||||
pageX: 0,
|
||||
pageY: 0
|
||||
}
|
||||
]
|
||||
});
|
||||
wrapper.vm.onDrag({
|
||||
preventDefault() {},
|
||||
changedTouches: [{
|
||||
pageX: 0,
|
||||
pageY: -50
|
||||
}
|
||||
]
|
||||
});
|
||||
wrapper.vm.offsetLeft = 20;
|
||||
wrapper.vm.rightWidth = 10;
|
||||
wrapper.vm.swipeLeaveTransition(-1);
|
||||
wrapper.vm.endDrag();
|
||||
expect(wrapper.hasClass('van-cell-swipe')).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('CellSwipe-0', () => {
|
||||
let wrapper;
|
||||
afterEach(() => {
|
||||
wrapper && wrapper.destroy();
|
||||
});
|
||||
|
||||
it('create a CellSwipe 0', () => {
|
||||
wrapper = mount(CellSwipe, {
|
||||
propsData: {
|
||||
leftWidth: 0,
|
||||
rightWidth: 2
|
||||
}
|
||||
});
|
||||
wrapper.vm.startDrag({
|
||||
pageX: 0,
|
||||
pageY: 0
|
||||
});
|
||||
wrapper.vm.onDrag({
|
||||
preventDefault() {},
|
||||
pageY: 0,
|
||||
pageX: -2
|
||||
});
|
||||
wrapper.vm.opened = true;
|
||||
wrapper.vm.onDrag({
|
||||
preventDefault() {},
|
||||
pageY: 0,
|
||||
pageX: -2
|
||||
});
|
||||
wrapper.vm.opened = false;
|
||||
wrapper.vm.onDrag({
|
||||
preventDefault() {},
|
||||
pageY: 0,
|
||||
pageX: 40
|
||||
});
|
||||
wrapper.vm.swipeLeaveTransition(0);
|
||||
wrapper.vm.endDrag();
|
||||
expect(wrapper.hasClass('van-cell-swipe')).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('CellSwipe-0', () => {
|
||||
let wrapper;
|
||||
afterEach(() => {
|
||||
wrapper && wrapper.destroy();
|
||||
});
|
||||
|
||||
it('create a CellSwipe 0', () => {
|
||||
wrapper = mount(CellSwipe, {
|
||||
propsData: {
|
||||
leftWidth: 0,
|
||||
rightWidth: 2
|
||||
}
|
||||
});
|
||||
wrapper.vm.startDrag({
|
||||
pageX: 0,
|
||||
pageY: 0
|
||||
});
|
||||
wrapper.vm.onDrag({
|
||||
preventDefault() {},
|
||||
pageY: 1000,
|
||||
pageX: 40
|
||||
});
|
||||
wrapper.vm.swipeMove();
|
||||
wrapper.vm.swiping = false;
|
||||
wrapper.vm.endDrag();
|
||||
wrapper.vm.swiping = true;
|
||||
wrapper.vm.endDrag();
|
||||
expect(wrapper.hasClass('van-cell-swipe')).to.be.true;
|
||||
});
|
||||
|
||||
it('drag vertical', () => {
|
||||
wrapper = mount(CellSwipe, defaultProps);
|
||||
|
||||
triggerTouch(wrapper, 'touchstart', 0, 0);
|
||||
triggerTouch(wrapper, 'touchmove', 0, 100);
|
||||
triggerTouch(wrapper, 'touchend', 0, 100);
|
||||
expect(wrapper.vm.offset).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
@ -31,7 +31,9 @@ export function DOMChecker(wrapper, rules) {
|
||||
if (style) {
|
||||
Object.keys(style).forEach(key => {
|
||||
Object.keys(style[key]).forEach(prop => {
|
||||
expect(wrapper.find(key)[0].hasStyle(prop, style[key][prop])).to.equal(true);
|
||||
expect(wrapper.find(key)[0].hasStyle(prop, style[key][prop])).to.equal(
|
||||
true
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -39,8 +41,35 @@ export function DOMChecker(wrapper, rules) {
|
||||
if (noStyle) {
|
||||
Object.keys(noStyle).forEach(key => {
|
||||
Object.keys(noStyle[key]).forEach(prop => {
|
||||
expect(wrapper.find(key)[0].hasStyle(prop, noStyle[key][prop])).to.equal(false);
|
||||
expect(
|
||||
wrapper.find(key)[0].hasStyle(prop, noStyle[key][prop])
|
||||
).to.equal(false);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 触发一个 touch 事件
|
||||
export function triggerTouch(wrapper, eventName, x, y) {
|
||||
const el = wrapper.element ? wrapper.element : wrapper;
|
||||
const touch = {
|
||||
identifier: Date.now(),
|
||||
target: el,
|
||||
pageX: x,
|
||||
pageY: y,
|
||||
clientX: x,
|
||||
clientY: y,
|
||||
radiusX: 2.5,
|
||||
radiusY: 2.5,
|
||||
rotationAngle: 10,
|
||||
force: 0.5,
|
||||
};
|
||||
|
||||
const event = document.createEvent('CustomEvent');
|
||||
event.initCustomEvent(eventName, true, true, {});
|
||||
event.touches = [touch];
|
||||
event.targetTouches = [touch];
|
||||
event.changedTouches = [touch];
|
||||
|
||||
el.dispatchEvent(event);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user