diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-13 15:07:41 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-13 15:07:41 +0300 |
commit | 8cc5f2790908ba9bb8eecba2b78a3c5a88c77b90 (patch) | |
tree | 2d6211503a5111d43a9edce0c56b94fd1b347e1b /spec/frontend | |
parent | 17b91a3c6ab73fff087e91665e9afb8046cbf045 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend')
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 42 | ||||
-rw-r--r-- | spec/frontend/monitoring/charts/time_series_spec.js | 5 | ||||
-rw-r--r-- | spec/frontend/monitoring/store/actions_spec.js | 103 | ||||
-rw-r--r-- | spec/frontend/monitoring/store/getters_spec.js | 12 | ||||
-rw-r--r-- | spec/frontend/monitoring/store/mutations_spec.js | 149 |
5 files changed, 258 insertions, 53 deletions
diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index 97f7f05cd85..0e818d6c402 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -323,3 +323,45 @@ describe('URL utility', () => { }); }); }); + +describe('setUrlParams', () => { + it('adds new params as query string', () => { + const url = 'https://gitlab.com/test'; + + expect( + urlUtils.setUrlParams({ group_id: 'gitlab-org', project_id: 'my-project' }, url), + ).toEqual('https://gitlab.com/test?group_id=gitlab-org&project_id=my-project'); + }); + + it('updates an existing parameter', () => { + const url = 'https://gitlab.com/test?group_id=gitlab-org&project_id=my-project'; + + expect(urlUtils.setUrlParams({ project_id: 'gitlab-test' }, url)).toEqual( + 'https://gitlab.com/test?group_id=gitlab-org&project_id=gitlab-test', + ); + }); + + it("removes the project_id param when it's value is null", () => { + const url = 'https://gitlab.com/test?group_id=gitlab-org&project_id=my-project'; + + expect(urlUtils.setUrlParams({ project_id: null }, url)).toEqual( + 'https://gitlab.com/test?group_id=gitlab-org', + ); + }); + + it('handles arrays properly', () => { + const url = 'https://gitlab.com/test'; + + expect(urlUtils.setUrlParams({ label_name: ['foo', 'bar'] }, url)).toEqual( + 'https://gitlab.com/test?label_name=foo&label_name=bar', + ); + }); + + it('removes all existing URL params and sets a new param when cleanParams=true', () => { + const url = 'https://gitlab.com/test?group_id=gitlab-org&project_id=my-project'; + + expect(urlUtils.setUrlParams({ foo: 'bar' }, url, true)).toEqual( + 'https://gitlab.com/test?foo=bar', + ); + }); +}); diff --git a/spec/frontend/monitoring/charts/time_series_spec.js b/spec/frontend/monitoring/charts/time_series_spec.js index 15967992374..128c4bc49f1 100644 --- a/spec/frontend/monitoring/charts/time_series_spec.js +++ b/spec/frontend/monitoring/charts/time_series_spec.js @@ -46,7 +46,10 @@ describe('Time series component', () => { store.commit(`monitoringDashboard/${types.RECEIVE_DEPLOYMENTS_DATA_SUCCESS}`, deploymentData); // Mock data contains 2 panel groups, with 1 and 2 panels respectively - store.commit(`monitoringDashboard/${types.SET_QUERY_RESULT}`, mockedQueryResultPayload); + store.commit( + `monitoringDashboard/${types.RECEIVE_METRIC_RESULT_SUCCESS}`, + mockedQueryResultPayload, + ); // Pick the second panel group and the first panel in it [mockGraphData] = store.state.monitoringDashboard.dashboard.panel_groups[1].panels; diff --git a/spec/frontend/monitoring/store/actions_spec.js b/spec/frontend/monitoring/store/actions_spec.js index e41158eb4b0..92d469270c9 100644 --- a/spec/frontend/monitoring/store/actions_spec.js +++ b/spec/frontend/monitoring/store/actions_spec.js @@ -434,13 +434,11 @@ describe('Monitoring store actions', () => { start: '2019-08-06T12:40:02.184Z', end: '2019-08-06T20:40:02.184Z', }; - let commit; let metric; let state; let data; beforeEach(() => { - commit = jest.fn(); state = storeState(); [metric] = metricsDashboardResponse.dashboard.panel_groups[0].panels[0].metrics; [data] = metricsGroupsAPIResponse[0].panels[0].metrics; @@ -449,17 +447,31 @@ describe('Monitoring store actions', () => { it('commits result', done => { mock.onGet('http://test').reply(200, { data }); // One attempt - fetchPrometheusMetric({ state, commit }, { metric, params }) - .then(() => { - expect(commit).toHaveBeenCalledWith(types.SET_QUERY_RESULT, { - metricId: metric.metric_id, - result: data.result, - }); - + testAction( + fetchPrometheusMetric, + { metric, params }, + state, + [ + { + type: types.REQUEST_METRIC_RESULT, + payload: { + metricId: metric.metric_id, + }, + }, + { + type: types.RECEIVE_METRIC_RESULT_SUCCESS, + payload: { + metricId: metric.metric_id, + result: data.result, + }, + }, + ], + [], + () => { expect(mock.history.get).toHaveLength(1); done(); - }) - .catch(done.fail); + }, + ).catch(done.fail); }); it('commits result, when waiting for results', done => { @@ -469,18 +481,31 @@ describe('Monitoring store actions', () => { mock.onGet('http://test').replyOnce(statusCodes.NO_CONTENT); mock.onGet('http://test').reply(200, { data }); // 4th attempt - const fetch = fetchPrometheusMetric({ state, commit }, { metric, params }); - - fetch - .then(() => { - expect(commit).toHaveBeenCalledWith(types.SET_QUERY_RESULT, { - metricId: metric.metric_id, - result: data.result, - }); + testAction( + fetchPrometheusMetric, + { metric, params }, + state, + [ + { + type: types.REQUEST_METRIC_RESULT, + payload: { + metricId: metric.metric_id, + }, + }, + { + type: types.RECEIVE_METRIC_RESULT_SUCCESS, + payload: { + metricId: metric.metric_id, + result: data.result, + }, + }, + ], + [], + () => { expect(mock.history.get).toHaveLength(4); done(); - }) - .catch(done.fail); + }, + ).catch(done.fail); }); it('commits failure, when waiting for results and getting a server error', done => { @@ -490,15 +515,33 @@ describe('Monitoring store actions', () => { mock.onGet('http://test').replyOnce(statusCodes.NO_CONTENT); mock.onGet('http://test').reply(500); // 4th attempt - fetchPrometheusMetric({ state, commit }, { metric, params }) - .then(() => { - done.fail(); - }) - .catch(() => { - expect(commit).not.toHaveBeenCalled(); - expect(mock.history.get).toHaveLength(4); - done(); - }); + const error = new Error('Request failed with status code 500'); + + testAction( + fetchPrometheusMetric, + { metric, params }, + state, + [ + { + type: types.REQUEST_METRIC_RESULT, + payload: { + metricId: metric.metric_id, + }, + }, + { + type: types.RECEIVE_METRIC_RESULT_ERROR, + payload: { + metricId: metric.metric_id, + error, + }, + }, + ], + [], + ).catch(e => { + expect(mock.history.get).toHaveLength(4); + expect(e).toEqual(error); + done(); + }); }); }); }); diff --git a/spec/frontend/monitoring/store/getters_spec.js b/spec/frontend/monitoring/store/getters_spec.js index 066f927dce7..3b6f33ed8b1 100644 --- a/spec/frontend/monitoring/store/getters_spec.js +++ b/spec/frontend/monitoring/store/getters_spec.js @@ -57,22 +57,22 @@ describe('Monitoring store Getters', () => { it('an empty metric, returns empty', () => { mutations[types.RECEIVE_METRICS_DATA_SUCCESS](state, metricsGroupsAPIResponse); - mutations[types.SET_QUERY_RESULT](state, mockedEmptyResult); + mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, mockedEmptyResult); expect(metricsWithData()).toEqual([]); }); it('a metric with results, it returns a metric', () => { mutations[types.RECEIVE_METRICS_DATA_SUCCESS](state, metricsGroupsAPIResponse); - mutations[types.SET_QUERY_RESULT](state, mockedQueryResultPayload); + mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, mockedQueryResultPayload); expect(metricsWithData()).toEqual([mockedQueryResultPayload.metricId]); }); it('multiple metrics with results, it return multiple metrics', () => { mutations[types.RECEIVE_METRICS_DATA_SUCCESS](state, metricsGroupsAPIResponse); - mutations[types.SET_QUERY_RESULT](state, mockedQueryResultPayload); - mutations[types.SET_QUERY_RESULT](state, mockedQueryResultPayloadCoresTotal); + mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, mockedQueryResultPayload); + mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, mockedQueryResultPayloadCoresTotal); expect(metricsWithData()).toEqual([ mockedQueryResultPayload.metricId, @@ -82,8 +82,8 @@ describe('Monitoring store Getters', () => { it('multiple metrics with results, it returns metrics filtered by group', () => { mutations[types.RECEIVE_METRICS_DATA_SUCCESS](state, metricsGroupsAPIResponse); - mutations[types.SET_QUERY_RESULT](state, mockedQueryResultPayload); - mutations[types.SET_QUERY_RESULT](state, mockedQueryResultPayloadCoresTotal); + mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, mockedQueryResultPayload); + mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, mockedQueryResultPayloadCoresTotal); // First group has no metrics expect(metricsWithData(state.dashboard.panel_groups[0].key)).toEqual([]); diff --git a/spec/frontend/monitoring/store/mutations_spec.js b/spec/frontend/monitoring/store/mutations_spec.js index 47177180aa1..8da172ec634 100644 --- a/spec/frontend/monitoring/store/mutations_spec.js +++ b/spec/frontend/monitoring/store/mutations_spec.js @@ -1,6 +1,9 @@ +import httpStatusCodes from '~/lib/utils/http_status'; + import mutations from '~/monitoring/stores/mutations'; import * as types from '~/monitoring/stores/mutation_types'; import state from '~/monitoring/stores/state'; +import { metricsErrors } from '~/monitoring/constants'; import { metricsGroupsAPIResponse, deploymentData, @@ -90,7 +93,7 @@ describe('Monitoring mutations', () => { expect(stateCopy.projectPath).toEqual('/gitlab-org/gitlab-foss'); }); }); - describe('SET_QUERY_RESULT', () => { + describe('Individual panel/metric results', () => { const metricId = '12_system_metrics_kubernetes_container_memory_total'; const result = [ { @@ -98,31 +101,145 @@ describe('Monitoring mutations', () => { }, ]; const dashboardGroups = metricsDashboardResponse.dashboard.panel_groups; - const getMetrics = () => stateCopy.dashboard.panel_groups[0].panels[0].metrics; + const getMetric = () => stateCopy.dashboard.panel_groups[0].panels[0].metrics[0]; - beforeEach(() => { - mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups); + describe('REQUEST_METRIC_RESULT', () => { + beforeEach(() => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups); + }); + it('stores a loading state on a metric', () => { + expect(stateCopy.showEmptyState).toBe(true); + + mutations[types.REQUEST_METRIC_RESULT](stateCopy, { + metricId, + result, + }); + + expect(stateCopy.showEmptyState).toBe(true); + expect(getMetric()).toEqual( + expect.objectContaining({ + loading: true, + result: null, + error: null, + }), + ); + }); }); - it('clears empty state', () => { - expect(stateCopy.showEmptyState).toBe(true); - mutations[types.SET_QUERY_RESULT](stateCopy, { - metricId, - result, + describe('RECEIVE_METRIC_RESULT_SUCCESS', () => { + beforeEach(() => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups); }); + it('clears empty state', () => { + expect(stateCopy.showEmptyState).toBe(true); - expect(stateCopy.showEmptyState).toBe(false); + mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](stateCopy, { + metricId, + result, + }); + + expect(stateCopy.showEmptyState).toBe(false); + }); + + it('adds results to the store', () => { + expect(getMetric().result).toBe(undefined); + + mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](stateCopy, { + metricId, + result, + }); + + expect(getMetric().result).toHaveLength(result.length); + expect(getMetric()).toEqual( + expect.objectContaining({ + loading: false, + error: null, + }), + ); + }); }); - it('adds results to the store', () => { - expect(getMetrics()[0].result).toBe(undefined); + describe('RECEIVE_METRIC_RESULT_ERROR', () => { + beforeEach(() => { + mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups); + }); + it('maintains the loading state when a metric fails', () => { + expect(stateCopy.showEmptyState).toBe(true); + + mutations[types.RECEIVE_METRIC_RESULT_ERROR](stateCopy, { + metricId, + error: 'an error', + }); + + expect(stateCopy.showEmptyState).toBe(true); + }); + + it('stores a timeout error in a metric', () => { + mutations[types.RECEIVE_METRIC_RESULT_ERROR](stateCopy, { + metricId, + error: { message: 'BACKOFF_TIMEOUT' }, + }); + + expect(getMetric()).toEqual( + expect.objectContaining({ + loading: false, + result: null, + error: metricsErrors.TIMEOUT, + }), + ); + }); + + it('stores a connection failed error in a metric', () => { + mutations[types.RECEIVE_METRIC_RESULT_ERROR](stateCopy, { + metricId, + error: { + response: { + status: httpStatusCodes.SERVICE_UNAVAILABLE, + }, + }, + }); + expect(getMetric()).toEqual( + expect.objectContaining({ + loading: false, + result: null, + error: metricsErrors.CONNECTION_FAILED, + }), + ); + }); - mutations[types.SET_QUERY_RESULT](stateCopy, { - metricId, - result, + it('stores a bad data error in a metric', () => { + mutations[types.RECEIVE_METRIC_RESULT_ERROR](stateCopy, { + metricId, + error: { + response: { + status: httpStatusCodes.BAD_REQUEST, + }, + }, + }); + + expect(getMetric()).toEqual( + expect.objectContaining({ + loading: false, + result: null, + error: metricsErrors.BAD_DATA, + }), + ); }); - expect(getMetrics()[0].result).toHaveLength(result.length); + it('stores an unknown error in a metric', () => { + mutations[types.RECEIVE_METRIC_RESULT_ERROR](stateCopy, { + metricId, + error: null, // no reason in response + }); + + expect(getMetric()).toEqual( + expect.objectContaining({ + loading: false, + result: null, + error: metricsErrors.UNKNOWN_ERROR, + }), + ); + }); }); }); describe('SET_ALL_DASHBOARDS', () => { |