2018-07-12 14:32:13 +00:00
|
|
|
const noop = () => {};
|
|
|
|
|
2017-08-11 19:58:20 +00:00
|
|
|
/**
|
2018-07-12 14:32:13 +00:00
|
|
|
* Helper for testing action with expected mutations inspired in
|
2017-08-11 19:58:20 +00:00
|
|
|
* https://vuex.vuejs.org/en/testing.html
|
2018-03-29 10:14:18 +00:00
|
|
|
*
|
2018-07-12 14:32:13 +00: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 10:14:18 +00:00
|
|
|
* @example
|
|
|
|
* testAction(
|
|
|
|
* actions.actionName, // action
|
2018-07-12 14:32:13 +00:00
|
|
|
* { }, // mocked payload
|
|
|
|
* state, //state
|
|
|
|
* // expected mutations
|
2018-03-29 10:14:18 +00:00
|
|
|
* [
|
|
|
|
* { type: types.MUTATION}
|
2018-07-12 14:32:13 +00:00
|
|
|
* { type: types.MUTATION_1, payload: jasmine.any(Number)}
|
|
|
|
* ],
|
|
|
|
* // expected actions
|
2018-03-29 10:14:18 +00:00
|
|
|
* [
|
2018-07-12 14:32:13 +00:00
|
|
|
* { type: 'actionName', payload: {param: 'foobar'}},
|
|
|
|
* { type: 'actionName1'}
|
|
|
|
* ]
|
2018-03-29 10:14:18 +00:00
|
|
|
* done,
|
|
|
|
* );
|
2018-07-12 14:32:13 +00: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 19:58:20 +00:00
|
|
|
*/
|
2018-07-12 14:32:13 +00:00
|
|
|
export default (
|
|
|
|
action,
|
|
|
|
payload,
|
|
|
|
state,
|
|
|
|
expectedMutations = [],
|
|
|
|
expectedActions = [],
|
|
|
|
done = noop,
|
|
|
|
) => {
|
|
|
|
const mutations = [];
|
|
|
|
const actions = [];
|
2017-08-11 19:58:20 +00:00
|
|
|
|
|
|
|
// mock commit
|
2018-03-29 09:43:32 +00:00
|
|
|
const commit = (type, mutationPayload) => {
|
2018-07-12 14:32:13 +00:00
|
|
|
const mutation = { type };
|
2018-03-29 09:43:32 +00:00
|
|
|
|
2018-07-12 14:32:13 +00:00
|
|
|
if (typeof mutationPayload !== 'undefined') {
|
|
|
|
mutation.payload = mutationPayload;
|
2017-08-11 19:58:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 14:32:13 +00:00
|
|
|
mutations.push(mutation);
|
2018-03-29 10:14:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// mock dispatch
|
|
|
|
const dispatch = (type, actionPayload) => {
|
2018-07-12 14:32:13 +00:00
|
|
|
const dispatchedAction = { type };
|
2018-03-29 10:14:18 +00:00
|
|
|
|
2018-07-12 14:32:13 +00:00
|
|
|
if (typeof actionPayload !== 'undefined') {
|
|
|
|
dispatchedAction.payload = actionPayload;
|
2018-03-29 10:14:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 14:32:13 +00:00
|
|
|
actions.push(dispatchedAction);
|
2017-08-11 19:58:20 +00:00
|
|
|
};
|
|
|
|
|
2018-07-12 14:32:13 +00:00
|
|
|
const validateResults = () => {
|
|
|
|
expect({
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
}).toEqual({
|
|
|
|
mutations: expectedMutations,
|
|
|
|
actions: expectedActions,
|
|
|
|
});
|
2018-03-29 10:14:18 +00:00
|
|
|
done();
|
2018-07-12 14:32:13 +00:00
|
|
|
};
|
2018-03-29 10:14:18 +00:00
|
|
|
|
2019-02-14 10:07:13 +00:00
|
|
|
const result = action(
|
|
|
|
{ commit, state, dispatch, rootState: state, rootGetters: state, getters: state },
|
|
|
|
payload,
|
|
|
|
);
|
2018-07-19 14:26:29 +00:00
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setImmediate(resolve);
|
2018-07-12 14:32:13 +00:00
|
|
|
})
|
2018-07-19 14:26:29 +00:00
|
|
|
.then(() => result)
|
2018-07-12 14:32:13 +00:00
|
|
|
.catch(error => {
|
|
|
|
validateResults();
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
validateResults();
|
|
|
|
return data;
|
|
|
|
});
|
2017-08-11 19:58:20 +00:00
|
|
|
};
|