如何在RouteUpdate导航防护之前测试Vue路由器? [英] How to test vue router beforeRouteUpdate navigation guard?
本文介绍了如何在RouteUpdate导航防护之前测试Vue路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我正在使用Vue-property-decorator. 我的组件具有这样定义的组件路由.
First off, I'm using Vue-property-decorator. My component has a component route, defined like this.
@Component({
components: {
ComponentA,
ComponentB
},
beforeRouteUpdate (to, _from, next) {
const { checkInDate, checkOutDate, items, currency, locale } = to.query;
const queryFullfilled = !!checkInDate && !!checkOutDate && !!items.length && !!currency && !!locale;
if ([BOOKING_PAGE, RESERVATION_PAGE].indexOf(to.name) > -1 && queryFullfilled) {
this.validateRooms();
}
next();
}
})
export default class Layout extends Vue {}
为了涵盖beforeRouteUpdate中发生的事情,我需要怎么做?我已经用谷歌搜索了,人们指的是呼叫wrapper.vm.$options.beforeRouteUpdate.call()
...对我不起作用.
What do I need to do in my specs in order to cover what goes on in beforeRouteUpdate? I've googled this and people are pointing calling wrapper.vm.$options.beforeRouteUpdate.call()
... that didn't work for me.
有人做过吗?我可以看任何代码示例吗?
Has anyone done this before? Any code samples I can look at?
先谢谢了. 约翰.
推荐答案
如果仍在寻找答案的人可以使用以下代码.
If anyone still looking for an answer following code will work.
const beforeRouteUpdate = wrapper.vm.$options.beforeRouteUpdate[0];
let nextFun = jest.fn();
// in place wrapper.vm you can send any object you want bcz when we call beforeRouteUpdate it looses this context
beforeRouteUpdate.call(wrapper.vm , "toObj", "fromObj", nextFun);
这篇关于如何在RouteUpdate导航防护之前测试Vue路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文