debian-mirror-gitlab/spec/frontend/__helpers__/vuex_action_helper_spec.js

175 lines
5.1 KiB
JavaScript
Raw Normal View History

2019-09-30 21:07:59 +05:30
import MockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'helpers/test_constants';
import axios from '~/lib/utils/axios_utils';
2021-02-22 17:27:13 +05:30
import testActionFn from './vuex_action_helper';
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
const testActionFnWithOptionsArg = (...args) => {
const [action, payload, state, expectedMutations, expectedActions, done] = args;
return testActionFn({ action, payload, state, expectedMutations, expectedActions, done });
};
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
describe.each([testActionFn, testActionFnWithOptionsArg])(
'VueX test helper (testAction)',
2021-03-08 18:12:59 +05:30
(testAction) => {
2021-02-22 17:27:13 +05:30
let originalExpect;
let assertion;
let mock;
const noop = () => {};
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
beforeEach(() => {
mock = new MockAdapter(axios);
/**
* In order to test the helper properly, we need to overwrite the Jest
* `expect` helper. We test that the testAction helper properly passes the
* dispatched actions/committed mutations to the Jest helper.
*/
originalExpect = expect;
assertion = null;
2021-03-08 18:12:59 +05:30
global.expect = (actual) => ({
2021-02-22 17:27:13 +05:30
toEqual: () => {
originalExpect(actual).toEqual(assertion);
},
});
});
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
afterEach(() => {
mock.restore();
global.expect = originalExpect;
});
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
it('properly passes state and payload to action', () => {
const exampleState = { FOO: 12, BAR: 3 };
const examplePayload = { BAZ: 73, BIZ: 55 };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
const action = ({ state }, payload) => {
originalExpect(state).toEqual(exampleState);
originalExpect(payload).toEqual(examplePayload);
2019-09-30 21:07:59 +05:30
};
2021-02-22 17:27:13 +05:30
assertion = { mutations: [], actions: [] };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
testAction(action, examplePayload, exampleState);
2019-09-30 21:07:59 +05:30
});
2021-02-22 17:27:13 +05:30
describe('given a sync action', () => {
it('mocks committing mutations', () => {
const action = ({ commit }) => {
commit('MUTATION');
};
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
assertion = { mutations: [{ type: 'MUTATION' }], actions: [] };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
testAction(action, null, {}, assertion.mutations, assertion.actions, noop);
});
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
it('mocks dispatching actions', () => {
const action = ({ dispatch }) => {
dispatch('ACTION');
};
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
assertion = { actions: [{ type: 'ACTION' }], mutations: [] };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
testAction(action, null, {}, assertion.mutations, assertion.actions, noop);
});
2019-09-30 21:07:59 +05:30
2021-03-08 18:12:59 +05:30
it('works with done callback once finished', (done) => {
2021-02-22 17:27:13 +05:30
assertion = { mutations: [], actions: [] };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
testAction(noop, null, {}, assertion.mutations, assertion.actions, done);
});
2021-03-08 18:12:59 +05:30
it('returns a promise', (done) => {
2021-02-22 17:27:13 +05:30
assertion = { mutations: [], actions: [] };
testAction(noop, null, {}, assertion.mutations, assertion.actions)
.then(done)
.catch(done.fail);
});
2019-09-30 21:07:59 +05:30
});
2021-02-22 17:27:13 +05:30
describe('given an async action (returning a promise)', () => {
let lastError;
const data = { FOO: 'BAR' };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
const asyncAction = ({ commit, dispatch }) => {
dispatch('ACTION');
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
return axios
.get(TEST_HOST)
2021-03-08 18:12:59 +05:30
.catch((error) => {
2021-02-22 17:27:13 +05:30
commit('ERROR');
lastError = error;
throw error;
})
.then(() => {
commit('SUCCESS');
return data;
});
};
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
beforeEach(() => {
lastError = null;
});
2019-09-30 21:07:59 +05:30
2021-03-08 18:12:59 +05:30
it('works with done callback once finished', (done) => {
2021-02-22 17:27:13 +05:30
mock.onGet(TEST_HOST).replyOnce(200, 42);
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
assertion = { mutations: [{ type: 'SUCCESS' }], actions: [{ type: 'ACTION' }] };
testAction(asyncAction, null, {}, assertion.mutations, assertion.actions, done);
});
2021-03-08 18:12:59 +05:30
it('returns original data of successful promise while checking actions/mutations', (done) => {
2021-02-22 17:27:13 +05:30
mock.onGet(TEST_HOST).replyOnce(200, 42);
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
assertion = { mutations: [{ type: 'SUCCESS' }], actions: [{ type: 'ACTION' }] };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
testAction(asyncAction, null, {}, assertion.mutations, assertion.actions)
2021-03-08 18:12:59 +05:30
.then((res) => {
2021-02-22 17:27:13 +05:30
originalExpect(res).toEqual(data);
done();
})
.catch(done.fail);
});
2019-09-30 21:07:59 +05:30
2021-03-08 18:12:59 +05:30
it('returns original error of rejected promise while checking actions/mutations', (done) => {
2021-02-22 17:27:13 +05:30
mock.onGet(TEST_HOST).replyOnce(500, '');
assertion = { mutations: [{ type: 'ERROR' }], actions: [{ type: 'ACTION' }] };
testAction(asyncAction, null, {}, assertion.mutations, assertion.actions)
.then(done.fail)
2021-03-08 18:12:59 +05:30
.catch((error) => {
2021-02-22 17:27:13 +05:30
originalExpect(error).toBe(lastError);
done();
});
});
2019-09-30 21:07:59 +05:30
});
2021-03-08 18:12:59 +05:30
it('works with async actions not returning promises', (done) => {
2021-02-22 17:27:13 +05:30
const data = { FOO: 'BAR' };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
const asyncAction = ({ commit, dispatch }) => {
dispatch('ACTION');
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
axios
.get(TEST_HOST)
.then(() => {
commit('SUCCESS');
return data;
})
2021-03-08 18:12:59 +05:30
.catch((error) => {
2021-02-22 17:27:13 +05:30
commit('ERROR');
throw error;
});
};
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
mock.onGet(TEST_HOST).replyOnce(200, 42);
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
assertion = { mutations: [{ type: 'SUCCESS' }], actions: [{ type: 'ACTION' }] };
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
testAction(asyncAction, null, {}, assertion.mutations, assertion.actions, done);
});
},
);