From 8c3eb819b0f5277aa35fa187f396b6c24e99820e Mon Sep 17 00:00:00 2001 From: pangxie1991 Date: Fri, 3 Mar 2017 11:07:16 +0800 Subject: [PATCH] vue creater --- test/unit/creater.js | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/unit/creater.js diff --git a/test/unit/creater.js b/test/unit/creater.js new file mode 100644 index 000000000..68f5779aa --- /dev/null +++ b/test/unit/creater.js @@ -0,0 +1,55 @@ +import Vue from 'vue'; + +let id = 0; + +class Creater { + constructor(Compo, propsData) { + let Ctor = Vue.extend(Compo); + this.vueInstance = new Ctor({ propsData }); + } + + mount() { + const elem = exports.createElm(); + this.vueInstance.$mount(elem); + } + + triggerEvent(name, ...opts) { + let eventName; + let elem = this.vueInstance.$el; + + if (/^mouse|click/.test(name)) { + eventName = 'MouseEvents'; + } else if (/^key/.test(name)) { + eventName = 'KeyboardEvent'; + } else { + eventName = 'HTMLEvents'; + } + const evt = document.createEvent(eventName); + + evt.initEvent(name, ...opts); + elem.dispatchEvent + ? elem.dispatchEvent(evt) + : elem.fireEvent('on' + name, evt); + + return elem; + } + + destroy() { + this.vueInstance.$el && + this.vueInstance.$el.parentNode && + this.vueInstance.$el.parentNode.removeChild(this.$el); + } +} + +exports.createElm = function() { + const elm = document.createElement('div'); + + elm.id = 'app' + ++id; + document.body.appendChild(elm); + + return elm; +}; + +exports.createVue = function(Compo, propsData = {}) { + return new Creater(Compo, propsData); +};