diff options
author | Filipa Lacerda <filipa@gitlab.com> | 2018-03-29 13:14:18 +0300 |
---|---|---|
committer | Filipa Lacerda <filipa@gitlab.com> | 2018-03-29 13:14:18 +0300 |
commit | 470b8ca10ade4af96eb74f908c01d51e227accb3 (patch) | |
tree | 58fed065a33e95e8c540e501372d7b3d74355cc7 /spec/javascripts/helpers | |
parent | e26d20311cf8ef1a20a8973572e781bab76a0a5b (diff) |
Add dispatch mock to the test helper
Diffstat (limited to 'spec/javascripts/helpers')
-rw-r--r-- | spec/javascripts/helpers/vuex_action_helper.js | 57 |
1 files changed, 47 insertions, 10 deletions
diff --git a/spec/javascripts/helpers/vuex_action_helper.js b/spec/javascripts/helpers/vuex_action_helper.js index 2b50af36df2..83f29d1b0c2 100644 --- a/spec/javascripts/helpers/vuex_action_helper.js +++ b/spec/javascripts/helpers/vuex_action_helper.js @@ -1,15 +1,30 @@ -/* eslint-disable */ - /** - * helper for testing action with expected mutations + * helper for testing action with expected mutations inspired in * https://vuex.vuejs.org/en/testing.html + * + * @example + * testAction( + * actions.actionName, // action + * { }, // mocked response + * state, // state + * [ + * { type: types.MUTATION} + * { type: types.MUTATION_1, payload: {}} + * ], // mutations + * [ + * { type: 'actionName', payload: {}}, + * { type: 'actionName1', payload: {}} + * ] //actions + * done, + * ); */ -export default (action, payload, state, expectedMutations, done) => { - let count = 0; +export default (action, payload, state, expectedMutations, expectedActions, done) => { + let mutationsCount = 0; + let actionsCount = 0; // mock commit const commit = (type, mutationPayload) => { - const mutation = expectedMutations[count]; + const mutation = expectedMutations[mutationsCount]; expect(mutation.type).toEqual(type); @@ -17,18 +32,40 @@ export default (action, payload, state, expectedMutations, done) => { expect(mutation.payload).toEqual(mutationPayload); } - count++; - if (count >= expectedMutations.length) { + mutationsCount += 1; + if (mutationsCount >= expectedMutations.length) { + done(); + } + }; + + // mock dispatch + const dispatch = (type, actionPayload) => { + const actionExpected = expectedActions[actionsCount]; + + expect(actionExpected.type).toEqual(type); + + if (actionExpected.payload) { + expect(actionExpected.payload).toEqual(actionPayload); + } + + actionsCount += 1; + if (actionsCount >= expectedActions.length) { done(); } }; // call the action with mocked store and arguments - action({ commit, state }, payload); + action({ commit, state, dispatch }, payload); // check if no mutations should have been dispatched if (expectedMutations.length === 0) { - expect(count).toEqual(0); + expect(mutationsCount).toEqual(0); + done(); + } + + // check if no mutations should have been dispatched + if (expectedActions.length === 0) { + expect(actionsCount).toEqual(0); done(); } }; |