@onedaycat/vue-test-actions
v1.0.6
Published
A utility to unit test Vuex actions
Downloads
5
Readme
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
- Fork this repository.
- Create new branch with feature name in format
feature/FEATURE_NAME
- Run
npm install
oryarn
- Create your feature.
- Commit and set commit message with feature name.
- Push your code to your fork repository.
- Create pull request.