gitlab-org--gitlab-foss/spec/javascripts/helpers/vuex_action_helper.js

102 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-07-12 10:32:13 -04:00
const noop = () => {};
2017-08-11 15:58:20 -04:00
/**
2018-07-12 10:32:13 -04:00
* Helper for testing action with expected mutations inspired in
2017-08-11 15:58:20 -04:00
* https://vuex.vuejs.org/en/testing.html
2018-03-29 06:14:18 -04:00
*
2018-07-12 10:32:13 -04:00
* @param {Function} action to be tested
* @param {Object} payload will be provided to the action
* @param {Object} state will be provided to the action
* @param {Array} [expectedMutations=[]] mutations expected to be committed
* @param {Array} [expectedActions=[]] actions expected to be dispatched
* @param {Function} [done=noop] to be executed after the tests
* @return {Promise}
*
2018-03-29 06:14:18 -04:00
* @example
* testAction(
* actions.actionName, // action
2018-07-12 10:32:13 -04:00
* { }, // mocked payload
* state, //state
* // expected mutations
2018-03-29 06:14:18 -04:00
* [
* { type: types.MUTATION}
2018-07-12 10:32:13 -04:00
* { type: types.MUTATION_1, payload: jasmine.any(Number)}
* ],
* // expected actions
2018-03-29 06:14:18 -04:00
* [
2018-07-12 10:32:13 -04:00
* { type: 'actionName', payload: {param: 'foobar'}},
* { type: 'actionName1'}
* ]
2018-03-29 06:14:18 -04:00
* done,
* );
2018-07-12 10:32:13 -04:00
*
* @example
* testAction(
* actions.actionName, // action
* { }, // mocked payload
* state, //state
* [ { type: types.MUTATION} ], // expected mutations
* [], // expected actions
* ).then(done)
* .catch(done.fail);
2017-08-11 15:58:20 -04:00
*/
2018-07-12 10:32:13 -04:00
export default (
action,
payload,
state,
expectedMutations = [],
expectedActions = [],
done = noop,
) => {
const mutations = [];
const actions = [];
2017-08-11 15:58:20 -04:00
// mock commit
const commit = (type, mutationPayload) => {
2018-07-12 10:32:13 -04:00
const mutation = { type };
2018-07-12 10:32:13 -04:00
if (typeof mutationPayload !== 'undefined') {
mutation.payload = mutationPayload;
2017-08-11 15:58:20 -04:00
}
2018-07-12 10:32:13 -04:00
mutations.push(mutation);
2018-03-29 06:14:18 -04:00
};
// mock dispatch
const dispatch = (type, actionPayload) => {
2018-07-12 10:32:13 -04:00
const dispatchedAction = { type };
2018-03-29 06:14:18 -04:00
2018-07-12 10:32:13 -04:00
if (typeof actionPayload !== 'undefined') {
dispatchedAction.payload = actionPayload;
2018-03-29 06:14:18 -04:00
}
2018-07-12 10:32:13 -04:00
actions.push(dispatchedAction);
2017-08-11 15:58:20 -04:00
};
2018-07-12 10:32:13 -04:00
const validateResults = () => {
expect({
mutations,
actions,
}).toEqual({
mutations: expectedMutations,
actions: expectedActions,
});
2018-03-29 06:14:18 -04:00
done();
2018-07-12 10:32:13 -04:00
};
2018-03-29 06:14:18 -04:00
const result = action({ commit, state, dispatch, rootState: state }, payload);
return new Promise(resolve => {
setImmediate(resolve);
2018-07-12 10:32:13 -04:00
})
.then(() => result)
2018-07-12 10:32:13 -04:00
.catch(error => {
validateResults();
throw error;
})
.then(data => {
validateResults();
return data;
});
2017-08-11 15:58:20 -04:00
};