2016-10-11 11:00:37 +00:00
|
|
|
|
import Vue from 'vue/dist/vue';
|
|
|
|
|
import Element from 'main/index.js';
|
|
|
|
|
|
|
|
|
|
Vue.use(Element);
|
|
|
|
|
|
|
|
|
|
let id = 0;
|
|
|
|
|
|
|
|
|
|
const createElm = function() {
|
|
|
|
|
const elm = document.createElement('div');
|
|
|
|
|
|
|
|
|
|
elm.id = 'app' + ++id;
|
|
|
|
|
document.body.appendChild(elm);
|
|
|
|
|
|
|
|
|
|
return elm;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个 Vue 的实例对象
|
2016-10-17 08:32:20 +00:00
|
|
|
|
* @param {Object|String} Compo 组件配置,可直接传 template
|
2016-10-11 11:00:37 +00:00
|
|
|
|
* @param {Boolean=false} mounted 是否添加到 DOM 上
|
|
|
|
|
* @return {Object} vm
|
|
|
|
|
*/
|
|
|
|
|
exports.createVue = function(Compo, mounted = false) {
|
|
|
|
|
const elm = createElm();
|
2016-10-17 08:32:20 +00:00
|
|
|
|
|
|
|
|
|
if (Object.prototype.toString.call(Compo) === '[object String]') {
|
|
|
|
|
Compo = { template: Compo };
|
|
|
|
|
}
|
2016-10-11 11:00:37 +00:00
|
|
|
|
return new Vue(Compo).$mount(mounted === false ? null : elm);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个测试组件实例
|
|
|
|
|
* @link http://vuejs.org/guide/unit-testing.html#Writing-Testable-Components
|
|
|
|
|
* @param {Object} Compo - 组件对象
|
|
|
|
|
* @param {Object} propsData - props 数据
|
|
|
|
|
* @param {Boolean=false} mounted - 是否添加到 DOM 上
|
|
|
|
|
* @return {Object} vm
|
|
|
|
|
*/
|
|
|
|
|
exports.createTest = function(Compo, propsData = {}, mounted = false) {
|
2016-10-17 08:32:20 +00:00
|
|
|
|
if (propsData === true || propsData === false) {
|
|
|
|
|
mounted = propsData;
|
|
|
|
|
propsData = {};
|
|
|
|
|
}
|
2016-10-11 11:00:37 +00:00
|
|
|
|
const elm = createElm();
|
|
|
|
|
const Ctor = Vue.extend(Compo);
|
|
|
|
|
return new Ctor({ propsData }).$mount(mounted === false ? null : elm);
|
|
|
|
|
};
|
2016-10-20 06:35:12 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 触发一个事件
|
|
|
|
|
* mouseenter, mouseleave, mouseover 等
|
|
|
|
|
* @param {Element} elm
|
|
|
|
|
* @param {EventName} name
|
|
|
|
|
* @param {options} opts
|
|
|
|
|
*/
|
|
|
|
|
exports.triggerEvent = function(elm, name, opts) {
|
|
|
|
|
const evt = document.createEvent('MouseEvents');
|
|
|
|
|
|
|
|
|
|
evt.initEvent(name, ...opts);
|
|
|
|
|
elm.dispatchEvent
|
|
|
|
|
? elm.dispatchEvent(evt)
|
|
|
|
|
: elm.fireEvent('on' + name, evt);
|
|
|
|
|
};
|