diff --git a/pages/API/navigator/navigator.test.js b/pages/API/navigator/navigator.test.js index d11a0088cc2da3d34dba65ba628a302a244c7a4b..fec3fec344a3a02382b3c34aa6431bfb11ae98da 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 dd9b64a457227c0b7cfe4460945a26dc6f53fad0..3a2125adde0e74a9585fe7dd18ce6010abd11ef1 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 8f7b2ee4c1ba60b1a300be8072a21e2b23385bf8..c48c3ef8a959ab22c0a103bd7e2ddf01d3bfa1b9 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')! }