debian-mirror-gitlab/spec/javascripts/helpers/vuex_action_helper.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
/**
2018-05-09 12:01:36 +05:30
* helper for testing action with expected mutations inspired in
2018-03-17 18:26:18 +05:30
* https://vuex.vuejs.org/en/testing.html
2018-05-09 12:01:36 +05:30
*
* @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,
* );
2018-03-17 18:26:18 +05:30
*/
2018-05-09 12:01:36 +05:30
export default (action, payload, state, expectedMutations, expectedActions, done) => {
let mutationsCount = 0;
let actionsCount = 0;
2018-03-17 18:26:18 +05:30
// mock commit
2018-05-09 12:01:36 +05:30
const commit = (type, mutationPayload) => {
const mutation = expectedMutations[mutationsCount];
expect(mutation.type).toEqual(type);
if (mutation.payload) {
expect(mutation.payload).toEqual(mutationPayload);
2018-03-17 18:26:18 +05:30
}
2018-05-09 12:01:36 +05:30
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) {
2018-03-17 18:26:18 +05:30
done();
}
};
// call the action with mocked store and arguments
2018-05-09 12:01:36 +05:30
action({ commit, state, dispatch }, payload);
2018-03-17 18:26:18 +05:30
// check if no mutations should have been dispatched
if (expectedMutations.length === 0) {
2018-05-09 12:01:36 +05:30
expect(mutationsCount).toEqual(0);
done();
}
// check if no mutations should have been dispatched
if (expectedActions.length === 0) {
expect(actionsCount).toEqual(0);
2018-03-17 18:26:18 +05:30
done();
}
};