Skip to content

Commit d33704e

Browse files
committed
fix: 补充测试用例
1 parent 3f2d24e commit d33704e

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

test/mp/helpers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ function strToRegExp (str) {
55

66
// runtime
77
// fix mp env
8-
const { App, Page, getApp } = require('./mp.runtime')
8+
const { App, Page, getApp, Component } = require('./mp.runtime')
99
global.App = App
1010
global.Page = Page
1111
global.getApp = getApp
12+
global.Component = Component
1213

1314
const Vue = require('../../../packages/mpvue')
1415

test/mp/helpers/mp.runtime.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ class MPApp extends MPPage {
7171
}
7272
}
7373

74+
class MPComponent extends MPPage {
75+
// component 与page生命周期不同
76+
_initLifecycle () {
77+
this._callHook('created')
78+
this._callHook('attached')
79+
this._callHook('ready')
80+
}
81+
82+
_leaveAndBack () {
83+
// 微信原生组件会进入后台,不会触发任何生命周期
84+
}
85+
}
86+
7487
function Page (config) {
7588
return new MPPage(config)
7689
}
@@ -84,8 +97,13 @@ function getApp () {
8497
return appVM
8598
}
8699

100+
function Component (config) {
101+
return new MPComponent(config)
102+
}
103+
87104
module.exports = {
88105
Page,
89106
App,
90-
getApp
107+
getApp,
108+
Component
91109
}

test/mp/runtime/lifecycle.spec.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ describe('init mpvue with lifecycle', function () {
7171
},
7272
onPageScroll () {
7373
onLifecycle.push(`pageScroll${key}`)
74+
},
75+
// custom component lifecycle
76+
attached () {
77+
onLifecycle.push(`attached${key}`)
78+
},
79+
ready () {
80+
onLifecycle.push(`ready${key}`)
81+
},
82+
moved () {
83+
onLifecycle.push(`moved${key}`)
84+
},
85+
detached () {
86+
onLifecycle.push(`detached${key}`)
7487
}
7588
}
7689
}
@@ -237,4 +250,101 @@ describe('init mpvue with lifecycle', function () {
237250
expect(app.$mp.status).toEqual('unload')
238251
expect(onLifecycle).toEqual(['beforeCreate', 'created', 'onLoad', 'onShow', 'onReady', 'beforeMount', 'mounted', 'pullDownRefresh', 'reachBottom', 'shareAppMessage', 'pageScroll', 'unload'])
239252
})
253+
254+
it('Component with render', function () {
255+
const options = Object.assign(getComponentOptions(), {
256+
mpType: 'component',
257+
render () {
258+
var _vm = this
259+
var _h = _vm.$createElement
260+
var _c = _vm._self._c || _h
261+
return _c('div', {
262+
staticClass: 'container'
263+
}, [], 1)
264+
}
265+
})
266+
267+
const app = createInstance(options)
268+
app.$mount()
269+
expect(onLifecycle).toEqual(['beforeCreate', 'created', 'attached', 'ready', 'beforeMount', 'mounted'])
270+
expect(!!app.$mp.page).toEqual(true)
271+
expect(app.$mp.mpType).toEqual('component')
272+
expect(app.$mp.status).toEqual('ready')
273+
})
274+
275+
it('Component with component', function () {
276+
const warpOptions = Object.assign(getComponentOptions('-warp'), {
277+
render () {
278+
var _vm = this
279+
var _h = _vm.$createElement
280+
var _c = _vm._self._c || _h
281+
return _c('div', {
282+
staticClass: 'container'
283+
}, [_vm._v('warp component')], 1)
284+
}
285+
})
286+
const options = Object.assign(getComponentOptions(), {
287+
mpType: 'component',
288+
components: {
289+
warp: warpOptions
290+
},
291+
render () {
292+
var _vm = this
293+
var _h = _vm.$createElement
294+
var _c = _vm._self._c || _h
295+
return _c('div', {
296+
staticClass: 'container'
297+
}, [_c('warp')], 1)
298+
}
299+
})
300+
const app = createInstance(options)
301+
app.$mount()
302+
303+
expect(onLifecycle).toEqual([
304+
'beforeCreate',
305+
'created',
306+
'attached',
307+
'ready',
308+
'beforeMount',
309+
'beforeCreate-warp',
310+
'created-warp',
311+
'onLoad-warp',
312+
'onReady-warp',
313+
'beforeMount-warp',
314+
'mounted-warp',
315+
'mounted'
316+
])
317+
expect(!!app.$mp.page).toEqual(true)
318+
expect(app.$mp.mpType).toEqual('component')
319+
expect(app.$mp.status).toEqual('ready')
320+
})
321+
322+
it('Component with customEvent', function () {
323+
const options = Object.assign(getComponentOptions(), {
324+
mpType: 'component',
325+
render () {
326+
var _vm = this
327+
var _h = _vm.$createElement
328+
var _c = _vm._self._c || _h
329+
return _c('div', {
330+
staticClass: 'container'
331+
}, [], 1)
332+
}
333+
})
334+
const app = createInstance(options)
335+
app.$mount()
336+
expect(onLifecycle).toEqual(['beforeCreate', 'created', 'attached', 'ready', 'beforeMount', 'mounted'])
337+
expect(!!app.$mp.page).toEqual(true)
338+
expect(app.$mp.mpType).toEqual('component')
339+
expect(app.$mp.status).toEqual('ready')
340+
341+
// moved
342+
app.$mp.page._callHook('moved')
343+
expect(onLifecycle).toEqual(['beforeCreate', 'created', 'attached', 'ready', 'beforeMount', 'mounted', 'moved'])
344+
345+
// detached
346+
app.$mp.page._callHook('detached')
347+
expect(app.$mp.status).toEqual('detached')
348+
expect(onLifecycle).toEqual(['beforeCreate', 'created', 'attached', 'ready', 'beforeMount', 'mounted', 'moved', 'detached'])
349+
})
240350
})

0 commit comments

Comments
 (0)