diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-12-19 14:01:45 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-12-19 14:01:45 +0300 |
commit | 9297025d0b7ddf095eb618dfaaab2ff8f2018d8b (patch) | |
tree | 865198c01d1824a9b098127baa3ab980c9cd2c06 /spec/frontend/observability/client_spec.js | |
parent | 6372471f43ee03c05a7c1f8b0c6ac6b8a7431dbe (diff) |
Add latest changes from gitlab-org/gitlab@16-7-stable-eev16.7.0-rc42
Diffstat (limited to 'spec/frontend/observability/client_spec.js')
-rw-r--r-- | spec/frontend/observability/client_spec.js | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/spec/frontend/observability/client_spec.js b/spec/frontend/observability/client_spec.js index b41b303f57d..e7b68a2346e 100644 --- a/spec/frontend/observability/client_spec.js +++ b/spec/frontend/observability/client_spec.js @@ -18,6 +18,7 @@ describe('buildClient', () => { const servicesUrl = 'https://example.com/services'; const operationsUrl = 'https://example.com/services/$SERVICE_NAME$/operations'; const metricsUrl = 'https://example.com/metrics'; + const metricsSearchUrl = 'https://example.com/metrics/search'; const FETCHING_TRACES_ERROR = 'traces are missing/invalid in the response'; const apiConfig = { @@ -26,6 +27,7 @@ describe('buildClient', () => { servicesUrl, operationsUrl, metricsUrl, + metricsSearchUrl, }; const getQueryParam = () => decodeURIComponent(axios.get.mock.calls[0][1].params.toString()); @@ -311,6 +313,16 @@ describe('buildClient', () => { expect(getQueryParam()).toBe(`sort=${SORTING_OPTIONS.TIMESTAMP_DESC}`); }); + it('ignores non-array filters', async () => { + await client.fetchTraces({ + filters: { + traceId: { operator: '=', value: 'foo' }, + }, + }); + + expect(getQueryParam()).toBe(`sort=${SORTING_OPTIONS.TIMESTAMP_DESC}`); + }); + it('ignores unsupported operators', async () => { await client.fetchTraces({ filters: { @@ -429,10 +441,84 @@ describe('buildClient', () => { expect(axios.get).toHaveBeenCalledTimes(1); expect(axios.get).toHaveBeenCalledWith(metricsUrl, { withCredentials: true, + params: expect.any(URLSearchParams), }); expect(result).toEqual(mockResponse); }); + describe('query filter', () => { + beforeEach(() => { + axiosMock.onGet(metricsUrl).reply(200, { + metrics: [], + }); + }); + + it('does not set any query param without filters', async () => { + await client.fetchMetrics(); + + expect(getQueryParam()).toBe(''); + }); + + it('sets the start_with query param based on the search filter', async () => { + await client.fetchMetrics({ + filters: { search: [{ value: 'foo' }, { value: 'bar' }, { value: ' ' }] }, + }); + expect(getQueryParam()).toBe('starts_with=foo+bar'); + }); + + it('ignores empty search', async () => { + await client.fetchMetrics({ + filters: { + search: [{ value: ' ' }, { value: '' }, { value: null }, { value: undefined }], + }, + }); + expect(getQueryParam()).toBe(''); + }); + + it('ignores unsupported filters', async () => { + await client.fetchMetrics({ + filters: { + unsupportedFilter: [{ operator: '=', value: 'foo' }], + }, + }); + + expect(getQueryParam()).toBe(''); + }); + + it('ignores non-array search filters', async () => { + await client.fetchMetrics({ + filters: { + search: { value: 'foo' }, + }, + }); + + expect(getQueryParam()).toBe(''); + }); + + it('adds the search limit param if specified with the search filter', async () => { + await client.fetchMetrics({ + filters: { search: [{ value: 'foo' }] }, + limit: 50, + }); + expect(getQueryParam()).toBe('starts_with=foo&limit=50'); + }); + + it('does not add the search limit param if the search filter is missing', async () => { + await client.fetchMetrics({ + limit: 50, + }); + expect(getQueryParam()).toBe(''); + }); + + it('does not add the search limit param if the search filter is empty', async () => { + await client.fetchMetrics({ + limit: 50, + search: [{ value: ' ' }, { value: '' }, { value: null }, { value: undefined }], + }); + expect(getQueryParam()).toBe(''); + }); + }); + it('rejects if metrics are missing', async () => { axiosMock.onGet(metricsUrl).reply(200, {}); @@ -447,4 +533,40 @@ describe('buildClient', () => { expectErrorToBeReported(new Error(FETCHING_METRICS_ERROR)); }); }); + + describe('fetchMetric', () => { + it('fetches the metric from the API', async () => { + const data = { results: [] }; + axiosMock.onGet(metricsSearchUrl).reply(200, data); + + const result = await client.fetchMetric('name', 'type'); + + expect(axios.get).toHaveBeenCalledTimes(1); + expect(axios.get).toHaveBeenCalledWith(metricsSearchUrl, { + withCredentials: true, + params: new URLSearchParams({ mname: 'name', mtype: 'type' }), + }); + expect(result).toEqual(data.results); + }); + + it('rejects if results is missing from the response', async () => { + axiosMock.onGet(metricsSearchUrl).reply(200, {}); + const e = 'metrics are missing/invalid in the response'; + + await expect(client.fetchMetric('name', 'type')).rejects.toThrow(e); + expectErrorToBeReported(new Error(e)); + }); + + it('rejects if metric name is missing', async () => { + const e = 'fetchMetric() - metric name is required.'; + await expect(client.fetchMetric()).rejects.toThrow(e); + expectErrorToBeReported(new Error(e)); + }); + + it('rejects if metric type is missing', async () => { + const e = 'fetchMetric() - metric type is required.'; + await expect(client.fetchMetric('name')).rejects.toThrow(e); + expectErrorToBeReported(new Error(e)); + }); + }); }); |