forked from Hanks10100/weex-native-directive
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.util.js
82 lines (75 loc) · 2.03 KB
/
test.util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const fs = require('fs')
const path = require('path')
const { expect } = require('chai')
const WeexRuntime = require('weex-js-runtime')
const Vue = require('weex-vue-framework')
const { compile } = require('weex-template-compiler')
function toFunction (content) {
return `function () { ${content} }`
}
function readFile (name) {
return fs.readFileSync(path.resolve('./examples/', name), 'utf8')
}
function omitUseless (object) {
if (object !== null && typeof object === 'object') {
delete object.ref
for (const key in object) {
if ((Object.keys(object[key] || {}).length < 1) && key.charAt(0) !== '@') {
delete object[key]
}
omitUseless(object[key])
}
}
return object
}
function execute (code) {
WeexRuntime.config.frameworks = { Vue }
const context = WeexRuntime.init(WeexRuntime.config)
context.registerModules({
timer: ['setTimeout', 'setInterval']
})
return new Promise(resolve => {
const id = String(Date.now() * Math.random())
const instance = context.createInstance(id, code)
setTimeout(() => {
resolve(omitUseless(instance.document.body.toJSON()))
context.destroyInstance(id)
}, 10)
})
}
function compileAndExecute (template, additional = '') {
const { render, staticRenderFns } = compile(
`<recycle-list>
<cell-slot>${template}</cell-slot>
</recycle-list>`
)
return execute(`
// { "framework": "Vue" }
Vue.config.silent = true
new Vue({
el: '#whatever',
render: ${toFunction(render)},
staticRenderFns: [${staticRenderFns.map(toFunction).join(',')}],
${additional}
})
`).then($root => {
return $root.children[0].children[0]
})
}
function createTestSuit (name) {
return done => {
const source = readFile(`${name}.vue`)
const target = readFile(`${name}.vdom.js`)
compileAndExecute(source).then($root => {
expect($root).to.deep.equal(eval(`(${target})`))
done()
}).catch(done)
}
}
module.exports = {
expect,
execute,
readFile,
createTestSuit,
compileAndExecute
}