Commit 4075c2e6 authored by Fernando's avatar Fernando

Add unit tests

* Add mutations, actions unit tests
parent 3b13c2b5
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import * as Sentry from '@sentry/browser';
import * as types from './mutation_types';
export const setSecurityConfigurationEndpoint = ({ commit }, endpoint) =>
commit(types.SET_SECURITY_CONFIGURATION_ENDPOINT, endpoint);
export const fetchSecurityConfiguration = ({ commit, state }) => {
if (!state.securityConfigurationPath) {
return commit(types.RECEIVE_SECURITY_CONFIGURATION_ERROR);
......@@ -25,4 +23,4 @@ export const fetchSecurityConfiguration = ({ commit, state }) => {
Sentry.captureException(error);
commit(types.RECEIVE_SECURITY_CONFIGURATION_ERROR);
});
};
\ No newline at end of file
};
import state from './state';
import mutations from './mutations';
import * as actions from './actions';
export default {
namespaced: true,
state,
mutations,
actions,
};
......@@ -2,4 +2,4 @@ export const SET_SECURITY_CONFIGURATION_ENDPOINT = 'SET_SECURITY_CONFIGURATION_E
export const REQUEST_SECURITY_CONFIGURATION = 'REQUEST_SECURITY_CONFIGURATION';
export const RECEIVE_SECURITY_CONFIGURATION_SUCCESS = 'RECEIVE_SECURITY_CONFIGURATION_SUCCESS';
export const RECEIVE_SECURITY_CONFIGURATION_ERROR = 'RECEIVE_SECURITY_CONFIGURATION_ERROR';
\ No newline at end of file
export const RECEIVE_SECURITY_CONFIGURATION_ERROR = 'RECEIVE_SECURITY_CONFIGURATION_ERROR';
......@@ -9,7 +9,7 @@ export default {
},
[types.RECEIVE_SECURITY_CONFIGURATION_SUCCESS](state, payload) {
state.isLoading = false;
state.pipelineJobs = payload;
state.configuration = payload;
},
[types.RECEIVE_SECURITY_CONFIGURATION_ERROR](state) {
state.isLoading = false;
......
......@@ -3,5 +3,5 @@ export default () => ({
initialized: false,
isLoading: false,
errorLoading: false,
configuration: [],
configuration: {},
});
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import { TEST_HOST } from 'spec/test_constants';
import testAction from 'helpers/vuex_action_helper';
import createState from 'ee/security_configuration/modules/configuration/state';
import * as types from 'ee/security_configuration/modules/configuration/mutation_types';
import * as actions from 'ee/security_configuration/modules/configuration/actions';
describe('security configuration module actions', () => {
let state;
beforeEach(() => {
state = createState();
});
describe('setSecurityConfigurationEndpoint', () => {
const securityConfigurationPath = 123;
it('should commit the SET_SECURITY_CONFIGURATION_ENDPOINT mutation', () => {
testAction(
actions.setSecurityConfigurationEndpoint,
securityConfigurationPath,
state,
[
{
type: types.SET_SECURITY_CONFIGURATION_ENDPOINT,
payload: securityConfigurationPath,
},
],
[],
);
});
});
describe('fetchSecurityConfiguration', () => {
let mock;
const configuration = {};
beforeEach(() => {
state.securityConfigurationPath = `${TEST_HOST}/-/security/configuration.json`;
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('on success', () => {
beforeEach(() => {
mock.onGet(state.securityConfigurationPath).replyOnce(200, configuration);
});
it('should commit the request and success mutations', () => {
testAction(
actions.fetchSecurityConfiguration,
{},
state,
[
{ type: types.REQUEST_SECURITY_CONFIGURATION },
{
type: types.RECEIVE_SECURITY_CONFIGURATION_SUCCESS,
payload: configuration,
},
],
[],
);
});
});
describe('without securityConfigurationPath set', () => {
beforeEach(() => {
mock.onGet(state.securityConfigurationPath).replyOnce(200, configuration);
});
it('should commit RECEIVE_SECURITY_CONFIGURATION_ERROR mutation', () => {
state.securityConfigurationPath = '';
testAction(
actions.fetchSecurityConfiguration,
{},
state,
[
{
type: types.RECEIVE_SECURITY_CONFIGURATION_ERROR,
},
],
[],
);
});
});
describe('with server error', () => {
beforeEach(() => {
mock.onGet(state.securityConfigurationPath).replyOnce(404);
});
it('should commit REQUEST_SECURITY_CONFIGURATION and RECEIVE_SECURITY_CONFIGURATION_ERRORmutation', () => {
testAction(
actions.fetchSecurityConfiguration,
{},
state,
[
{ type: types.REQUEST_SECURITY_CONFIGURATION },
{
type: types.RECEIVE_SECURITY_CONFIGURATION_ERROR,
},
],
[],
);
});
});
});
});
import * as types from 'ee/security_configuration/modules/configuration/mutation_types';
import mutations from 'ee/security_configuration/modules/configuration/mutations';
describe('security configuration module mutations', () => {
let state;
beforeEach(() => {
state = {};
});
describe('SET_SECURITY_CONFIGURATION_ENDPOINT', () => {
const securityConfigurationPath = 123;
it(`should set the securityConfigurationPath to ${securityConfigurationPath}`, () => {
mutations[types.SET_SECURITY_CONFIGURATION_ENDPOINT](state, securityConfigurationPath);
expect(state.securityConfigurationPath).toBe(securityConfigurationPath);
});
});
describe('REQUEST_SECURITY_CONFIGURATION', () => {
it('should set the isLoading to true', () => {
mutations[types.REQUEST_SECURITY_CONFIGURATION](state);
expect(state.isLoading).toBe(true);
});
});
describe('RECEIVE_SECURITY_CONFIGURATION_SUCCESS', () => {
it('should set the isLoading to false and configuration to the response object', () => {
const configuration = {};
mutations[types.RECEIVE_SECURITY_CONFIGURATION_SUCCESS](state, configuration);
expect(state.isLoading).toBe(false);
expect(state.configuration).toBe(configuration);
});
});
describe('RECEIVE_SECURITY_CONFIGURATION_ERROR', () => {
it('should set the isLoading to false', () => {
mutations[types.RECEIVE_SECURITY_CONFIGURATION_ERROR](state);
expect(state.isLoading).toBe(false);
});
});
});
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