@onedaycat/vue-test-actions

A utility to unit test Vuex actions

Usage no npm install needed!

<script type="module">
  import onedaycatVueTestActions from 'https://cdn.skypack.dev/@onedaycat/vue-test-actions';
</script>

README

npm (scoped) GitHub Coverage Status Build Status

vue-test-actions

A utility to unit test Vuex actions

Install

npm install @onedaycat/vue-test-actions

or

yarn add @onedaycat/vue-test-actions

Usage

import testAction from '@onedaycat/vue-test-actions'

const value = await testAction(ACTION, EXPECTED_MUTATIONS, EXPECTED_DISPATCHS, ACTION_PAYLOAD, STORE)

Example

types.ts

export enum Mutations {
  FETCH_PRODUCT_BEGIN = 'fetchProductBegin',
  FETCH_PRODUCT_SUCCESS = 'fetchProductSuccess',
  FETCH_PRODUCT_FAILURE = 'fetchProductFailure',
}

actions.ts

import { ActionTree } from 'vuex'
import { Mutations } from './types'

const actions: ActionTree<Store.Product, Store.Root> = {
  async fetchProduct(context, payload: number) {
    context.commit(Mutations.FETCH_PRODUCT_BEGIN)
    try {
      context.commit(Mutations.FETCH_PRODUCT_SUCCESS, payload)
    } catch (e) {
      context.commit(Mutations.FETCH_PRODUCT_FAILURE, e)
    }
  },
}

export default actions

actions.test.ts

import { Mutations } from './types'
import testAction from '@onedaycat/vue-test-actions'
import actions from './actions'

it('should fetch product success', async () => {
  const actionPayload = 1
  const expectedMutations = [{
    type: Mutations.FETCH_PRODUCT_BEGIN,
  }, {
    type: Mutations.FETCH_PRODUCT_SUCCESS,
    payload: actionPayload,
  }]

  const expectedDispatchs = []

  await testAction(actions.fetchProduct, expectedMutations, expectedDispatchs, actionPayload)
})

Contributing

  1. Fork this repository.
  2. Create new branch with feature name in format feature/FEATURE_NAME
  3. Run npm install or yarn
  4. Create your feature.
  5. Commit and set commit message with feature name.
  6. Push your code to your fork repository.
  7. Create pull request.

Licence

MIT