Commit f13ae2fe authored by Miguel Rincon's avatar Miguel Rincon

Refactor to remove hardcoded mock data

We can simplify adding data to state by using already
existing helpers.
parent e5b26975
...@@ -36,29 +36,6 @@ export const dashboardProps = { ...@@ -36,29 +36,6 @@ export const dashboardProps = {
export const metricsDashboardViewModel = mapToDashboardViewModel(metricsDashboardPayload); export const metricsDashboardViewModel = mapToDashboardViewModel(metricsDashboardPayload);
export const metricsDashboardPanelCount = 22; export const metricsDashboardPanelCount = 22;
export const metricResultStatus = {
// First metric in fixture `metrics_dashboard/environment_metrics_dashboard.json`
metricId: 'NO_DB_response_metrics_nginx_ingress_throughput_status_code',
data: {
resultType: 'matrix',
result: metricsResult,
},
};
export const metricResultPods = {
// Second metric in fixture `metrics_dashboard/environment_metrics_dashboard.json`
metricId: 'NO_DB_response_metrics_nginx_ingress_latency_pod_average',
data: {
resultType: 'matrix',
result: metricsResult,
},
};
export const metricResultEmpty = {
metricId: 'NO_DB_response_metrics_nginx_ingress_16_throughput_status_code',
data: {
resultType: 'matrix',
result: [],
},
};
// Graph data // Graph data
......
...@@ -11,37 +11,36 @@ import { ...@@ -11,37 +11,36 @@ import {
storeVariables, storeVariables,
mockLinks, mockLinks,
} from '../mock_data'; } from '../mock_data';
import { import { metricsDashboardPayload } from '../fixture_data';
metricsDashboardPayload,
metricResultStatus,
metricResultPods,
metricResultEmpty,
} from '../fixture_data';
describe('Monitoring store Getters', () => { describe('Monitoring store Getters', () => {
let state;
const getMetric = ({ group = 0, panel = 0, metric = 0 } = {}) =>
state.dashboard.panelGroups[group].panels[panel].metrics[metric];
const setMetricSuccess = ({ group, panel, metric, result = metricsResult } = {}) => {
const { metricId } = getMetric({ group, panel, metric });
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, {
metricId,
data: {
resultType: 'matrix',
result,
},
});
};
const setMetricFailure = ({ group, panel, metric } = {}) => {
const { metricId } = getMetric({ group, panel, metric });
mutations[types.RECEIVE_METRIC_RESULT_FAILURE](state, {
metricId,
});
};
describe('getMetricStates', () => { describe('getMetricStates', () => {
let setupState; let setupState;
let state;
let getMetricStates; let getMetricStates;
const setMetricSuccess = ({ result = metricsResult, group = 0, panel = 0, metric = 0 }) => {
const { metricId } = state.dashboard.panelGroups[group].panels[panel].metrics[metric];
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, {
metricId,
data: {
resultType: 'matrix',
result,
},
});
};
const setMetricFailure = ({ group = 0, panel = 0, metric = 0 }) => {
const { metricId } = state.dashboard.panelGroups[group].panels[panel].metrics[metric];
mutations[types.RECEIVE_METRIC_RESULT_FAILURE](state, {
metricId,
});
};
beforeEach(() => { beforeEach(() => {
setupState = (initState = {}) => { setupState = (initState = {}) => {
state = initState; state = initState;
...@@ -81,7 +80,7 @@ describe('Monitoring store Getters', () => { ...@@ -81,7 +80,7 @@ describe('Monitoring store Getters', () => {
it('on an empty metric with no result, returns NO_DATA', () => { it('on an empty metric with no result, returns NO_DATA', () => {
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload); mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
setMetricSuccess({ result: [], group: 2 }); setMetricSuccess({ group: 2, result: [] });
expect(getMetricStates()).toEqual([metricStates.NO_DATA]); expect(getMetricStates()).toEqual([metricStates.NO_DATA]);
}); });
...@@ -147,7 +146,6 @@ describe('Monitoring store Getters', () => { ...@@ -147,7 +146,6 @@ describe('Monitoring store Getters', () => {
describe('metricsWithData', () => { describe('metricsWithData', () => {
let metricsWithData; let metricsWithData;
let setupState; let setupState;
let state;
beforeEach(() => { beforeEach(() => {
setupState = (initState = {}) => { setupState = (initState = {}) => {
...@@ -191,35 +189,39 @@ describe('Monitoring store Getters', () => { ...@@ -191,35 +189,39 @@ describe('Monitoring store Getters', () => {
it('an empty metric, returns empty', () => { it('an empty metric, returns empty', () => {
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload); mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, metricResultEmpty); setMetricSuccess({ result: [] });
expect(metricsWithData()).toEqual([]); expect(metricsWithData()).toEqual([]);
}); });
it('a metric with results, it returns a metric', () => { it('a metric with results, it returns a metric', () => {
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload); mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, metricResultStatus); setMetricSuccess();
expect(metricsWithData()).toEqual([metricResultStatus.metricId]); expect(metricsWithData()).toEqual([getMetric().metricId]);
}); });
it('multiple metrics with results, it return multiple metrics', () => { it('multiple metrics with results, it return multiple metrics', () => {
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload); mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, metricResultStatus); setMetricSuccess({ panel: 0 });
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, metricResultPods); setMetricSuccess({ panel: 1 });
expect(metricsWithData()).toEqual([metricResultStatus.metricId, metricResultPods.metricId]); expect(metricsWithData()).toEqual([
getMetric({ panel: 0 }).metricId,
getMetric({ panel: 1 }).metricId,
]);
}); });
it('multiple metrics with results, it returns metrics filtered by group', () => { it('multiple metrics with results, it returns metrics filtered by group', () => {
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload); mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, metricResultStatus);
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, metricResultPods); setMetricSuccess({ group: 1 });
setMetricSuccess({ group: 1, panel: 1 });
// First group has metrics // First group has metrics
expect(metricsWithData(state.dashboard.panelGroups[1].key)).toEqual([ expect(metricsWithData(state.dashboard.panelGroups[1].key)).toEqual([
metricResultStatus.metricId, getMetric({ group: 1 }).metricId,
metricResultPods.metricId, getMetric({ group: 1, panel: 1 }).metricId,
]); ]);
// Second group has no metrics // Second group has no metrics
...@@ -229,7 +231,6 @@ describe('Monitoring store Getters', () => { ...@@ -229,7 +231,6 @@ describe('Monitoring store Getters', () => {
}); });
describe('filteredEnvironments', () => { describe('filteredEnvironments', () => {
let state;
const setupState = (initState = {}) => { const setupState = (initState = {}) => {
state = { state = {
...state, ...state,
...@@ -284,7 +285,6 @@ describe('Monitoring store Getters', () => { ...@@ -284,7 +285,6 @@ describe('Monitoring store Getters', () => {
describe('metricsSavedToDb', () => { describe('metricsSavedToDb', () => {
let metricsSavedToDb; let metricsSavedToDb;
let state;
let mockData; let mockData;
beforeEach(() => { beforeEach(() => {
...@@ -335,8 +335,6 @@ describe('Monitoring store Getters', () => { ...@@ -335,8 +335,6 @@ describe('Monitoring store Getters', () => {
}); });
describe('getCustomVariablesParams', () => { describe('getCustomVariablesParams', () => {
let state;
beforeEach(() => { beforeEach(() => {
state = { state = {
variables: {}, variables: {},
...@@ -367,58 +365,65 @@ describe('Monitoring store Getters', () => { ...@@ -367,58 +365,65 @@ describe('Monitoring store Getters', () => {
describe('selectedDashboard', () => { describe('selectedDashboard', () => {
const { selectedDashboard } = getters; const { selectedDashboard } = getters;
const localGetters = state => ({ const localGetters = localState => ({
fullDashboardPath: getters.fullDashboardPath(state), fullDashboardPath: getters.fullDashboardPath(localState),
}); });
it('returns a dashboard', () => { it('returns a dashboard', () => {
const state = { const localState = {
allDashboards: dashboardGitResponse, allDashboards: dashboardGitResponse,
currentDashboard: dashboardGitResponse[0].path, currentDashboard: dashboardGitResponse[0].path,
customDashboardBasePath, customDashboardBasePath,
}; };
expect(selectedDashboard(state, localGetters(state))).toEqual(dashboardGitResponse[0]); expect(selectedDashboard(localState, localGetters(localState))).toEqual(
dashboardGitResponse[0],
);
}); });
it('returns a non-default dashboard', () => { it('returns a non-default dashboard', () => {
const state = { const localState = {
allDashboards: dashboardGitResponse, allDashboards: dashboardGitResponse,
currentDashboard: dashboardGitResponse[1].path, currentDashboard: dashboardGitResponse[1].path,
customDashboardBasePath, customDashboardBasePath,
}; };
expect(selectedDashboard(state, localGetters(state))).toEqual(dashboardGitResponse[1]); expect(selectedDashboard(localState, localGetters(localState))).toEqual(
dashboardGitResponse[1],
);
}); });
it('returns a default dashboard when no dashboard is selected', () => { it('returns a default dashboard when no dashboard is selected', () => {
const state = { const localState = {
allDashboards: dashboardGitResponse, allDashboards: dashboardGitResponse,
currentDashboard: null, currentDashboard: null,
customDashboardBasePath, customDashboardBasePath,
}; };
expect(selectedDashboard(state, localGetters(state))).toEqual(dashboardGitResponse[0]); expect(selectedDashboard(localState, localGetters(localState))).toEqual(
dashboardGitResponse[0],
);
}); });
it('returns a default dashboard when dashboard cannot be found', () => { it('returns a default dashboard when dashboard cannot be found', () => {
const state = { const localState = {
allDashboards: dashboardGitResponse, allDashboards: dashboardGitResponse,
currentDashboard: 'wrong_path', currentDashboard: 'wrong_path',
customDashboardBasePath, customDashboardBasePath,
}; };
expect(selectedDashboard(state, localGetters(state))).toEqual(dashboardGitResponse[0]); expect(selectedDashboard(localState, localGetters(localState))).toEqual(
dashboardGitResponse[0],
);
}); });
it('returns null when no dashboards are present', () => { it('returns null when no dashboards are present', () => {
const state = { const localState = {
allDashboards: [], allDashboards: [],
currentDashboard: dashboardGitResponse[0].path, currentDashboard: dashboardGitResponse[0].path,
customDashboardBasePath, customDashboardBasePath,
}; };
expect(selectedDashboard(state, localGetters(state))).toEqual(null); expect(selectedDashboard(localState, localGetters(localState))).toEqual(null);
}); });
}); });
describe('linksWithMetadata', () => { describe('linksWithMetadata', () => {
let state;
const setupState = (initState = {}) => { const setupState = (initState = {}) => {
state = { state = {
...state, ...state,
......
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