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

test: 新增 Interceptor switchTab 测试

上级 284984a6
...@@ -55,5 +55,22 @@ describe('interceptor', () => { ...@@ -55,5 +55,22 @@ describe('interceptor', () => {
const currentPage = await program.currentPage() const currentPage = await program.currentPage()
expect(currentPage.path).toBe('pages/API/interceptor/page1') expect(currentPage.path).toBe('pages/API/interceptor/page1')
await program.navigateBack() 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 @@ ...@@ -4,14 +4,15 @@
<button @click="removeInterceptor">移除路由拦截器</button> <button @click="removeInterceptor">移除路由拦截器</button>
<text>点击下方按钮{{ msg }}</text> <text>点击下方按钮{{ msg }}</text>
<button @click="navigateTo">navigatorTo API跳转到测试页面</button> <button @click="navigateTo">navigatorTo API跳转到测试页面</button>
<navigator url="./page1"><button class="navigatorButton"> <navigator url="./page1">
navigator组件跳转到测试页面 <button class="navigatorButton">navigator组件跳转到测试页面</button>
</button></navigator> </navigator>
<button class="navigatorButton" @click="switchTab">switchTab API</button>
</view> </view>
</template> </template>
<script> <script>
const interceptor = { const navigateToInterceptor = {
invoke: function (options : NavigateToOptions) { invoke: function (options : NavigateToOptions) {
console.log('拦截 navigateTo 接口传入参数为:', options) console.log('拦截 navigateTo 接口传入参数为:', options)
const url = './page2' const url = './page2'
...@@ -30,6 +31,23 @@ ...@@ -30,6 +31,23 @@
console.log('拦截 navigateTo 接口 complete 返回参数为:', res) console.log('拦截 navigateTo 接口 complete 返回参数为:', res)
} }
} as Interceptor } 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 { export default {
data() { data() {
return { return {
...@@ -39,21 +57,28 @@ ...@@ -39,21 +57,28 @@
beforeUnmount() { beforeUnmount() {
// 移除 navigateTo 所有拦截器 // 移除 navigateTo 所有拦截器
uni.removeInterceptor('navigateTo') uni.removeInterceptor('navigateTo')
uni.removeInterceptor('switchTab')
}, },
methods: { methods: {
addInterceptor() { addInterceptor() {
uni.addInterceptor('navigateTo', interceptor) uni.addInterceptor('navigateTo', navigateToInterceptor)
uni.showToast({ uni.showToast({
title: '页面跳转已拦截' title: '页面跳转/切换tabbar已拦截'
}) })
this.msg = ",路由被劫持到测试页面2" this.msg = ",路由被劫持到测试页面2"
}, },
removeInterceptor() { removeInterceptor() {
uni.removeInterceptor('navigateTo', interceptor) uni.removeInterceptor('navigateTo', navigateToInterceptor)
uni.showToast({ uni.showToast({
title: '拦截器已移除' title: '拦截器已移除'
}) })
this.msg = "会跳转到测试页面1" this.msg = "会跳转到测试页面1"
},
addSwitchTabInterceptor() {
uni.addInterceptor('switchTab', switchTabInterceptor)
},
removeSwitchTabInterceptor() {
uni.removeInterceptor('switchTab', switchTabInterceptor)
}, },
navigateTo() { navigateTo() {
uni.navigateTo({ uni.navigateTo({
...@@ -68,6 +93,20 @@ ...@@ -68,6 +93,20 @@
console.log('res:', res) 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.
先完成此消息的编辑!
想要评论请 注册