Skip to content

Commit fea505a

Browse files
committed
test(weex): refactor the test cases to fit the new weex-js-runtime
1 parent 63fba06 commit fea505a

File tree

10 files changed

+499
-1092
lines changed

10 files changed

+499
-1092
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@
121121
"typescript": "^2.3.4",
122122
"uglify-js": "^3.0.15",
123123
"webpack": "^2.6.1",
124-
"weex-js-runtime": "^0.20.5",
125-
"weex-vdom-tester": "^0.2.0"
124+
"weex-js-runtime": "^0.22.0"
126125
},
127126
"config": {
128127
"commitizen": {

src/platforms/weex/entry-framework.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export function destroyInstance (instanceId) {
9393
if (instance && instance.app instanceof instance.Vue) {
9494
instance.document.destroy()
9595
instance.app.$destroy()
96+
delete instance.document
97+
delete instance.app
9698
}
9799
delete instances[instanceId]
98100
}

test/weex/helpers/index.js

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
// suppress logs from vdom-tester
2-
const domModule = require('weex-vdom-tester/lib/modules/dom')
3-
domModule.updateFinish = domModule.createFinish = domModule.refreshFinish = () => {}
4-
51
import * as Vue from '../../../packages/weex-vue-framework'
62
import { compile } from '../../../packages/weex-template-compiler'
7-
import { Runtime, Instance } from 'weex-vdom-tester'
8-
import { init, config } from 'weex-js-runtime'
3+
import WeexRuntime from 'weex-js-runtime'
94

10-
init()
5+
console.debug = () => {}
116

127
// http://stackoverflow.com/a/35478115
138
const matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g
@@ -27,32 +22,66 @@ function parseStatic (fns) {
2722
return '[' + fns.map(fn => `function () { ${fn} }`).join(',') + ']'
2823
}
2924

30-
export function prepareRuntime () {
31-
let sendTasksHandler = function () {}
32-
config.sendTasks = config.Document.handler = function () {
33-
sendTasksHandler.apply(null, arguments)
34-
}
35-
Vue.init(config)
36-
const runtime = new Runtime(Vue)
37-
sendTasksHandler = function () {
38-
runtime.target.callNative.apply(runtime.target, arguments)
25+
function isObject (object) {
26+
return object !== null && typeof object === 'object'
27+
}
28+
29+
function isEmptyObject (object) {
30+
return isObject(object) && Object.keys(object).length < 1
31+
}
32+
33+
function omitUseless (object) {
34+
if (isObject(object)) {
35+
delete object.ref
36+
for (const key in object) {
37+
if (isEmptyObject(object[key]) || object[key] === undefined) {
38+
delete object[key]
39+
}
40+
omitUseless(object[key])
41+
}
3942
}
40-
return runtime
43+
return object
4144
}
4245

43-
export function resetRuntime () {
44-
delete config.Document.handler
45-
Vue.reset()
46+
export function getRoot (instance) {
47+
return omitUseless(instance.document.body.toJSON())
4648
}
4749

48-
export function createInstance (runtime, code) {
49-
const instance = new Instance(runtime)
50-
if (code) {
51-
instance.$create(code)
50+
export function fireEvent (instance, ref, type, event = {}) {
51+
const el = instance.document.getRef(ref)
52+
if (el) {
53+
instance.document.fireEvent(el, type, event = {})
5254
}
55+
}
56+
57+
export function createInstance (id, code, ...args) {
58+
WeexRuntime.config.frameworks = { Vue }
59+
const context = WeexRuntime.init(WeexRuntime.config)
60+
context.registerModules({
61+
timer: ['setTimeout', 'setInterval']
62+
})
63+
const instance = context.createInstance(id, `// { "framework": "Vue" }\n${code}`, ...args)
64+
instance.$refresh = (data) => context.refreshInstance(id, data)
65+
instance.$destroy = () => context.destroyInstance(id)
5366
return instance
5467
}
5568

69+
export function compileAndExecute (template, additional = '') {
70+
return new Promise(resolve => {
71+
const id = String(Date.now() * Math.random())
72+
const { render, staticRenderFns } = compile(template)
73+
const instance = createInstance(id, `
74+
new Vue({
75+
el: '#whatever',
76+
render: function () { ${render} },
77+
staticRenderFns: ${parseStatic(staticRenderFns)},
78+
${additional}
79+
})
80+
`)
81+
setTimeout(() => resolve(instance), 10)
82+
})
83+
}
84+
5685
export function syncPromise (arr) {
5786
let p = Promise.resolve()
5887
arr.forEach(item => {
@@ -65,7 +94,7 @@ export function checkRefresh (instance, data, checker) {
6594
return () => new Promise(res => {
6695
instance.$refresh(data)
6796
setTimeout(() => {
68-
checker(instance.getRealRoot())
97+
checker(getRoot(instance))
6998
res()
7099
})
71100
})

test/weex/runtime/attrs.spec.js

Lines changed: 52 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,82 @@
1-
import {
2-
compileAndStringify,
3-
prepareRuntime,
4-
resetRuntime,
5-
createInstance
6-
} from '../helpers/index'
1+
import { getRoot, fireEvent, compileAndExecute } from '../helpers/index'
72

83
describe('generate attribute', () => {
9-
let runtime
10-
11-
beforeAll(() => {
12-
runtime = prepareRuntime()
13-
})
14-
15-
afterAll(() => {
16-
resetRuntime()
17-
runtime = null
18-
})
19-
20-
it('should be generated', () => {
21-
const { render, staticRenderFns } = compileAndStringify(`
4+
it('should be generated', (done) => {
5+
compileAndExecute(`
226
<div>
237
<text value="Hello World" style="font-size: 100"></text>
248
</div>
25-
`)
26-
const instance = createInstance(runtime, `
27-
new Vue({
28-
render: ${render},
29-
staticRenderFns: ${staticRenderFns},
30-
el: 'body'
9+
`).then(instance => {
10+
expect(getRoot(instance)).toEqual({
11+
type: 'div',
12+
children: [{
13+
type: 'text',
14+
style: { fontSize: '100' },
15+
attr: { value: 'Hello World' }
16+
}]
3117
})
32-
`)
33-
expect(instance.getRealRoot()).toEqual({
34-
type: 'div',
35-
children: [
36-
{ type: 'text', style: { fontSize: '100' }, attr: { value: 'Hello World' }}
37-
]
38-
})
18+
done()
19+
}).catch(e => done.fail(e))
3920
})
4021

4122
it('should be updated', (done) => {
42-
const { render, staticRenderFns } = compileAndStringify(`
23+
compileAndExecute(`
4324
<div @click="foo">
4425
<text :value="x"></text>
4526
</div>
46-
`)
47-
const instance = createInstance(runtime, `
48-
new Vue({
49-
data: {
50-
x: 'Hello World'
51-
},
52-
methods: {
53-
foo: function () {
54-
this.x = 'Hello Vue'
55-
}
56-
},
57-
render: ${render},
58-
staticRenderFns: ${staticRenderFns},
59-
el: "body"
27+
`, `
28+
data: { x: 'Hello World' },
29+
methods: {
30+
foo: function () {
31+
this.x = 'Hello Vue'
32+
}
33+
}
34+
`).then(instance => {
35+
expect(getRoot(instance)).toEqual({
36+
type: 'div',
37+
event: ['click'],
38+
children: [
39+
{ type: 'text', attr: { value: 'Hello World' }}
40+
]
6041
})
61-
`)
62-
expect(instance.getRealRoot()).toEqual({
63-
type: 'div',
64-
event: ['click'],
65-
children: [
66-
{ type: 'text', attr: { value: 'Hello World' }}
67-
]
68-
})
69-
70-
instance.$fireEvent(instance.doc.body.ref, 'click', {})
71-
setTimeout(() => {
72-
expect(instance.getRealRoot()).toEqual({
42+
fireEvent(instance, '_root', 'click')
43+
return instance
44+
}).then(instance => {
45+
expect(getRoot(instance)).toEqual({
7346
type: 'div',
7447
event: ['click'],
7548
children: [
7649
{ type: 'text', attr: { value: 'Hello Vue' }}
7750
]
7851
})
7952
done()
80-
})
53+
}).catch(e => done.fail(e))
8154
})
8255

8356
it('should be cleared', (done) => {
84-
const { render, staticRenderFns } = compileAndStringify(`
57+
compileAndExecute(`
8558
<div @click="foo">
8659
<text :value="x"></text>
8760
</div>
88-
`)
89-
const instance = createInstance(runtime, `
90-
new Vue({
91-
data: {
92-
x: 'Hello World'
93-
},
94-
methods: {
95-
foo: function () {
96-
this.x = ''
97-
}
98-
},
99-
render: ${render},
100-
staticRenderFns: ${staticRenderFns},
101-
el: "body"
61+
`, `
62+
data: { x: 'Hello World' },
63+
methods: {
64+
foo: function () {
65+
this.x = ''
66+
}
67+
}
68+
`).then(instance => {
69+
expect(getRoot(instance)).toEqual({
70+
type: 'div',
71+
event: ['click'],
72+
children: [
73+
{ type: 'text', attr: { value: 'Hello World' }}
74+
]
10275
})
103-
`)
104-
expect(instance.getRealRoot()).toEqual({
105-
type: 'div',
106-
event: ['click'],
107-
children: [
108-
{ type: 'text', attr: { value: 'Hello World' }}
109-
]
110-
})
111-
112-
instance.$fireEvent(instance.doc.body.ref, 'click', {})
113-
setTimeout(() => {
114-
expect(instance.getRealRoot()).toEqual({
76+
fireEvent(instance, '_root', 'click')
77+
return instance
78+
}).then(instance => {
79+
expect(getRoot(instance)).toEqual({
11580
type: 'div',
11681
event: ['click'],
11782
children: [

0 commit comments

Comments
 (0)