From 239477b97be0f5c3df162f120a6efffc0f584d2c Mon Sep 17 00:00:00 2001
From: zhenyuWang <13641039885@163.com>
Date: Mon, 17 Jul 2023 11:08:51 +0800
Subject: [PATCH] feat: navigator & lifecycle
---
pages.json | 61 +++++----
pages/API/navigator/navigator.uvue | 122 +++++++++++++++---
pages/API/navigator/new-page/new-page-1.uvue | 28 ++++
.../{new-page.test.js => new-page-2.test.js} | 68 ++++++----
pages/API/navigator/new-page/new-page-2.uvue | 87 +++++++++++++
pages/API/navigator/new-page/new-page.uvue | 55 --------
store/index.uts | 20 +--
7 files changed, 301 insertions(+), 140 deletions(-)
create mode 100644 pages/API/navigator/new-page/new-page-1.uvue
rename pages/API/navigator/new-page/{new-page.test.js => new-page-2.test.js} (56%)
create mode 100644 pages/API/navigator/new-page/new-page-2.uvue
delete mode 100644 pages/API/navigator/new-page/new-page.uvue
diff --git a/pages.json b/pages.json
index fffd7632..c47e5494 100644
--- a/pages.json
+++ b/pages.json
@@ -121,13 +121,13 @@
"style": {
"navigationBarTitleText": "大图测试"
}
- },
- {
- "path": "pages/component/video/video",
- "style": {
- "navigationBarTitleText": "video"
- }
- },
+ },
+ {
+ "path": "pages/component/video/video",
+ "style": {
+ "navigationBarTitleText": "video"
+ }
+ },
{
"path": "pages/component/web-view/web-view",
"style": {
@@ -154,9 +154,16 @@
}
},
{
- "path": "pages/API/navigator/new-page/new-page",
+ "path": "pages/API/navigator/new-page/new-page-1",
"style": {
- "navigationBarTitleText": "新页面"
+ "navigationBarTitleText": "新页面-1"
+ }
+ },
+ {
+ "path": "pages/API/navigator/new-page/new-page-2",
+ "style": {
+ "navigationBarTitleText": "新页面-2",
+ "enablePullDownRefresh": true
}
},
{
@@ -592,24 +599,24 @@
"navigationBarTitleText": "详情示例"
}
},
- {
- "path": "pages/template/swiper-vertical-video/swiper-vertical-video",
- "style": {
- "navigationBarTitleText": ""
- }
- },
- {
- "path": "pages/template/custom-pull-page/custom-pull-page",
- "style": {
- "navigationBarTitleText": "自定义下拉上拉效果"
- }
- },
- {
- "path": "pages/template/scroll-fold-nav/scroll-fold-nav",
- "style": {
- "navigationStyle": "custom"
- }
- },
+ {
+ "path": "pages/template/swiper-vertical-video/swiper-vertical-video",
+ "style": {
+ "navigationBarTitleText": ""
+ }
+ },
+ {
+ "path": "pages/template/custom-pull-page/custom-pull-page",
+ "style": {
+ "navigationBarTitleText": "自定义下拉上拉效果"
+ }
+ },
+ {
+ "path": "pages/template/scroll-fold-nav/scroll-fold-nav",
+ "style": {
+ "navigationStyle": "custom"
+ }
+ },
{
"path": "pages/API/get-battery-info/get-battery-info",
"style": {
diff --git a/pages/API/navigator/navigator.uvue b/pages/API/navigator/navigator.uvue
index 9b05385d..faaa5426 100644
--- a/pages/API/navigator/navigator.uvue
+++ b/pages/API/navigator/navigator.uvue
@@ -77,17 +77,101 @@ export default {
console.log('onUnload', Date.now())
},
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() {
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) {
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)
+ },
+ })
+ },
+ 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 {
uni.navigateBack({
success(result) {
console.log('navigateBack success', result.errMsg)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum + 1)
},
fail(error) {
console.log('navigateBack fail', error.errMsg)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum - 1)
},
complete(result) {
console.log('navigateBack complete', result.errMsg)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum + 1)
},
})
},
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) {
console.log('redirectTo success', result.errMsg)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum + 1)
},
fail(error) {
console.log('redirectTo fail', error.errMsg)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum - 1)
},
complete(result) {
console.log('redirectTo complete', result.errMsg)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum + 1)
},
})
},
@@ -123,32 +219,24 @@ export default {
url: '/pages/tabBar/template',
success(result) {
console.log('switchTab success', result.errMsg)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum + 1)
},
fail(error) {
console.log('switchTab fail', error.errMsg)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum - 1)
},
complete(result) {
console.log('switchTab complete', result.errMsg)
- },
- })
- },
- 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)
+ // 自动化测试
+ setLifeCycleNum(state.lifeCycleNum + 1)
},
})
},
customAnimation() {
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',
animationDuration: 200,
})
diff --git a/pages/API/navigator/new-page/new-page-1.uvue b/pages/API/navigator/new-page/new-page-1.uvue
new file mode 100644
index 00000000..07efc3fd
--- /dev/null
+++ b/pages/API/navigator/new-page/new-page-1.uvue
@@ -0,0 +1,28 @@
+
+
+ onLoad 接收到参数
+ data: {{ data }}
+
+
+
diff --git a/pages/API/navigator/new-page/new-page.test.js b/pages/API/navigator/new-page/new-page-2.test.js
similarity index 56%
rename from pages/API/navigator/new-page/new-page.test.js
rename to pages/API/navigator/new-page/new-page-2.test.js
index 14d4d5b7..45cff1a3 100644
--- a/pages/API/navigator/new-page/new-page.test.js
+++ b/pages/API/navigator/new-page/new-page-2.test.js
@@ -1,55 +1,73 @@
-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'
+let page
+let lifeCycleNum
-describe('page-lifecycle', () => {
- let page
- let lifeCycleNum
- beforeAll(async () => {
+describe('app-lifecycle', () => {
+ it('onLaunch onShow', async () => {
page = await program.reLaunch(INTERMEDIATE_PAGE_PATH)
+ await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
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', initLifecycleNum)
+ await page.callMethod('setLifeCycleNum', 0)
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)
await page.waitFor(1000)
- })
- it('onLoad onShow onReady', async () => {
lifeCycleNum = await page.callMethod('getLifeCycleNum')
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 () => {
page = await program.navigateTo(INTERMEDIATE_PAGE_PATH)
+ await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
- expect(lifeCycleNum).toBe(110)
+ expect(lifeCycleNum).toBe(120)
page = await program.navigateBack()
+ await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
- expect(lifeCycleNum).toBe(120)
+ expect(lifeCycleNum).toBe(130)
})
it('onUnload', async () => {
page = await program.redirectTo(INTERMEDIATE_PAGE_PATH)
+ await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
- expect(lifeCycleNum).toBe(20)
+ expect(lifeCycleNum).toBe(30)
})
it('onBackPress', async () => {
page = await program.navigateTo(PAGE_PATH)
+ await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
- expect(lifeCycleNum).toBe(140)
- page = await program.navigateBack()
- lifeCycleNum = await page.callMethod('getLifeCycleNum')
- expect(lifeCycleNum).toBe(30)
- })
- it('onLastPageBackPress', async () => {
+ expect(lifeCycleNum).toBe(150)
page = await program.navigateBack()
+ await page.waitFor(1000)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
- expect(lifeCycleNum).toBe(-970)
-
- const resetLifecycleNum = 1100
- await page.callMethod('setLifeCycleNum', resetLifecycleNum)
- lifeCycleNum = await page.callMethod('getLifeCycleNum')
- expect(lifeCycleNum).toBe(resetLifecycleNum)
+ expect(lifeCycleNum).toBe(40)
+ await page.callMethod('setLifeCycleNum', 0)
})
})
diff --git a/pages/API/navigator/new-page/new-page-2.uvue b/pages/API/navigator/new-page/new-page-2.uvue
new file mode 100644
index 00000000..05b49e0f
--- /dev/null
+++ b/pages/API/navigator/new-page/new-page-2.uvue
@@ -0,0 +1,87 @@
+
+
+
+
+
+
+
+
diff --git a/pages/API/navigator/new-page/new-page.uvue b/pages/API/navigator/new-page/new-page.uvue
deleted file mode 100644
index 0032290b..00000000
--- a/pages/API/navigator/new-page/new-page.uvue
+++ /dev/null
@@ -1,55 +0,0 @@
-
-
- onLoad 接收到参数
- data: {{ data }}
-
-
-
diff --git a/store/index.uts b/store/index.uts
index 7bf68743..e60cf830 100644
--- a/store/index.uts
+++ b/store/index.uts
@@ -1,21 +1,9 @@
-export type State = {
- colorIndex: number,
- currentColor: string,
- lifeCycleNum: number
+class State {
+ constructor(public lifeCycleNum: number) {
+ }
}
-const colorList: string[] = ['#FF0000', '#00FF00', '#0000FF']
-
-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 state = reactive(new State(0))
export const setLifeCycleNum = (num: number) => {
state.lifeCycleNum = num
--
GitLab