Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate testAction helper in favor of vuex-mock-store #1562

Open
winniehell opened this issue May 31, 2019 · 7 comments
Open

Deprecate testAction helper in favor of vuex-mock-store #1562

winniehell opened this issue May 31, 2019 · 7 comments
Labels
documentation Improvements or additions to documentation

Comments

@winniehell
Copy link

winniehell commented May 31, 2019

What problem does this feature solve?

The officially documented way to test actions

  • has a complex interface (5 positional, required arguments)
  • is always asynchronous—even if the action under test is not
  • requires copying a helper function from the documentation into a project's code base
  • involves manually patching that helper because of some shortcomings (see for example Proposal: Enhancement for testAction helper method #939)
  • offers no way to automatically update the copied helper once it changes in the documentation

What does the proposed API look like?

Recommend to use https://github.com/posva/vuex-mock-store instead.

// actions.js

import shop from '../api/shop'

export const getAllProducts = ({ commit }) => {
  commit('REQUEST_PRODUCTS')
  return shop.getProducts().then(products => {
    commit('RECEIVE_PRODUCTS', products)
  })
}
// actions.spec.js

import { Store } from 'vuex-mock-store'

const store = new Store()

afterEach(() => store.reset())

describe('actions', () => {
  it('getAllProducts', done => {
    actions.getAllProducts(store)
      .then(() => {
        expect(store.commit).toHaveBeenCalledTimes(2)
        expect(store.commit).toHaveBeenCalledWith('REQUEST_PRODUCTS')
        expect(store.commit).toHaveBeenCalledWith('RECEIVE_PRODUCTS',  /* mocked response */)
      })
      .then(done)
      .catch(done.fail)
})

see also vuejs/vue-test-utils#1060

@winniehell
Copy link
Author

@posva What do you think?

@posva
Copy link
Member

posva commented Jun 3, 2019

I think my mock doesn't cover testing actions so far. To be honest, when testing actions I always pass things manually using jest.fn() to create mocks and just see if they were called.

I was thinking of adding some kind of api to generate a context that call other actions and mutations: posva/vuex-mock-store#9

@winniehell
Copy link
Author

@posva Thanks for the fast reply! 🚀

I think my mock doesn't cover testing actions so far.

Ignoring Vuex modules for now (which aren't supported by the existing testAction helper either), what do you think is missing? Judging from the API docs, your mock store seems to provide everything from the context apart from rootState and rootGetters which should only be relevant in the presence of Vuex modules.

I always pass things manually using jest.fn() to create mocks

Isn't that exactly what your mock store does with dispatch and commit?

@posva
Copy link
Member

posva commented Jun 3, 2019

but the Store mock can mock the whole store so you don't care about it in components. testAction is a helper to individually test an action, which you should still do if you are mocking the whole Store

@winniehell
Copy link
Author

@posva You are only advertising vuex-mock-store for unit testing components but I think it can be used to unit test actions, too, because it has all we need. 😃

When unit testing actions, we want to mock away the state, getters, and mutations. All of that is already supported by vuex-mock-store. 🎉

Here is a working example project: https://gitlab.com/winniehell/vue-examples/blob/master/vuex-mock-action-tests/test/store/actions.spec.js

@posva
Copy link
Member

posva commented Jun 4, 2019

oh right, you pass the store to the action, I didn't see that! That's neat. Adding support for rootState and rootGetters could be added as well either directly on the store or with a getContext() method. Now, that I think about it, it could be useful to have o Module so modules can also be tested individually, but I need to think more about it

@winniehell
Copy link
Author

Adding support for rootState and rootGetters could be added as well

@posva Do you think that is a requirement to replace the testAction helper? or can that be a second iteration?

with a getContext() method

I think such a method would be cleaner. 👍

it could be useful to have o Module

I agree! 👍 I would be happy to help adding that to vuex-mock-store if you like.

@kiaking kiaking added the documentation Improvements or additions to documentation label Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants