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

feat: navigator & lifecycle

上级 4f347962
...@@ -121,13 +121,13 @@ ...@@ -121,13 +121,13 @@
"style": { "style": {
"navigationBarTitleText": "大图测试" "navigationBarTitleText": "大图测试"
} }
}, },
{ {
"path": "pages/component/video/video", "path": "pages/component/video/video",
"style": { "style": {
"navigationBarTitleText": "video" "navigationBarTitleText": "video"
} }
}, },
{ {
"path": "pages/component/web-view/web-view", "path": "pages/component/web-view/web-view",
"style": { "style": {
...@@ -154,9 +154,16 @@ ...@@ -154,9 +154,16 @@
} }
}, },
{ {
"path": "pages/API/navigator/new-page/new-page", "path": "pages/API/navigator/new-page/new-page-1",
"style": { "style": {
"navigationBarTitleText": "新页面" "navigationBarTitleText": "新页面-1"
}
},
{
"path": "pages/API/navigator/new-page/new-page-2",
"style": {
"navigationBarTitleText": "新页面-2",
"enablePullDownRefresh": true
} }
}, },
{ {
...@@ -592,24 +599,24 @@ ...@@ -592,24 +599,24 @@
"navigationBarTitleText": "详情示例" "navigationBarTitleText": "详情示例"
} }
}, },
{ {
"path": "pages/template/swiper-vertical-video/swiper-vertical-video", "path": "pages/template/swiper-vertical-video/swiper-vertical-video",
"style": { "style": {
"navigationBarTitleText": "" "navigationBarTitleText": ""
} }
}, },
{ {
"path": "pages/template/custom-pull-page/custom-pull-page", "path": "pages/template/custom-pull-page/custom-pull-page",
"style": { "style": {
"navigationBarTitleText": "自定义下拉上拉效果" "navigationBarTitleText": "自定义下拉上拉效果"
} }
}, },
{ {
"path": "pages/template/scroll-fold-nav/scroll-fold-nav", "path": "pages/template/scroll-fold-nav/scroll-fold-nav",
"style": { "style": {
"navigationStyle": "custom" "navigationStyle": "custom"
} }
}, },
{ {
"path": "pages/API/get-battery-info/get-battery-info", "path": "pages/API/get-battery-info/get-battery-info",
"style": { "style": {
......
...@@ -77,17 +77,101 @@ export default { ...@@ -77,17 +77,101 @@ export default {
console.log('onUnload', Date.now()) console.log('onUnload', Date.now())
}, },
methods: { methods: {
reLaunch() {
uni.reLaunch({
url: '/pages/tabBar/component',
success(result) {
console.log('reLaunch success', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
},
fail(error) {
console.log('reLaunch fail', error.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1)
},
complete(result) {
console.log('reLaunch complete', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
},
})
},
navigateTo() { navigateTo() {
uni.navigateTo({ uni.navigateTo({
url: '/pages/API/navigator/new-page/new-page?data=Hello', url: '/pages/API/navigator/new-page/new-page-1?data=Hello',
success(result) {
console.log('navigateTo success', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
},
fail(error) {
console.log('navigateTo fail', error.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1)
},
complete(result) {
console.log('navigateTo complete', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
},
})
},
navigateToErrorPage() {
uni.navigateTo({
url: '/pages/error-page/error-page',
success(result) { success(result) {
console.log('navigateTo success', result.errMsg) console.log('navigateTo success', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1)
}, },
fail(error) { fail(error) {
console.log('navigateTo fail', error.errMsg) console.log('navigateTo fail', error.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
}, },
complete(result) { complete(result) {
console.log('navigateTo complete', result.errMsg) console.log('navigateTo complete', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
},
})
},
navigateToDebounce() {
uni.navigateTo({
url: '/pages/API/navigator/new-page/new-page-1?data=debounce',
success(result) {
console.log('navigateTo success', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
},
fail(error) {
console.log('navigateTo fail', error.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1)
},
complete(result) {
console.log('navigateTo complete', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
},
})
uni.navigateTo({
url: '/pages/API/navigator/new-page/new-page-1?data=debounce',
success(result) {
console.log('navigateTo success', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1)
},
fail(error) {
console.log('navigateTo fail', error.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
},
complete(result) {
console.log('navigateTo complete', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
}, },
}) })
}, },
...@@ -95,26 +179,38 @@ export default { ...@@ -95,26 +179,38 @@ export default {
uni.navigateBack({ uni.navigateBack({
success(result) { success(result) {
console.log('navigateBack success', result.errMsg) console.log('navigateBack success', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
}, },
fail(error) { fail(error) {
console.log('navigateBack fail', error.errMsg) console.log('navigateBack fail', error.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1)
}, },
complete(result) { complete(result) {
console.log('navigateBack complete', result.errMsg) console.log('navigateBack complete', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
}, },
}) })
}, },
redirectTo() { redirectTo() {
uni.redirectTo({ uni.redirectTo({
url: '/pages/API/navigator/new-page/new-page?data=Hello', url: '/pages/API/navigator/new-page/new-page-1?data=Hello',
success(result) { success(result) {
console.log('redirectTo success', result.errMsg) console.log('redirectTo success', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
}, },
fail(error) { fail(error) {
console.log('redirectTo fail', error.errMsg) console.log('redirectTo fail', error.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1)
}, },
complete(result) { complete(result) {
console.log('redirectTo complete', result.errMsg) console.log('redirectTo complete', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
}, },
}) })
}, },
...@@ -123,32 +219,24 @@ export default { ...@@ -123,32 +219,24 @@ export default {
url: '/pages/tabBar/template', url: '/pages/tabBar/template',
success(result) { success(result) {
console.log('switchTab success', result.errMsg) console.log('switchTab success', result.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1)
}, },
fail(error) { fail(error) {
console.log('switchTab fail', error.errMsg) console.log('switchTab fail', error.errMsg)
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1)
}, },
complete(result) { complete(result) {
console.log('switchTab complete', result.errMsg) console.log('switchTab complete', result.errMsg)
}, // 自动化测试
}) setLifeCycleNum(state.lifeCycleNum + 1)
},
reLaunch() {
uni.reLaunch({
url: '/pages/tabBar/component',
success(result) {
console.log('reLaunch success', result.errMsg)
},
fail(error) {
console.log('reLaunch fail', error.errMsg)
},
complete(result) {
console.log('reLaunch complete', result.errMsg)
}, },
}) })
}, },
customAnimation() { customAnimation() {
uni.navigateTo({ uni.navigateTo({
url: '/pages/API/navigator/new-page/new-page?data=自定义动画打开页面', url: '/pages/API/navigator/new-page/new-page-1?data=自定义动画打开页面',
animationType: 'slide-in-bottom', animationType: 'slide-in-bottom',
animationDuration: 200, animationDuration: 200,
}) })
......
<template>
<view class="uni-padding-wrap">
<text>onLoad 接收到参数</text>
<text>data: {{ data }}</text>
</view>
</template>
<script lang="ts">
import { state } from '@/store/index.uts'
export default {
data() {
return {
data: '',
}
},
onLoad(options: OnLoadOptions) {
console.log('new-page2 onLoad', options)
if (options.has('data')) {
this.data = options.get('data')!
}
},
methods: {
// 自动化测试
getLifeCycleNum(): number {
return state.lifeCycleNum
},
},
}
</script>
const PAGE_PATH = '/pages/API/navigator/new-page/new-page' const PAGE_PATH = '/pages/API/navigator/new-page/new-page-2'
const INTERMEDIATE_PAGE_PATH = '/pages/API/navigator/navigator' const INTERMEDIATE_PAGE_PATH = '/pages/API/navigator/navigator'
let page
let lifeCycleNum
describe('page-lifecycle', () => { describe('app-lifecycle', () => {
let page it('onLaunch onShow', async () => {
let lifeCycleNum
beforeAll(async () => {
page = await program.reLaunch(INTERMEDIATE_PAGE_PATH) page = await program.reLaunch(INTERMEDIATE_PAGE_PATH)
await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(1100) expect(lifeCycleNum).toBe(1100)
})
it('onLastPageBackPress', async () => {
page = await program.navigateBack()
await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(100)
const initLifecycleNum = 0 await page.callMethod('setLifeCycleNum', 0)
await page.callMethod('setLifeCycleNum', initLifecycleNum)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(initLifecycleNum) expect(lifeCycleNum).toBe(0)
})
})
describe('page-lifecycle', () => {
it('onLoad onShow onReady', async () => {
page = await program.reLaunch(PAGE_PATH) page = await program.reLaunch(PAGE_PATH)
await page.waitFor(1000) await page.waitFor(1000)
})
it('onLoad onShow onReady', async () => {
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(120) expect(lifeCycleNum).toBe(120)
}) })
it('onPullDownRefresh', async () => {
await page.callMethod('pullDownRefresh')
await page.waitFor(1500)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(130)
})
// TODO: pageScrollTo 无法触发 onReachBottom, 待修复后调整 lifeCycleNum
it('onPageScroll onReachBottom', async () => {
await program.pageScrollTo(2000)
const data = await page.data()
expect(data.isScrolled).toBe(true)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(130)
})
it('onHide', async () => { it('onHide', async () => {
page = await program.navigateTo(INTERMEDIATE_PAGE_PATH) page = await program.navigateTo(INTERMEDIATE_PAGE_PATH)
await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(110) expect(lifeCycleNum).toBe(120)
page = await program.navigateBack() page = await program.navigateBack()
await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(120) expect(lifeCycleNum).toBe(130)
}) })
it('onUnload', async () => { it('onUnload', async () => {
page = await program.redirectTo(INTERMEDIATE_PAGE_PATH) page = await program.redirectTo(INTERMEDIATE_PAGE_PATH)
await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(20) expect(lifeCycleNum).toBe(30)
}) })
it('onBackPress', async () => { it('onBackPress', async () => {
page = await program.navigateTo(PAGE_PATH) page = await program.navigateTo(PAGE_PATH)
await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(140) expect(lifeCycleNum).toBe(150)
page = await program.navigateBack()
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(30)
})
it('onLastPageBackPress', async () => {
page = await program.navigateBack() page = await program.navigateBack()
await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(-970) expect(lifeCycleNum).toBe(40)
await page.callMethod('setLifeCycleNum', 0)
const resetLifecycleNum = 1100
await page.callMethod('setLifeCycleNum', resetLifecycleNum)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(resetLifecycleNum)
}) })
}) })
<template> <template>
<view class="uni-padding-wrap"> <view class="uni-padding-wrap container">
<text>onLoad 接收到参数</text> <button @click="pageScrollTo">scrollToBottom</button>
<text>data: {{ data }}</text>
</view> </view>
</template> </template>
<script lang="ts"> <script lang="ts">
...@@ -9,14 +8,10 @@ import { state, setLifeCycleNum } from '@/store/index.uts' ...@@ -9,14 +8,10 @@ import { state, setLifeCycleNum } from '@/store/index.uts'
export default { export default {
data() { data() {
return { return {
data: '', isScrolled: false,
} }
}, },
onLoad(options: OnLoadOptions) { onLoad(_: OnLoadOptions) {
console.log('new-page onLoad', options)
if(options.has('data')) {
this.data = options.get('data')!
}
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum + 100) setLifeCycleNum(state.lifeCycleNum + 100)
}, },
...@@ -28,15 +23,29 @@ export default { ...@@ -28,15 +23,29 @@ export default {
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum + 10) setLifeCycleNum(state.lifeCycleNum + 10)
}, },
onHide() { onPullDownRefresh() {
console.log('onPullDownRefresh')
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum - 10) setLifeCycleNum(state.lifeCycleNum + 10)
},
onPageScroll(_) {
// 自动化测试
this.isScrolled = true
},
onReachBottom() {
console.log('onReachBottom')
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 10)
}, },
onBackPress(_: OnBackPressOptions): boolean | null { onBackPress(_: OnBackPressOptions): boolean | null {
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum - 10) setLifeCycleNum(state.lifeCycleNum - 10)
return null return null
}, },
onHide() {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 10)
},
onUnload() { onUnload() {
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum - 100) setLifeCycleNum(state.lifeCycleNum - 100)
...@@ -50,6 +59,29 @@ export default { ...@@ -50,6 +59,29 @@ export default {
setLifeCycleNum(num: number) { setLifeCycleNum(num: number) {
setLifeCycleNum(num) setLifeCycleNum(num)
}, },
pullDownRefresh() {
uni.startPullDownRefresh({
success() {
setTimeout(() => {
uni.stopPullDownRefresh()
}, 1000)
},
})
},
pageScrollTo(){
uni.pageScrollTo({
scrollTop: 1200,
complete(res){
console.warn('pageScrollTo', res)
}
})
}
}, },
} }
</script> </script>
<style>
.container {
height: 1200px;
}
</style>
export type State = { class State {
colorIndex: number, constructor(public lifeCycleNum: number) {
currentColor: string, }
lifeCycleNum: number
} }
const colorList: string[] = ['#FF0000', '#00FF00', '#0000FF'] export const state = reactive(new State(0))
export const state = reactive({
colorIndex: 0,
currentColor: colorList[0],
lifeCycleNum: 0
} as State)
export const setColorIndex = (index: number) => {
state.colorIndex = index
state.currentColor = colorList[index]
}
export const setLifeCycleNum = (num: number) => { export const setLifeCycleNum = (num: number) => {
state.lifeCycleNum = num state.lifeCycleNum = num
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册