question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing `$watch` on `$route`

See original GitHub issue

Hi,

I have a test that is using a mock vuex store, and a real router. I want to test if an action is dispatched when the route changes.

I do something like:

SomeComponent:

export default {
  created() {
     this.$store.dispatch('someAction')
  },
  
  watch: {
    '$route' (newVal, val) {
        if (val.params.category !== newVal.params.category) {
          this.$stoore.dispatch('someAction')
        }
    }
  }
    

Test

import router from '../real-router'

describe('SomeComponent', () => {
  let actions;
  let store;

  beforeEach(() => {  
    actions = {
      someAction: jest.fn() // also tried sinon.mock()
    }
    store = new Vuex.Store({ state: {}, actions })
  })

  it('calls action on route change', () => { 
    mount(SomeComponent, { store, router })

    router.push({ name: 'someNamedRoute', params: { category: { 'okok' }})
    
    expect(actions.someAction.mocks.length).toBe(2)
  })
 })
})

For some reason, even though the $watch on the $route is triggering, the mock is not called again. I can confirm it does work in the created hook.

Is there something that would stop the action in a watch/route hook being called? Am I missing something? Or should I write this test different? I want to test that when the route changes, the action is called.

I am happy to contribute again if needed!

Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
peteringram0commented, Jan 5, 2018

For anyone else who comes across this issue it seems to be fixed without the need for vue-test-utils.

Working like the following:

it('Should collapse on route change', function (end) {

        const wrapper = mount(Nav, { router });

        wrapper.vm.navExpanded = true;

        expect(wrapper.vm.navExpanded).toBe(true);

        wrapper.vm.$router.push('/about');

        setTimeout(() => {
            expect(wrapper.vm.navExpanded).toBe(false);
            end();
        }, 0);

    });
1reaction
lmiller1990commented, Sep 28, 2017

Reopening (see above comment for repro).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to unit test VueJS watcher on $route - Stack Overflow
This is how I'm testing a watch on route change that adds the current route name as a css class to my app...
Read more >
Chapter 10. Testing Vue Router
Testing router properties. Vue Router adds two instance properties when you install it on Vue: the $route property and the $router property ...
Read more >
Vue.js 3 Unit Testing Routing Tutorial - KoderHQ
In this Vue tutorial we learn how to test the router and routing. We cover how to test with a local router and...
Read more >
Testing Vue Router
This article will present two ways to test an application using Vue Router: ... We could use a real router, then navigate to...
Read more >
Vue Router - Vue Testing Handbook
As usual, we start by importing the various modules for the test. Notably, we are importing the actual routes we will be using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found