How to test Vue watcher that watches a computed property from VueX?

后端 未结 4 1690
一个人的身影
一个人的身影 2021-02-12 12:53

Suppose I have the following component:

import { mapState } from \'vuex\';
import externalDependency from \'...\';

export default {
  name: \'Foo\',
  computed:         


        
相关标签:
4条回答
  • 2021-02-12 12:56

    The Vue Test Utils documentation points at a different approach where you use a very simple Vuex store:

    import { shallowMount, createLocalVue } from '@vue/test-utils'
    import Vuex from 'vuex'
    
    // use a localVue to prevent vuex state from polluting the global Vue instance
    const localVue = createLocalVue();
    localVue.use(Vuex);
    
    describe('Foo.vue', () => {
      let state;
      let store;
    
      beforeEach(() => {
        // create a new store for each test to prevent pollution
        state = { bar: 'bar' };
        store = new Vuex.Store({ state });
      })
    
      it('should call externalDependency.doThing with bar', () => 
      {
        shallowMount(MyComponent, { store, localVue });
        const spy = jest.spyOn(externalDependency, 'doThing');
        // trigger the watch
        state.bar = 'baz';
        expect(spy).toHaveBeenCalledWith('baz');
      });
    })
    
    0 讨论(0)
  • 2021-02-12 13:07

    You can set the value straight at the source, i.e. VueX. so you'd have something like this in your store.js:

    const state = {
      bar: 'foo',
    };
    const mutations = {
      SET_BAR: (currentState, payload) => {
        currentState.bar = payload;
      },
    };
    const actions = {
      setBar: ({ commit }, payload) => {
        commit('SET_BAR', payload);
      },
    };
    
    export const mainStore = {
      state,
      mutations,
      actions,
    };
    
    export default new Vuex.Store(mainStore);
    

    and then in your component.spec.js you'd do this:

    import { mainStore } from '../store';
    import Vuex from 'vuex';
    
    //... describe, and other setup functions
    it('should call externalDependency.doThing with bar', async () => {
      const localState = {
        bar: 'foo',
      };
      const localStore = new Vuex.Store({
          ...mainStore,
          state: localState,
      });
      const wrapper = mount(Foo, {
        store: localStore,
      });
      const spy = jest.spyOn(externalDependency, 'doThing');
      localStore.state.bar = 'baz';
      await wrapper.vm.$nextTick();
      expect(spy).toHaveBeenCalledWith('baz');
    });
    

    You can also call the dispatch('setBar', 'baz') method on the store and have the mutation occur properly instead of directly setting the state.

    NB It's important to re-initialize your state for every mount (i.e. either make a clone or re-declare it). Otherwise one tests can change the state and the next test will start with that dirty state, even if wrapper was destroyed.

    0 讨论(0)
  • 2021-02-12 13:19

    You will need some sort of mutator on the VueX instance, yes this does introduce another unrelated unit to the test but personally by your test including the use of Vuex, that concept has already been broken.

    Modifying the state in an unexpected way is more prone to cause behaviour that differs from the actual usage.

    0 讨论(0)
  • 2021-02-12 13:21

    From you're trying to achieve

    When testing, I want to ensure that externalDependency.doThing() is called with bar (which comes from the vuex state) like so:

    (and this is indeed pure unit test approach), you can just force change of this watcher, which basically is a function. There's no need to track if watcher is changing in case of computed or data value change - let Vue handle it. So, to change a watcher in a mounted Vue instance, just call it like

    wrapper.vm.$options.watch.bar.call(wrapper.vm)
    

    Where bar is name of your watcher. This way you will be able to test exact functionality that you're aiming to test.

    Idea taken from this comment https://github.com/vuejs/vue-test-utils/issues/331#issuecomment-382037200, on a vue-test-utils issue, mentioned by you in a question.

    0 讨论(0)
提交回复
热议问题