mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
actionsheet unit test
This commit is contained in:
parent
b207efe729
commit
7c75f48eaa
@ -56,6 +56,7 @@ export default {
|
||||
title: String,
|
||||
cancelText: String,
|
||||
overlay: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
closeOnClickOverlay: {
|
||||
|
@ -10,7 +10,7 @@ let dialogQueue = [];
|
||||
|
||||
const defaultCallback = action => {
|
||||
if (currentDialog) {
|
||||
let callback = currentDialog.callback;
|
||||
const callback = currentDialog.callback;
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
callback(action);
|
||||
@ -40,9 +40,9 @@ const showNextDialog = () => {
|
||||
if (!instance.value && dialogQueue.length > 0) {
|
||||
currentDialog = dialogQueue.shift();
|
||||
|
||||
let options = currentDialog.options;
|
||||
const options = currentDialog.options;
|
||||
|
||||
for (let prop in options) {
|
||||
for (const prop in options) {
|
||||
if (options.hasOwnProperty(prop)) {
|
||||
instance[prop] = options[prop];
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
import Vue from 'vue';
|
||||
import ImagePreview from './image-preview.vue';
|
||||
import merge from 'src/utils/merge';
|
||||
|
||||
let instance;
|
||||
|
||||
|
@ -24,7 +24,6 @@ Input.prototype = Object.create(new EventEmitter());
|
||||
|
||||
extend(Input.prototype, {
|
||||
bind: function(host) {
|
||||
|
||||
bindEvents(host, 'touchstart mousedown', this.onTouchStart);
|
||||
if (this.options.listenMoving) {
|
||||
bindEvents(window, 'touchmove mousemove', this.onTouchMove);
|
||||
@ -74,11 +73,11 @@ extend(Input.prototype, {
|
||||
this.orgDirection = Math.abs(distX) > Math.abs(distY);
|
||||
}
|
||||
|
||||
this.emit('move', {x: distX, y: distY}, isEnd, e, {orgDirection: this.orgDirection});
|
||||
this.emit('move', { x: distX, y: distY }, isEnd, e, { orgDirection: this.orgDirection });
|
||||
},
|
||||
|
||||
pointerEventToXY: function(e) {
|
||||
var out = {x: 0, y: 0};
|
||||
var out = { x: 0, y: 0 };
|
||||
var type = e.type;
|
||||
if (e.originalEvent) {
|
||||
e = e.originalEvent;
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { EventEmitter, extend } from './utils'
|
||||
import { EventEmitter, extend } from './utils';
|
||||
|
||||
const setElementsStyles = (elems, styles) => {
|
||||
Array.prototype.forEach.call(elems, item => {
|
||||
extend(item.style, styles)
|
||||
})
|
||||
}
|
||||
extend(item.style, styles);
|
||||
});
|
||||
};
|
||||
|
||||
function Scroll(wrapElem, options) {
|
||||
EventEmitter.apply(this, arguments);
|
||||
@ -33,14 +33,14 @@ extend(Scroll.prototype, {
|
||||
},
|
||||
|
||||
update: function() {
|
||||
const oldPages = this.pages
|
||||
const oldPages = this.pages;
|
||||
this.pages = this.wrapElem.querySelectorAll('.zan-swipe-item');
|
||||
if (oldPages && oldPages.length === this.pages.length) {
|
||||
const isSame = Array.prototype.every.call(this.pages, (elem, index) => {
|
||||
return this.pages[index] === oldPages[index]
|
||||
})
|
||||
return this.pages[index] === oldPages[index];
|
||||
});
|
||||
if (isSame) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
}
|
||||
var defaultStyle = {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { requestAnimationFrame, cancelAnimationFrame, EventEmitter, extend } from './utils'
|
||||
import { requestAnimationFrame, cancelAnimationFrame, EventEmitter, extend } from './utils';
|
||||
|
||||
function SpringDummy(scroll, input, options) {
|
||||
var wrapElem = scroll.wrapElem;
|
||||
@ -31,7 +31,6 @@ function SpringDummy(scroll, input, options) {
|
||||
}).on('bounceStart', function() {
|
||||
self.input.deaf();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
SpringDummy.prototype = Object.create(new EventEmitter());
|
||||
@ -85,7 +84,6 @@ extend(SpringDummy.prototype, {
|
||||
addition = w * (tempOffsetPage - offsetPage + this.scroll.pages.length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.initTween(addition - dist, 150, 'bounce');
|
||||
@ -105,12 +103,12 @@ extend(SpringDummy.prototype, {
|
||||
function round() {
|
||||
elapse = new Date() - startTime;
|
||||
if (elapse > duration) {
|
||||
self.emit(eventName, {x: dist}, true);
|
||||
self.emit(eventName, { x: dist }, true);
|
||||
self.emit(eventName + 'End');
|
||||
return;
|
||||
}
|
||||
|
||||
self.emit(eventName, {x: dist / duration * elapse}, false);
|
||||
self.emit(eventName, { x: dist / duration * elapse }, false);
|
||||
self.tweenRid = requestAnimationFrame(round);
|
||||
}
|
||||
round();
|
||||
|
@ -18,11 +18,11 @@ EventEmitter.prototype = {
|
||||
if (arr) {
|
||||
arr.forEach(function(cb) {
|
||||
cb.apply(self, argus);
|
||||
})
|
||||
});
|
||||
}
|
||||
},
|
||||
removeListener: function(name, fn) {
|
||||
if (this.__events[name] == undefined) {
|
||||
if (!this.__events[name]) {
|
||||
return;
|
||||
}
|
||||
let index;
|
||||
@ -50,14 +50,14 @@ const cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnim
|
||||
};
|
||||
|
||||
const bindEvents = (elem, eventNames, fn) => {
|
||||
eventNames = eventNames.split(/\s+/)
|
||||
eventNames.forEach(eventName => elem.addEventListener(eventName, fn))
|
||||
}
|
||||
eventNames = eventNames.split(/\s+/);
|
||||
eventNames.forEach(eventName => elem.addEventListener(eventName, fn));
|
||||
};
|
||||
|
||||
const removeEvents = (elem, eventNames, fn) => {
|
||||
eventNames = eventNames.split(/\s+/)
|
||||
eventNames.forEach(eventName => elem.removeEventListener(eventName, fn))
|
||||
}
|
||||
eventNames = eventNames.split(/\s+/);
|
||||
eventNames.forEach(eventName => elem.removeEventListener(eventName, fn));
|
||||
};
|
||||
|
||||
export {
|
||||
extend,
|
||||
|
@ -13,7 +13,6 @@ const getInstance = () => {
|
||||
return instance;
|
||||
};
|
||||
|
||||
|
||||
const removeDom = event => {
|
||||
if (event.target.parentNode) {
|
||||
event.target.parentNode.removeChild(event.target);
|
||||
@ -23,7 +22,7 @@ const removeDom = event => {
|
||||
var Toast = (options = {}) => {
|
||||
const duration = options.duration || 3000;
|
||||
|
||||
let instance = getInstance();
|
||||
const instance = getInstance();
|
||||
instance.closed = false;
|
||||
clearTimeout(instance.timer);
|
||||
instance.type = options.type ? options.type : 'text';
|
||||
@ -71,6 +70,6 @@ Toast.fail = (options) => {
|
||||
|
||||
Toast.clear = () => {
|
||||
if (instance) instance.clear();
|
||||
}
|
||||
};
|
||||
|
||||
export default Toast;
|
||||
|
@ -2,7 +2,7 @@ import merge from 'src/utils/merge';
|
||||
|
||||
let context;
|
||||
if (window && window.popupContext) {
|
||||
context = window.popupContext
|
||||
context = window.popupContext;
|
||||
}
|
||||
|
||||
const DEFAULT_CONTEXT = {
|
||||
@ -11,7 +11,7 @@ const DEFAULT_CONTEXT = {
|
||||
hasModal: false,
|
||||
instances: {},
|
||||
modalStack: []
|
||||
}
|
||||
};
|
||||
|
||||
context = window.popupContext = merge({}, DEFAULT_CONTEXT, context);
|
||||
|
||||
|
@ -36,14 +36,14 @@ const PopupManager = {
|
||||
|
||||
register(id, instance) {
|
||||
if (id && instance) {
|
||||
let instances = PopupContext.getContext('instances');
|
||||
const instances = PopupContext.getContext('instances');
|
||||
instances[id] = instance;
|
||||
}
|
||||
},
|
||||
|
||||
deregister(id) {
|
||||
if (id) {
|
||||
let instances = PopupContext.getContext('instances');
|
||||
const instances = PopupContext.getContext('instances');
|
||||
instances[id] = null;
|
||||
delete instances[id];
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
import Vue from 'vue';
|
||||
import ActionSheet from 'packages/actionsheet';
|
||||
import { mount } from 'avoriaz';
|
||||
|
||||
@ -13,5 +14,103 @@ describe('ActionSheet', () => {
|
||||
});
|
||||
|
||||
expect(wrapper.hasClass('zan-actionsheet')).to.be.true;
|
||||
expect(wrapper.contains('.zan-actionsheet__list')).to.be.true;
|
||||
expect(wrapper.instance().actions.length).to.equal(0);
|
||||
expect(wrapper.instance().overlay).to.be.true;
|
||||
expect(wrapper.instance().closeOnClickOverlay).to.be.true;
|
||||
});
|
||||
|
||||
it('create displayed actionsheet', () => {
|
||||
wrapper = mount(ActionSheet, {
|
||||
propsData: {
|
||||
value: true
|
||||
}
|
||||
});
|
||||
|
||||
expect(wrapper.instance().currentValue).to.be.true;
|
||||
});
|
||||
|
||||
it('create title type actionsheet', () => {
|
||||
wrapper = mount(ActionSheet, {
|
||||
propsData: {
|
||||
title: 'test'
|
||||
}
|
||||
});
|
||||
|
||||
expect(wrapper.hasClass('zan-actionsheet--withtitle')).to.be.true;
|
||||
expect(wrapper.contains('.zan-actionsheet__header')).to.be.true;
|
||||
expect(wrapper.contains('.zan-actionsheet__content')).to.be.true;
|
||||
});
|
||||
|
||||
it('create actions actionsheet', () => {
|
||||
wrapper = mount(ActionSheet, {
|
||||
propsData: {
|
||||
actions: [
|
||||
{
|
||||
name: '有赞E卡',
|
||||
subname: '(剩余260.50元)'
|
||||
},
|
||||
{
|
||||
name: '信用卡支付',
|
||||
loading: true
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
const actionItems = wrapper.find('.zan-actionsheet__item');
|
||||
|
||||
expect(actionItems.length).to.equal(2);
|
||||
expect(actionItems[0].contains('.zan-actionsheet__name')).to.be.true;
|
||||
expect(actionItems[0].contains('.zan-actionsheet__subname')).to.be.true;
|
||||
expect(actionItems[1].contains('.zan-actionsheet__loading')).to.be.true;
|
||||
});
|
||||
|
||||
it('handle actionsheet item click with callback', () => {
|
||||
let called = false;
|
||||
wrapper = mount(ActionSheet, {
|
||||
propsData: {
|
||||
actions: [
|
||||
{
|
||||
name: '有赞E卡',
|
||||
callback: () => {
|
||||
called = true;
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
const actionItem = wrapper.find('.zan-actionsheet__item')[0];
|
||||
actionItem.simulate('click');
|
||||
expect(called).to.be.true;
|
||||
});
|
||||
|
||||
it('create actionsheet with cancel button', () => {
|
||||
wrapper = mount(ActionSheet, {
|
||||
propsData: {
|
||||
cancelText: 'cancel'
|
||||
}
|
||||
});
|
||||
|
||||
const cancelButton = wrapper.find('.zan-actionsheet__button')[0];
|
||||
expect(wrapper.contains('.zan-actionsheet__button')).to.be.true;
|
||||
expect(cancelButton.text()).to.equal('cancel');
|
||||
});
|
||||
|
||||
it('toggle actionsheet value', () => {
|
||||
wrapper = mount(ActionSheet, {
|
||||
propsData: {
|
||||
value: false
|
||||
}
|
||||
});
|
||||
|
||||
expect(wrapper.data().currentValue).to.be.false;
|
||||
wrapper.vm.value = true;
|
||||
wrapper.update();
|
||||
Vue.nextTick(() => {
|
||||
expect(wrapper.data().currentValue).to.be.true;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -110,7 +110,6 @@ describe('Switch', () => {
|
||||
});
|
||||
|
||||
it('toggle switch value from v-model', function(done) {
|
||||
let checked = false;
|
||||
wrapper = mount(Switch, {
|
||||
propsData: {
|
||||
value: false
|
||||
|
Loading…
x
Reference in New Issue
Block a user