0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vuexの名前空間を使ったactionsをJestでMockする

Posted at

はじめに

公式に名前空間を使わないactionsをMockする方法は記載されていますが、名前空間を使ったactionsのMock方法が見当たらなかったので、試してみてできた方法をここに記載します。

やり方

対象のコンポーネント

こんなmethodsを持っているコンポーネントを想定します。

  methods: {
    ...mapActions('namespaceA', ['hoge']),
    ...mapActions('namespaceB', ['fuga']),
}

Mock

こんな感じでMockできます。

const localVue = createLocalVue()
localVue.use(Vuex)
const actions = {
  'namespaceA/hoge': jest.fn(),
  'namespaceB/fuga': jest.fn()
}

const wrapper = shallowMount(Component, {
    store: new Vuex.Store({
      actions,
      mutations: {},
      state: {},
    }),
    localVue
})

テスト

こんな感じでテストできます。

expect(actions['namespaceA/hoge']).toHaveBeenCalled()
expect(actions['namespaceB/fuga']).toHaveBeenCalled()

もしかしたら、もっとスマートな方法があるかもしれませんが、こういう方法もあるということで。。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?