|
1 | | -# Vue Test Actions |
| 1 | +<p> |
| 2 | +<a href="https://npmjs.com/package/vue-test-actions"><img src="https://img.shields.io/npm/v/vue-test-actions.svg?style=flat" alt="NPM version"></a> |
| 3 | +<a href="https://npmjs.com/package/vue-test-actions"><img src="https://img.shields.io/npm/dm/vue-test-actions.svg?style=flat" alt="NPM downloads"></a> |
| 4 | +<a href="https://www.npmjs.com/package/vue-test-actions"><img src="https://img.shields.io/npm/l/vue-test-actions.svg?style=flat" alt="License"></a> |
| 5 | +<a href="https://biigpongsatorn.github.io/#/vue-test-actions"><img src="https://travis-ci.org/biigpongsatorn/biigpongsatorn.github.io.svg?branch=dev" alt="build status"></a> |
| 6 | +</p> |
2 | 7 |
|
3 | | -β
Unit testing Vuex actions with Jest mocks. (π§ WIP) |
| 8 | +# β
Vue Test Actions |
4 | 9 |
|
5 | | -# Ref. |
| 10 | +Unit testing Vuex actions with Jest mocks. |
6 | 11 |
|
7 | | -- [vuex-jest-mocking-examples](https://github.com/lmiller1990/vuex-jest-mocking-examples) |
8 | | -- [vue-test-utils](https://github.com/vuejs/vue-test-utils) |
9 | | -- [vuex.vuejs.org/guide/testing](https://vuex.vuejs.org/guide/testing.html) |
10 | | -- [forum.vuejs](https://forum.vuejs.org/t/how-to-test-a-vuex-store-action-with-rootsate/22931) |
11 | | -- [vue-test-utils](https://github.com/vuejs/vue-test-utils) |
| 12 | +# π» Install |
| 13 | + |
| 14 | +```sh |
| 15 | +npm i --save vue-test-actions |
| 16 | +``` |
| 17 | +or |
| 18 | +```sh |
| 19 | +yarn add vue-test-actions |
| 20 | +``` |
| 21 | + |
| 22 | +# πΉ Usage |
| 23 | + |
| 24 | +```javascript |
| 25 | +import testAction from 'vue-test-actions' |
| 26 | + |
| 27 | +testAction(action, payload, expectedCommits, expectedDispatchs, store) |
| 28 | +``` |
| 29 | + |
| 30 | +# βοΈ Params |
| 31 | +| Params | Type | Description | |
| 32 | +| ----------------- |:--------------| ----------------------------------| |
| 33 | +| action | Function | Action function | |
| 34 | +| payload | Any | Parameter send to action function | |
| 35 | +| expected commits | Array | Array of commit expectation | |
| 36 | +| expected dispatchs| Array | Array of dispatchs expectation | |
| 37 | +| store | Object | Object for mock store data such as `state` or `getter` | |
| 38 | + |
| 39 | + |
| 40 | +# π Example |
| 41 | + |
| 42 | +```javascript |
| 43 | +// user/actions.js |
| 44 | + |
| 45 | +import userSerive from '../myUserService' |
| 46 | + |
| 47 | +export async function getUser ({ commit, dispatch }, userId) { |
| 48 | + commit('setLoading', true) |
| 49 | + try { |
| 50 | + const { data } = await userSerive.fetchUser(userId) |
| 51 | + commit('setUser', data) |
| 52 | + dispatch('getUserPermission', data.id) |
| 53 | + } catch (e) { |
| 54 | + commit('setNotificationError', true) |
| 55 | + } |
| 56 | + commit('setLoading', false) |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +```javascript |
| 61 | +// user.spec.js |
| 62 | + |
| 63 | +import testAction from 'vue-test-actions' |
| 64 | +import * as actions from '~/store/user/actions' |
| 65 | +import userSerive from '~/services/myUserService' |
| 66 | + |
| 67 | +describe('getUser', () => { |
| 68 | + test('If success', () => { |
| 69 | + const payload = 1 |
| 70 | + userSerive.fetchUser = jest.fn().mockReturnValue({ data: { id: 1 } }) |
| 71 | + const expectedCommits = [ |
| 72 | + { type: 'setLoading', payload: true }, |
| 73 | + { type: 'setUser', payload: { id: 1 } }, |
| 74 | + { type: 'setLoading', payload: false } |
| 75 | + ] |
| 76 | + const expectedDispatchs = [ |
| 77 | + { type: 'getUserPermission', payload: 1 } |
| 78 | + ] |
| 79 | + testAction(actions.getUser, payload, expectedCommits, expectedDispatchs) |
| 80 | + }) |
| 81 | +}) |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +# π€ Contributing |
| 86 | +1. Fork this repository. |
| 87 | +2. Create new branch with feature name. |
| 88 | +3. Run `npm install` and `npm run dev`. |
| 89 | +4. Create your feature. |
| 90 | +5. Commit and set commit message with feature name. |
| 91 | +6. Push your code to your fork repository. |
| 92 | +7. Create pull request. π |
| 93 | + |
| 94 | +# βοΈ Support |
| 95 | + |
| 96 | +``` |
| 97 | +If you like this project, You can support me with starring β this repository. |
| 98 | +``` |
| 99 | + |
| 100 | +# π License |
| 101 | + |
| 102 | +[MIT](LICENSE) |
| 103 | + |
| 104 | +Developed with β€οΈ and βοΈ |
0 commit comments