提交 f4f42962 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat: 补充 globalData 示例及测试

上级 52e52d95
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
let firstBackTime = 0 let firstBackTime = 0
export default { export default {
globalData: {
str: 'global data str',
num: 123,
bool: true,
},
onLaunch: function () { onLaunch: function () {
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1000) setLifeCycleNum(state.lifeCycleNum + 1000)
......
const PAGE_PATH = '/pages/API/get-app/get-app' const PAGE_PATH = '/pages/API/get-app/get-app'
describe('getApp', () => { describe('getApp', () => {
it('getApp', async () => { let page = null
const page = await program.navigateTo(PAGE_PATH) beforeAll(async () => {
await page.waitFor(1000) page = await program.navigateTo(PAGE_PATH)
const oldData = await page.data() await page.waitFor('view')
})
it('globalData', async () => {
await page.callMethod('getGlobalData')
const data = await page.data()
expect(data.globalDataStr).toBe('global data str')
expect(data.globalDataNum).toBe(123)
expect(data.globalDataBool).toBe(true)
})
it('method', async () => {
const oldLifeCycleNum = await page.data('lifeCycleNum')
await page.callMethod('_increasetLifeCycleNum') await page.callMethod('_increasetLifeCycleNum')
const newData = await page.data() const newLifeCycleNum = await page.data('lifeCycleNum')
expect(newData.lifeCycleNum - oldData.lifeCycleNum).toBe(100) expect(newLifeCycleNum - oldLifeCycleNum).toBe(100)
await page.callMethod('setLifeCycleNum', oldData.lifeCycleNum) await page.callMethod('setLifeCycleNum', oldLifeCycleNum)
}) })
}) })
...@@ -2,7 +2,15 @@ ...@@ -2,7 +2,15 @@
<view> <view>
<page-head title="getApp"></page-head> <page-head title="getApp"></page-head>
<view class="uni-padding-wrap"> <view class="uni-padding-wrap">
<text>点击按钮调用 App.uvue methods</text> <button @click="getGlobalData">
get globalData
</button>
<template v-if="globalDataStr">
<text class="uni-common-mt">globalData str: {{ globalDataStr }}</text>
<text class="uni-common-mt">globalData num: {{ globalDataNum }}</text>
<text class="uni-common-mt">globalData boolean: {{ globalDataBool }}</text>
</template>
<text class="uni-common-mt">点击按钮调用 App.uvue methods</text>
<text class="margin-top:6px;">increasetLifeCycleNum 方法</text> <text class="margin-top:6px;">increasetLifeCycleNum 方法</text>
<button class="uni-common-mt" @click="_increasetLifeCycleNum"> <button class="uni-common-mt" @click="_increasetLifeCycleNum">
increase lifeCycleNum increase lifeCycleNum
...@@ -13,11 +21,14 @@ ...@@ -13,11 +21,14 @@
</template> </template>
<script lang="uts"> <script lang="uts">
import { State, state, setLifeCycleNum } from '@/store/index.uts' import { state, setLifeCycleNum } from '@/store/index.uts'
export default { export default {
data() { data() {
return { return {
globalDataStr: '',
globalDataNum: 0,
globalDataBool: false,
lifeCycleNum: 0, lifeCycleNum: 0,
} }
}, },
...@@ -25,15 +36,21 @@ export default { ...@@ -25,15 +36,21 @@ export default {
this.lifeCycleNum = state.lifeCycleNum this.lifeCycleNum = state.lifeCycleNum
}, },
methods: { methods: {
getGlobalData() {
const app = getApp()
this.globalDataStr = app.globalData.str
this.globalDataNum = app.globalData.num
this.globalDataBool = app.globalData.bool
},
_increasetLifeCycleNum: function () { _increasetLifeCycleNum: function () {
const app = getApp() const app = getApp()
app.increasetLifeCycleNum() app.increasetLifeCycleNum()
this.lifeCycleNum = state.lifeCycleNum this.lifeCycleNum = state.lifeCycleNum
}, },
// 自动化测试 // 自动化测试
setLifeCycleNum(num: number) { setLifeCycleNum(num : number) {
setLifeCycleNum(num) setLifeCycleNum(num)
}
}, },
}, }
}
</script> </script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册