Commit 2e9a1608 authored by Simon Knox's avatar Simon Knox Committed by Andrew Fontaine

Allow passing named parameters to testAction function

This might need a better docstring update, could also
be done in a nicer way
parent 72fdd799
...@@ -4,7 +4,7 @@ const noop = () => {}; ...@@ -4,7 +4,7 @@ const noop = () => {};
* Helper for testing action with expected mutations inspired in * Helper for testing action with expected mutations inspired in
* https://vuex.vuejs.org/en/testing.html * https://vuex.vuejs.org/en/testing.html
* *
* @param {Function} action to be tested * @param {(Function|Object)} action to be tested, or object of named parameters
* @param {Object} payload will be provided to the action * @param {Object} payload will be provided to the action
* @param {Object} state 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} [expectedMutations=[]] mutations expected to be committed
...@@ -39,15 +39,42 @@ const noop = () => {}; ...@@ -39,15 +39,42 @@ const noop = () => {};
* [], // expected actions * [], // expected actions
* ).then(done) * ).then(done)
* .catch(done.fail); * .catch(done.fail);
*
* @example
* await testAction({
* action: actions.actionName,
* payload: { deleteListId: 1 },
* state: { lists: [1, 2, 3] },
* expectedMutations: [ { type: types.MUTATION} ],
* expectedActions: [],
* })
*/ */
export default ( export default (
actionArg,
payloadArg,
stateArg,
expectedMutationsArg = [],
expectedActionsArg = [],
doneArg = noop,
) => {
let action = actionArg;
let payload = payloadArg;
let state = stateArg;
let expectedMutations = expectedMutationsArg;
let expectedActions = expectedActionsArg;
let done = doneArg;
if (typeof actionArg !== 'function') {
({
action, action,
payload, payload,
state, state,
expectedMutations = [], expectedMutations = [],
expectedActions = [], expectedActions = [],
done = noop, done = noop,
) => { } = actionArg);
}
const mutations = []; const mutations = [];
const actions = []; const actions = [];
......
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'helpers/test_constants'; import { TEST_HOST } from 'helpers/test_constants';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import testAction from './vuex_action_helper'; import testActionFn from './vuex_action_helper';
describe('VueX test helper (testAction)', () => { const testActionFnWithOptionsArg = (...args) => {
const [action, payload, state, expectedMutations, expectedActions, done] = args;
return testActionFn({ action, payload, state, expectedMutations, expectedActions, done });
};
describe.each([testActionFn, testActionFnWithOptionsArg])(
'VueX test helper (testAction)',
testAction => {
let originalExpect; let originalExpect;
let assertion; let assertion;
let mock; let mock;
...@@ -163,4 +170,5 @@ describe('VueX test helper (testAction)', () => { ...@@ -163,4 +170,5 @@ describe('VueX test helper (testAction)', () => {
testAction(asyncAction, null, {}, assertion.mutations, assertion.actions, done); testAction(asyncAction, null, {}, assertion.mutations, assertion.actions, done);
}); });
}); },
);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment