2019-03-28 19:07:40 +00:00
|
|
|
const noop = () => {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper for testing action with expected mutations inspired in
|
|
|
|
* https://vuex.vuejs.org/en/testing.html
|
|
|
|
*
|
2020-12-03 18:10:10 +00:00
|
|
|
* @param {(Function|Object)} action to be tested, or object of named parameters
|
2019-03-28 19:07:40 +00:00
|
|
|
* @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}
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* testAction(
|
|
|
|
* actions.actionName, // action
|
|
|
|
* { }, // mocked payload
|
|
|
|
* state, //state
|
|
|
|
* // expected mutations
|
|
|
|
* [
|
|
|
|
* { type: types.MUTATION}
|
2019-06-25 15:44:20 +00:00
|
|
|
* { type: types.MUTATION_1, payload: expect.any(Number)}
|
2019-03-28 19:07:40 +00:00
|
|
|
* ],
|
|
|
|
* // expected actions
|
|
|
|
* [
|
|
|
|
* { type: 'actionName', payload: {param: 'foobar'}},
|
|
|
|
* { type: 'actionName1'}
|
|
|
|
* ]
|
|
|
|
* done,
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* testAction(
|
|
|
|
* actions.actionName, // action
|
|
|
|
* { }, // mocked payload
|
|
|
|
* state, //state
|
|
|
|
* [ { type: types.MUTATION} ], // expected mutations
|
|
|
|
* [], // expected actions
|
|
|
|
* ).then(done)
|
|
|
|
* .catch(done.fail);
|
2020-12-03 18:10:10 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* await testAction({
|
|
|
|
* action: actions.actionName,
|
|
|
|
* payload: { deleteListId: 1 },
|
|
|
|
* state: { lists: [1, 2, 3] },
|
|
|
|
* expectedMutations: [ { type: types.MUTATION} ],
|
|
|
|
* expectedActions: [],
|
|
|
|
* })
|
2019-03-28 19:07:40 +00:00
|
|
|
*/
|
|
|
|
export default (
|
2020-12-03 18:10:10 +00:00
|
|
|
actionArg,
|
|
|
|
payloadArg,
|
|
|
|
stateArg,
|
|
|
|
expectedMutationsArg = [],
|
|
|
|
expectedActionsArg = [],
|
|
|
|
doneArg = noop,
|
2019-03-28 19:07:40 +00:00
|
|
|
) => {
|
2020-12-03 18:10:10 +00:00
|
|
|
let action = actionArg;
|
|
|
|
let payload = payloadArg;
|
|
|
|
let state = stateArg;
|
|
|
|
let expectedMutations = expectedMutationsArg;
|
|
|
|
let expectedActions = expectedActionsArg;
|
|
|
|
let done = doneArg;
|
|
|
|
|
|
|
|
if (typeof actionArg !== 'function') {
|
|
|
|
({
|
|
|
|
action,
|
|
|
|
payload,
|
|
|
|
state,
|
|
|
|
expectedMutations = [],
|
|
|
|
expectedActions = [],
|
|
|
|
done = noop,
|
|
|
|
} = actionArg);
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:07:40 +00:00
|
|
|
const mutations = [];
|
|
|
|
const actions = [];
|
|
|
|
|
|
|
|
// mock commit
|
|
|
|
const commit = (type, mutationPayload) => {
|
|
|
|
const mutation = { type };
|
|
|
|
|
|
|
|
if (typeof mutationPayload !== 'undefined') {
|
|
|
|
mutation.payload = mutationPayload;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutations.push(mutation);
|
|
|
|
};
|
|
|
|
|
|
|
|
// mock dispatch
|
|
|
|
const dispatch = (type, actionPayload) => {
|
|
|
|
const dispatchedAction = { type };
|
|
|
|
|
|
|
|
if (typeof actionPayload !== 'undefined') {
|
|
|
|
dispatchedAction.payload = actionPayload;
|
|
|
|
}
|
|
|
|
|
|
|
|
actions.push(dispatchedAction);
|
|
|
|
};
|
|
|
|
|
|
|
|
const validateResults = () => {
|
|
|
|
expect({
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
}).toEqual({
|
|
|
|
mutations: expectedMutations,
|
|
|
|
actions: expectedActions,
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = action(
|
|
|
|
{ commit, state, dispatch, rootState: state, rootGetters: state, getters: state },
|
|
|
|
payload,
|
|
|
|
);
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
return (result || new Promise((resolve) => setImmediate(resolve)))
|
|
|
|
.catch((error) => {
|
2019-03-28 19:07:40 +00:00
|
|
|
validateResults();
|
|
|
|
throw error;
|
|
|
|
})
|
2020-12-23 21:10:24 +00:00
|
|
|
.then((data) => {
|
2019-03-28 19:07:40 +00:00
|
|
|
validateResults();
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
};
|