From 23f789b1346e9cae0b283d0df295ca068c618acd Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Wed, 2 Aug 2023 15:49:08 +0800 Subject: [PATCH] test(API): navigateBack delta --- pages/API/navigator/navigator.test.js | 87 +++++++++++--------- pages/API/navigator/navigator.uvue | 50 +++++++++++ pages/API/navigator/new-page/new-page-1.uvue | 1 - 3 files changed, 98 insertions(+), 40 deletions(-) diff --git a/pages/API/navigator/navigator.test.js b/pages/API/navigator/navigator.test.js index d11a0088..fec3fec3 100644 --- a/pages/API/navigator/navigator.test.js +++ b/pages/API/navigator/navigator.test.js @@ -1,20 +1,25 @@ jest.setTimeout(40000) const CURRENT_PAGE_PATH = '/pages/API/navigator/navigator' -const HOME_PAGE_PATH = '/pages/tabBar/component' describe('navigator', () => { let page let lifeCycleNum + + afterAll(async () => { + const resetLifecycleNum = 1100 + await page.callMethod('setLifeCycleNum', resetLifecycleNum) + lifeCycleNum = await page.callMethod('getLifeCycleNum') + expect(lifeCycleNum).toBe(resetLifecycleNum) + }) + it('reLaunch', async () => { page = await program.navigateTo(CURRENT_PAGE_PATH) - await page.waitFor(1000) + await page.waitFor(500) await page.callMethod('setLifeCycleNum', 0) await page.callMethod('reLaunch') - await new Promise((resolve) => { - setTimeout(resolve, 1000) - }) + await page.waitFor(500) page = await program.navigateTo(CURRENT_PAGE_PATH) - await page.waitFor(1000) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) }) @@ -22,19 +27,17 @@ describe('navigator', () => { it('navigateTo', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('navigateTo') - await new Promise((resolve) => { - setTimeout(resolve, 1000) - }) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) page = await program.navigateBack() - await page.waitFor(1000) + await page.waitFor(500) }) it('navigateTo error page', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('navigateToErrorPage') - await page.waitFor(1000) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) }) @@ -42,53 +45,70 @@ describe('navigator', () => { it('navigateTo debounce', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('navigateToDebounce') - await new Promise((resolve) => { - setTimeout(resolve, 1000) - }) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(4) page = await program.navigateBack() - await page.waitFor(1000) + await page.waitFor(500) }) it('navigateTo relative path', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('navigateToRelativePath1') page = await program.navigateBack() - await page.waitFor(1000) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) - await page.waitFor(1000) + await page.waitFor(500) }) it('navigateTo relative path ./', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('navigateToRelativePath2') page = await program.navigateBack() - await page.waitFor(1000) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) - await page.waitFor(1000) + await page.waitFor(500) }) it('navigateTo relative path ../', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('navigateToRelativePath3') page = await program.navigateBack() - await page.waitFor(1000) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) - await page.waitFor(1000) + await page.waitFor(500) }) it('navigateBack', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('navigateBack') - await new Promise((resolve) => { - setTimeout(resolve, 1000) - }) + await page.waitFor(500) page = await program.navigateTo(CURRENT_PAGE_PATH) + await page.waitFor(500) + lifeCycleNum = await page.callMethod('getLifeCycleNum') + expect(lifeCycleNum).toBe(2) + }) + + it('navigateBackWithDelta1', async () => { + await page.callMethod('setLifeCycleNum', 0) + await page.callMethod('navigateBackWithDelta1') + await page.waitFor(1000) + page = await program.currentPage() + expect(`/${page.path}`).toBe(CURRENT_PAGE_PATH) + lifeCycleNum = await page.callMethod('getLifeCycleNum') + expect(lifeCycleNum).toBe(2) + }) + + it('navigateBackWithDelta100', async () => { + page = await program.reLaunch(CURRENT_PAGE_PATH) + await page.callMethod('setLifeCycleNum', 0) + await page.callMethod('navigateBackWithDelta100') await page.waitFor(1000) + page = await program.currentPage() + expect(`/${page.path}`).toBe(CURRENT_PAGE_PATH) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) }) @@ -96,11 +116,9 @@ describe('navigator', () => { it('redirectTo', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('redirectTo') - await new Promise((resolve) => { - setTimeout(resolve, 1000) - }) + await page.waitFor(500) page = await program.redirectTo(CURRENT_PAGE_PATH) - await page.waitFor(1000) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) }) @@ -108,19 +126,10 @@ describe('navigator', () => { it('switchTab', async () => { await page.callMethod('setLifeCycleNum', 0) await page.callMethod('switchTab') - await new Promise((resolve) => { - setTimeout(resolve, 1000) - }) + await page.waitFor(500) page = await program.navigateTo(CURRENT_PAGE_PATH) - await page.waitFor(1000) + await page.waitFor(500) lifeCycleNum = await page.callMethod('getLifeCycleNum') expect(lifeCycleNum).toBe(2) }) - - it('reset lifecycle', async () => { - const resetLifecycleNum = 1100 - await page.callMethod('setLifeCycleNum', resetLifecycleNum) - lifeCycleNum = await page.callMethod('getLifeCycleNum') - expect(lifeCycleNum).toBe(resetLifecycleNum) - }) }) diff --git a/pages/API/navigator/navigator.uvue b/pages/API/navigator/navigator.uvue index dd9b64a4..3a2125ad 100644 --- a/pages/API/navigator/navigator.uvue +++ b/pages/API/navigator/navigator.uvue @@ -240,6 +240,56 @@ export default { }, }) }, + navigateBackWithDelta1() { + uni.navigateTo({ + url: '/pages/API/navigator/new-page/new-page-1', + success() { + uni.navigateBack({ + delta: 1, + 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) + }, + }) + }, + }) + }, + navigateBackWithDelta100() { + uni.navigateTo({ + url: '/pages/API/navigator/new-page/new-page-1', + success() { + uni.navigateBack({ + delta: 100, + 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-1?data=Hello', diff --git a/pages/API/navigator/new-page/new-page-1.uvue b/pages/API/navigator/new-page/new-page-1.uvue index 8f7b2ee4..c48c3ef8 100644 --- a/pages/API/navigator/new-page/new-page-1.uvue +++ b/pages/API/navigator/new-page/new-page-1.uvue @@ -15,7 +15,6 @@ export default { } }, onLoad(options: OnLoadOptions) { - console.log('new-page2 onLoad', options) if (options.has('data')) { this.data = options.get('data')! } -- GitLab