debian-mirror-gitlab/spec/frontend/monitoring/store/actions_spec.js

417 lines
11 KiB
JavaScript
Raw Normal View History

2019-09-04 21:01:54 +05:30
import MockAdapter from 'axios-mock-adapter';
2019-12-26 22:10:19 +05:30
import { TEST_HOST } from 'helpers/test_constants';
import testAction from 'helpers/vuex_action_helper';
import axios from '~/lib/utils/axios_utils';
import statusCodes from '~/lib/utils/http_status';
import { backOff } from '~/lib/utils/common_utils';
2019-09-04 21:01:54 +05:30
import store from '~/monitoring/stores';
import * as types from '~/monitoring/stores/mutation_types';
import {
2019-12-26 22:10:19 +05:30
backOffRequest,
2019-09-04 21:01:54 +05:30
fetchDashboard,
receiveMetricsDashboardSuccess,
receiveMetricsDashboardFailure,
fetchDeploymentsData,
fetchEnvironmentsData,
fetchPrometheusMetrics,
fetchPrometheusMetric,
requestMetricsData,
setEndpoints,
setGettingStartedEmptyState,
} from '~/monitoring/stores/actions';
import storeState from '~/monitoring/stores/state';
import {
deploymentData,
environmentData,
metricsDashboardResponse,
metricsGroupsAPIResponse,
2019-09-30 21:07:59 +05:30
dashboardGitResponse,
2019-09-04 21:01:54 +05:30
} from '../mock_data';
2019-12-26 22:10:19 +05:30
jest.mock('~/lib/utils/common_utils');
const resetStore = str => {
str.replaceState({
showEmptyState: true,
emptyState: 'loading',
groups: [],
});
};
const MAX_REQUESTS = 3;
describe('Monitoring store helpers', () => {
2019-09-04 21:01:54 +05:30
let mock;
2019-12-26 22:10:19 +05:30
// Mock underlying `backOff` function to remove in-built delay.
backOff.mockImplementation(
callback =>
new Promise((resolve, reject) => {
const stop = arg => (arg instanceof Error ? reject(arg) : resolve(arg));
const next = () => callback(next, stop);
callback(next, stop);
}),
);
2019-09-04 21:01:54 +05:30
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
2019-12-26 22:10:19 +05:30
describe('backOffRequest', () => {
it('returns immediately when recieving a 200 status code', () => {
mock.onGet(TEST_HOST).reply(200);
return backOffRequest(() => axios.get(TEST_HOST)).then(() => {
expect(mock.history.get.length).toBe(1);
});
});
it(`repeats the network call ${MAX_REQUESTS} times when receiving a 204 response`, done => {
mock.onGet(TEST_HOST).reply(statusCodes.NO_CONTENT, {});
backOffRequest(() => axios.get(TEST_HOST))
.then(done.fail)
.catch(() => {
expect(mock.history.get.length).toBe(MAX_REQUESTS);
done();
});
});
});
});
describe('Monitoring store actions', () => {
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
resetStore(store);
mock.restore();
});
2019-09-04 21:01:54 +05:30
describe('requestMetricsData', () => {
it('sets emptyState to loading', () => {
2019-12-26 22:10:19 +05:30
const commit = jest.fn();
2019-09-04 21:01:54 +05:30
const { state } = store;
2019-12-26 22:10:19 +05:30
requestMetricsData({
state,
commit,
});
2019-09-04 21:01:54 +05:30
expect(commit).toHaveBeenCalledWith(types.REQUEST_METRICS_DATA);
});
});
describe('fetchDeploymentsData', () => {
it('commits RECEIVE_DEPLOYMENTS_DATA_SUCCESS on error', done => {
2019-12-26 22:10:19 +05:30
const dispatch = jest.fn();
2019-09-04 21:01:54 +05:30
const { state } = store;
state.deploymentsEndpoint = '/success';
mock.onGet(state.deploymentsEndpoint).reply(200, {
deployments: deploymentData,
});
2019-12-26 22:10:19 +05:30
fetchDeploymentsData({
state,
dispatch,
})
2019-09-04 21:01:54 +05:30
.then(() => {
expect(dispatch).toHaveBeenCalledWith('receiveDeploymentsDataSuccess', deploymentData);
done();
})
.catch(done.fail);
});
it('commits RECEIVE_DEPLOYMENTS_DATA_FAILURE on error', done => {
2019-12-26 22:10:19 +05:30
const dispatch = jest.fn();
2019-09-04 21:01:54 +05:30
const { state } = store;
state.deploymentsEndpoint = '/error';
mock.onGet(state.deploymentsEndpoint).reply(500);
2019-12-26 22:10:19 +05:30
fetchDeploymentsData({
state,
dispatch,
})
2019-09-04 21:01:54 +05:30
.then(() => {
expect(dispatch).toHaveBeenCalledWith('receiveDeploymentsDataFailure');
done();
})
.catch(done.fail);
});
});
describe('fetchEnvironmentsData', () => {
it('commits RECEIVE_ENVIRONMENTS_DATA_SUCCESS on error', done => {
2019-12-26 22:10:19 +05:30
const dispatch = jest.fn();
2019-09-04 21:01:54 +05:30
const { state } = store;
state.environmentsEndpoint = '/success';
mock.onGet(state.environmentsEndpoint).reply(200, {
environments: environmentData,
});
2019-12-26 22:10:19 +05:30
fetchEnvironmentsData({
state,
dispatch,
})
2019-09-04 21:01:54 +05:30
.then(() => {
expect(dispatch).toHaveBeenCalledWith('receiveEnvironmentsDataSuccess', environmentData);
done();
})
.catch(done.fail);
});
it('commits RECEIVE_ENVIRONMENTS_DATA_FAILURE on error', done => {
2019-12-26 22:10:19 +05:30
const dispatch = jest.fn();
2019-09-04 21:01:54 +05:30
const { state } = store;
state.environmentsEndpoint = '/error';
mock.onGet(state.environmentsEndpoint).reply(500);
2019-12-26 22:10:19 +05:30
fetchEnvironmentsData({
state,
dispatch,
})
2019-09-04 21:01:54 +05:30
.then(() => {
expect(dispatch).toHaveBeenCalledWith('receiveEnvironmentsDataFailure');
done();
})
.catch(done.fail);
});
});
describe('Set endpoints', () => {
let mockedState;
beforeEach(() => {
mockedState = storeState();
});
it('should commit SET_ENDPOINTS mutation', done => {
testAction(
setEndpoints,
{
metricsEndpoint: 'additional_metrics.json',
deploymentsEndpoint: 'deployments.json',
environmentsEndpoint: 'deployments.json',
},
mockedState,
[
{
type: types.SET_ENDPOINTS,
payload: {
metricsEndpoint: 'additional_metrics.json',
deploymentsEndpoint: 'deployments.json',
environmentsEndpoint: 'deployments.json',
},
},
],
[],
done,
);
});
});
describe('Set empty states', () => {
let mockedState;
beforeEach(() => {
mockedState = storeState();
});
it('should commit SET_METRICS_ENDPOINT mutation', done => {
testAction(
setGettingStartedEmptyState,
null,
mockedState,
2019-12-26 22:10:19 +05:30
[
{
type: types.SET_GETTING_STARTED_EMPTY_STATE,
},
],
2019-09-04 21:01:54 +05:30
[],
done,
);
});
});
describe('fetchDashboard', () => {
let dispatch;
let state;
const response = metricsDashboardResponse;
beforeEach(() => {
2019-12-26 22:10:19 +05:30
dispatch = jest.fn();
2019-09-04 21:01:54 +05:30
state = storeState();
state.dashboardEndpoint = '/dashboard';
});
it('dispatches receive and success actions', done => {
const params = {};
mock.onGet(state.dashboardEndpoint).reply(200, response);
2019-12-26 22:10:19 +05:30
fetchDashboard(
{
state,
dispatch,
},
params,
)
2019-09-04 21:01:54 +05:30
.then(() => {
expect(dispatch).toHaveBeenCalledWith('requestMetricsDashboard');
expect(dispatch).toHaveBeenCalledWith('receiveMetricsDashboardSuccess', {
response,
params,
});
done();
})
.catch(done.fail);
});
it('dispatches failure action', done => {
const params = {};
mock.onGet(state.dashboardEndpoint).reply(500);
2019-12-26 22:10:19 +05:30
fetchDashboard(
{
state,
dispatch,
},
params,
)
2019-09-04 21:01:54 +05:30
.then(() => {
expect(dispatch).toHaveBeenCalledWith(
'receiveMetricsDashboardFailure',
new Error('Request failed with status code 500'),
);
done();
})
.catch(done.fail);
});
});
describe('receiveMetricsDashboardSuccess', () => {
let commit;
let dispatch;
2019-09-30 21:07:59 +05:30
let state;
2019-09-04 21:01:54 +05:30
beforeEach(() => {
2019-12-26 22:10:19 +05:30
commit = jest.fn();
dispatch = jest.fn();
2019-09-30 21:07:59 +05:30
state = storeState();
2019-09-04 21:01:54 +05:30
});
it('stores groups ', () => {
const params = {};
const response = metricsDashboardResponse;
2019-12-26 22:10:19 +05:30
receiveMetricsDashboardSuccess(
{
state,
commit,
dispatch,
},
{
response,
params,
},
);
2019-09-04 21:01:54 +05:30
expect(commit).toHaveBeenCalledWith(
types.RECEIVE_METRICS_DATA_SUCCESS,
metricsDashboardResponse.dashboard.panel_groups,
);
expect(dispatch).toHaveBeenCalledWith('fetchPrometheusMetrics', params);
});
2019-09-30 21:07:59 +05:30
it('sets the dashboards loaded from the repository', () => {
const params = {};
const response = metricsDashboardResponse;
response.all_dashboards = dashboardGitResponse;
2019-12-26 22:10:19 +05:30
receiveMetricsDashboardSuccess(
{
state,
commit,
dispatch,
},
{
response,
params,
},
);
2019-09-30 21:07:59 +05:30
expect(commit).toHaveBeenCalledWith(types.SET_ALL_DASHBOARDS, dashboardGitResponse);
});
2019-09-04 21:01:54 +05:30
});
describe('receiveMetricsDashboardFailure', () => {
let commit;
beforeEach(() => {
2019-12-26 22:10:19 +05:30
commit = jest.fn();
2019-09-04 21:01:54 +05:30
});
it('commits failure action', () => {
2019-12-26 22:10:19 +05:30
receiveMetricsDashboardFailure({
commit,
});
2019-09-04 21:01:54 +05:30
expect(commit).toHaveBeenCalledWith(types.RECEIVE_METRICS_DATA_FAILURE, undefined);
});
it('commits failure action with error', () => {
2019-12-26 22:10:19 +05:30
receiveMetricsDashboardFailure(
{
commit,
},
'uh-oh',
);
2019-09-04 21:01:54 +05:30
expect(commit).toHaveBeenCalledWith(types.RECEIVE_METRICS_DATA_FAILURE, 'uh-oh');
});
});
describe('fetchPrometheusMetrics', () => {
let commit;
let dispatch;
beforeEach(() => {
2019-12-26 22:10:19 +05:30
commit = jest.fn();
dispatch = jest.fn();
2019-09-04 21:01:54 +05:30
});
it('commits empty state when state.groups is empty', done => {
const state = storeState();
const params = {};
2019-12-26 22:10:19 +05:30
fetchPrometheusMetrics(
{
state,
commit,
dispatch,
},
params,
)
2019-09-04 21:01:54 +05:30
.then(() => {
expect(commit).toHaveBeenCalledWith(types.SET_NO_DATA_EMPTY_STATE);
expect(dispatch).not.toHaveBeenCalled();
done();
})
.catch(done.fail);
});
it('dispatches fetchPrometheusMetric for each panel query', done => {
const params = {};
const state = storeState();
2019-12-26 22:10:19 +05:30
state.dashboard.panel_groups = metricsDashboardResponse.dashboard.panel_groups;
const metric = state.dashboard.panel_groups[0].panels[0].metrics[0];
fetchPrometheusMetrics(
{
state,
commit,
dispatch,
},
params,
)
2019-09-04 21:01:54 +05:30
.then(() => {
2019-12-26 22:10:19 +05:30
expect(dispatch).toHaveBeenCalledTimes(3);
expect(dispatch).toHaveBeenCalledWith('fetchPrometheusMetric', {
metric,
params,
});
2019-09-04 21:01:54 +05:30
done();
})
.catch(done.fail);
done();
});
});
describe('fetchPrometheusMetric', () => {
it('commits prometheus query result', done => {
2019-12-26 22:10:19 +05:30
const commit = jest.fn();
2019-09-04 21:01:54 +05:30
const params = {
2019-10-12 21:52:04 +05:30
start: '2019-08-06T12:40:02.184Z',
end: '2019-08-06T20:40:02.184Z',
2019-09-04 21:01:54 +05:30
};
const metric = metricsDashboardResponse.dashboard.panel_groups[0].panels[0].metrics[0];
const state = storeState();
2019-12-26 22:10:19 +05:30
const data = metricsGroupsAPIResponse[0].panels[0].metrics[0];
const response = {
data,
};
2019-09-04 21:01:54 +05:30
mock.onGet('http://test').reply(200, response);
2019-12-26 22:10:19 +05:30
fetchPrometheusMetric({ state, commit }, { metric, params })
.then(() => {
expect(commit).toHaveBeenCalledWith(types.SET_QUERY_RESULT, {
metricId: metric.metric_id,
result: data.result,
});
done();
})
.catch(done.fail);
2019-09-04 21:01:54 +05:30
});
});
});