2022-08-27 11:52:29 +05:30
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
import { setImmediate } from 'timers';
|
|
|
|
|
|
|
|
/** Helper for testing action with expected mutations inspired in
|
2019-07-07 11:18:12 +05:30
|
|
|
* https://vuex.vuejs.org/en/testing.html
|
|
|
|
*
|
2021-02-22 17:27:13 +05:30
|
|
|
* @param {(Function|Object)} action to be tested, or object of named parameters
|
2019-07-07 11:18:12 +05:30
|
|
|
* @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
|
|
|
|
* @return {Promise}
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* testAction(
|
|
|
|
* actions.actionName, // action
|
|
|
|
* { }, // mocked payload
|
|
|
|
* state, //state
|
|
|
|
* // expected mutations
|
|
|
|
* [
|
|
|
|
* { type: types.MUTATION}
|
2019-09-30 21:07:59 +05:30
|
|
|
* { type: types.MUTATION_1, payload: expect.any(Number)}
|
2019-07-07 11:18:12 +05:30
|
|
|
* ],
|
|
|
|
* // expected actions
|
|
|
|
* [
|
|
|
|
* { type: 'actionName', payload: {param: 'foobar'}},
|
|
|
|
* { type: 'actionName1'}
|
|
|
|
* ]
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @example
|
2021-02-22 17:27:13 +05:30
|
|
|
* await testAction({
|
|
|
|
* action: actions.actionName,
|
|
|
|
* payload: { deleteListId: 1 },
|
|
|
|
* state: { lists: [1, 2, 3] },
|
|
|
|
* expectedMutations: [ { type: types.MUTATION} ],
|
|
|
|
* expectedActions: [],
|
|
|
|
* })
|
2019-07-07 11:18:12 +05:30
|
|
|
*/
|
2022-06-21 17:19:12 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
export default (
|
2021-02-22 17:27:13 +05:30
|
|
|
actionArg,
|
|
|
|
payloadArg,
|
|
|
|
stateArg,
|
|
|
|
expectedMutationsArg = [],
|
|
|
|
expectedActionsArg = [],
|
2019-07-07 11:18:12 +05:30
|
|
|
) => {
|
2021-02-22 17:27:13 +05:30
|
|
|
let action = actionArg;
|
|
|
|
let payload = payloadArg;
|
|
|
|
let state = stateArg;
|
|
|
|
let expectedMutations = expectedMutationsArg;
|
|
|
|
let expectedActions = expectedActionsArg;
|
|
|
|
|
|
|
|
if (typeof actionArg !== 'function') {
|
2022-07-16 23:28:13 +05:30
|
|
|
({ action, payload, state, expectedMutations = [], expectedActions = [] } = actionArg);
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
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);
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
return Promise.resolve();
|
2019-07-07 11:18:12 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const validateResults = () => {
|
|
|
|
expect({
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
}).toEqual({
|
|
|
|
mutations: expectedMutations,
|
|
|
|
actions: expectedActions,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = action(
|
|
|
|
{ commit, state, dispatch, rootState: state, rootGetters: state, getters: state },
|
|
|
|
payload,
|
|
|
|
);
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
return (
|
|
|
|
result ||
|
|
|
|
new Promise((resolve) => {
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
setImmediate(resolve);
|
|
|
|
})
|
|
|
|
)
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((error) => {
|
2019-07-07 11:18:12 +05:30
|
|
|
validateResults();
|
|
|
|
throw error;
|
|
|
|
})
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((data) => {
|
2019-07-07 11:18:12 +05:30
|
|
|
validateResults();
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
};
|