2018-11-18 11:00:15 +05:30
|
|
|
const noop = () => {};
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
/**
|
2018-11-18 11:00:15 +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
|
|
|
*
|
2018-11-18 11:00:15 +05:30
|
|
|
* @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-05-09 12:01:36 +05:30
|
|
|
* @example
|
|
|
|
* testAction(
|
|
|
|
* actions.actionName, // action
|
2018-11-18 11:00:15 +05:30
|
|
|
* { }, // mocked payload
|
|
|
|
* state, //state
|
|
|
|
* // expected mutations
|
2018-05-09 12:01:36 +05:30
|
|
|
* [
|
|
|
|
* { type: types.MUTATION}
|
2018-11-18 11:00:15 +05:30
|
|
|
* { type: types.MUTATION_1, payload: jasmine.any(Number)}
|
|
|
|
* ],
|
|
|
|
* // expected actions
|
2018-05-09 12:01:36 +05:30
|
|
|
* [
|
2018-11-18 11:00:15 +05:30
|
|
|
* { type: 'actionName', payload: {param: 'foobar'}},
|
|
|
|
* { type: 'actionName1'}
|
|
|
|
* ]
|
2018-05-09 12:01:36 +05:30
|
|
|
* done,
|
|
|
|
* );
|
2018-11-18 11:00:15 +05:30
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* testAction(
|
|
|
|
* actions.actionName, // action
|
|
|
|
* { }, // mocked payload
|
|
|
|
* state, //state
|
|
|
|
* [ { type: types.MUTATION} ], // expected mutations
|
|
|
|
* [], // expected actions
|
|
|
|
* ).then(done)
|
|
|
|
* .catch(done.fail);
|
2018-03-17 18:26:18 +05:30
|
|
|
*/
|
2018-11-18 11:00:15 +05:30
|
|
|
export default (
|
|
|
|
action,
|
|
|
|
payload,
|
|
|
|
state,
|
|
|
|
expectedMutations = [],
|
|
|
|
expectedActions = [],
|
|
|
|
done = noop,
|
|
|
|
) => {
|
|
|
|
const mutations = [];
|
|
|
|
const actions = [];
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
// mock commit
|
2018-05-09 12:01:36 +05:30
|
|
|
const commit = (type, mutationPayload) => {
|
2018-11-18 11:00:15 +05:30
|
|
|
const mutation = { type };
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (typeof mutationPayload !== 'undefined') {
|
|
|
|
mutation.payload = mutationPayload;
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
mutations.push(mutation);
|
2018-05-09 12:01:36 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
// mock dispatch
|
|
|
|
const dispatch = (type, actionPayload) => {
|
2018-11-18 11:00:15 +05:30
|
|
|
const dispatchedAction = { type };
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (typeof actionPayload !== 'undefined') {
|
|
|
|
dispatchedAction.payload = actionPayload;
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
actions.push(dispatchedAction);
|
2018-03-17 18:26:18 +05:30
|
|
|
};
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
const validateResults = () => {
|
|
|
|
expect({
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
}).toEqual({
|
|
|
|
mutations: expectedMutations,
|
|
|
|
actions: expectedActions,
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
done();
|
2018-11-18 11:00:15 +05:30
|
|
|
};
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
const result = action(
|
|
|
|
{ commit, state, dispatch, rootState: state, rootGetters: state, getters: state },
|
|
|
|
payload,
|
|
|
|
);
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
return new Promise(setImmediate)
|
2018-11-18 11:00:15 +05:30
|
|
|
.then(() => result)
|
|
|
|
.catch(error => {
|
|
|
|
validateResults();
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
validateResults();
|
|
|
|
return data;
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
};
|