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

feat: 补充 globalData 示例及测试

上级 52e52d95
...@@ -2,7 +2,12 @@ ...@@ -2,7 +2,12 @@
import { state, setLifeCycleNum } from '@/store/index.uts' import { state, setLifeCycleNum } from '@/store/index.uts'
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)
}) })
}) })
<template> <template>
<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">
<text class="margin-top:6px;">increasetLifeCycleNum 方法</text> get globalData
<button class="uni-common-mt" @click="_increasetLifeCycleNum">
increase lifeCycleNum
</button> </button>
<text class="uni-common-mt">lifeCycleNum: {{ lifeCycleNum }}</text> <template v-if="globalDataStr">
</view> <text class="uni-common-mt">globalData str: {{ globalDataStr }}</text>
</view> <text class="uni-common-mt">globalData num: {{ globalDataNum }}</text>
</template> <text class="uni-common-mt">globalData boolean: {{ globalDataBool }}</text>
</template>
<script lang="uts"> <text class="uni-common-mt">点击按钮调用 App.uvue methods</text>
import { State, state, setLifeCycleNum } from '@/store/index.uts' <text class="margin-top:6px;">increasetLifeCycleNum 方法</text>
<button class="uni-common-mt" @click="_increasetLifeCycleNum">
export default { increase lifeCycleNum
data() { </button>
return { <text class="uni-common-mt">lifeCycleNum: {{ lifeCycleNum }}</text>
lifeCycleNum: 0, </view>
} </view>
}, </template>
onReady() {
this.lifeCycleNum = state.lifeCycleNum <script lang="uts">
}, import { state, setLifeCycleNum } from '@/store/index.uts'
methods: {
_increasetLifeCycleNum: function () { export default {
const app = getApp() data() {
app.increasetLifeCycleNum() return {
this.lifeCycleNum = state.lifeCycleNum globalDataStr: '',
}, globalDataNum: 0,
// 自动化测试 globalDataBool: false,
setLifeCycleNum(num: number) { lifeCycleNum: 0,
setLifeCycleNum(num) }
}, },
}, onReady() {
} this.lifeCycleNum = state.lifeCycleNum
},
methods: {
getGlobalData() {
const app = getApp()
this.globalDataStr = app.globalData.str
this.globalDataNum = app.globalData.num
this.globalDataBool = app.globalData.bool
},
_increasetLifeCycleNum: function () {
const app = getApp()
app.increasetLifeCycleNum()
this.lifeCycleNum = state.lifeCycleNum
},
// 自动化测试
setLifeCycleNum(num : number) {
setLifeCycleNum(num)
}
},
}
</script> </script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册