You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-11Lines changed: 11 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1657,7 +1657,7 @@ This topic has been fully covered by [the official documentation](https://vue-te
1657
1657
1658
1658
## Testing navigation guards
1659
1659
1660
-
Unfortunately, there is no easy way how to test navigation guards. If you want to simulate the event triggering by calling `router.push` function, you are going to have a hard time. ~~Better~~Easier solution is to call the guard manually in `beforeEach()`, but even this solution doesn't have clean approach. See following examples:
1660
+
Unfortunately, there is no easy way to test navigation guards. If you want to simulate the event triggering by calling `router.push` function, you are going to have a hard time. A ~~better~~easier solution is to call the guard manually in `beforeEach()`, but even this solution doesn't have a clean approach. See the following example:
1661
1661
1662
1662
### beforeRouteEnter
1663
1663
@@ -1675,39 +1675,39 @@ class MyView extends Vue {
1675
1675
```js
1676
1676
// my-view.spec.js
1677
1677
it('should trigger beforeRouteEnter event', function () {
1678
-
let view =mount(MyView);
1679
-
let spy =sinon.spy(view.vm.$options.beforeRouteEnter, '0'); // you cannot just call view.vm.beforeRouteEnter(). The function exists only in $options object.
1678
+
constview=mount(MyView);
1679
+
constspy=sinon.spy(view.vm.$options.beforeRouteEnter, '0'); // you can't just call view.vm.beforeRouteEnter(). The function exists only in $options object.
In case you are wondering how to do the same thing using `router.push`, so you don't have to mock routes and callback function, this is the way:
1690
+
In case you are wondering how to do the same thing using `router.push`, in a way that you don't have to mock routes and callback functions, this is the way to go:
1691
1691
1692
1692
```js
1693
1693
it('should trigger beforeRouteEnter event', function () {
1694
-
let view =mount(MyView);
1695
-
let spy =sinon.spy(view.vm.$options.beforeRouteEnter, '0'); // beforeRouteEnter is an array of callback functions
1694
+
constview=mount(MyView);
1695
+
constspy=sinon.spy(view.vm.$options.beforeRouteEnter, '0'); // beforeRouteEnter is an array of callback functions
1696
1696
1697
-
// at this moment, this.route.currentRoute doesn't exist. We need to push route we want to have in the $route object inside out view
1697
+
// at this moment, this.route.currentRoute doesn't exist. We need to push the route we want to have in the $route object inside our view
1698
1698
this.router.push({ name:'my-route' });
1699
1699
1700
1700
// now the route exists, but, because it's not resolved (committed) yet, the route is not aware of the view. we need to assign the mounted view to the instances
// now we will finally execute the life cycle of the router, which will go through global event, then it will check if default instance on matched route exist and if so, it will call the beforeRouteEnter event on it.
1702
+
// now we will finally execute the life cycle of the router, which will go through global event, then it will check if default instance on the matched route exist and if so, it will call the beforeRouteEnter event on it.
1703
1703
awaitflushPromises();
1704
1704
1705
1705
expect(view.vm.entered).to.be.true;
1706
1706
expect(spy).to.have.been.called;
1707
1707
});
1708
1708
```
1709
1709
1710
-
This approach requires a knowledge about VUE and router internals and **can stop working** after every minor refactoring done by the VUE team. I do not recommend to use it unless guys from VUE provide some helper function in [@vue/test-utils](https://github.com/vuejs/vue-test-utils) library for setting it up using cleaner way.
1710
+
This approach requires VUE and router internals knowledge, keep in mind that it **can stop working** after every minor refactoring done by the VUE core team. I do not recommend to use it unless guys from VUE provide some helper function in [@vue/test-utils](https://github.com/vuejs/vue-test-utils) library for setting it up using a cleaner approach.
0 commit comments