提交 379e7373 编写于 作者: H hdx

test: 新增 Interceptor switchTab 测试

上级 284984a6
......@@ -56,4 +56,21 @@ describe('interceptor', () => {
expect(currentPage.path).toBe('pages/API/interceptor/page1')
await program.navigateBack()
})
it('addSwitchTabInterceptor', async () => {
await page.callMethod('addSwitchTabInterceptor')
await page.callMethod('switchTab')
const currentPage = await program.currentPage()
expect(currentPage.path).toBe('pages/tabBar/API')
})
it('removeSwitchTabInterceptor', async () => {
const currentPage1 = await program.navigateTo(PAGE_PATH)
await currentPage1.callMethod('addSwitchTabInterceptor')
await currentPage1.callMethod('removeSwitchTabInterceptor')
await currentPage1.callMethod('switchTab')
const currentPage2 = await program.currentPage()
expect(currentPage2.path).toBe('pages/tabBar/component')
})
})
......@@ -4,14 +4,15 @@
<button @click="removeInterceptor">移除路由拦截器</button>
<text>点击下方按钮{{ msg }}</text>
<button @click="navigateTo">navigatorTo API跳转到测试页面</button>
<navigator url="./page1"><button class="navigatorButton">
navigator组件跳转到测试页面
</button></navigator>
<navigator url="./page1">
<button class="navigatorButton">navigator组件跳转到测试页面</button>
</navigator>
<button class="navigatorButton" @click="switchTab">switchTab API</button>
</view>
</template>
<script>
const interceptor = {
const navigateToInterceptor = {
invoke: function (options : NavigateToOptions) {
console.log('拦截 navigateTo 接口传入参数为:', options)
const url = './page2'
......@@ -30,6 +31,23 @@
console.log('拦截 navigateTo 接口 complete 返回参数为:', res)
}
} as Interceptor
const switchTabInterceptor = {
invoke: function (options : SwitchTabOptions) {
console.log('拦截 switchTab 接口传入参数为:', options)
options.url = 'pages/tabBar/API'
},
success: function (res : SwitchTabSuccess) {
console.log('拦截 switchTab 接口 success 返回参数为:', res)
},
fail: function (err : SwitchTabFail) {
console.log('拦截 switchTab 接口 fail 返回参数为:', err)
},
complete: function (res : SwitchTabComplete) {
console.log('拦截 switchTab 接口 complete 返回参数为:', res)
}
} as Interceptor
export default {
data() {
return {
......@@ -39,22 +57,29 @@
beforeUnmount() {
// 移除 navigateTo 所有拦截器
uni.removeInterceptor('navigateTo')
uni.removeInterceptor('switchTab')
},
methods: {
addInterceptor() {
uni.addInterceptor('navigateTo', interceptor)
uni.addInterceptor('navigateTo', navigateToInterceptor)
uni.showToast({
title: '页面跳转已拦截'
title: '页面跳转/切换tabbar已拦截'
})
this.msg = ",路由被劫持到测试页面2"
},
removeInterceptor() {
uni.removeInterceptor('navigateTo', interceptor)
uni.removeInterceptor('navigateTo', navigateToInterceptor)
uni.showToast({
title: '拦截器已移除'
})
this.msg = "会跳转到测试页面1"
},
addSwitchTabInterceptor() {
uni.addInterceptor('switchTab', switchTabInterceptor)
},
removeSwitchTabInterceptor() {
uni.removeInterceptor('switchTab', switchTabInterceptor)
},
navigateTo() {
uni.navigateTo({
url: './page1',
......@@ -68,6 +93,20 @@
console.log('res:', res)
}
})
},
switchTab() {
uni.switchTab({
url: 'pages/tabBar/component',
success(res) {
console.log('res:', res)
},
fail(err) {
console.error('err:', err)
},
complete(res) {
console.log('res:', res)
}
})
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册