Welcome to mirror list, hosted at ThFree Co, Russian Federation.

alert_management_alerts_api_spec.js « api « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 507f659a170e22a6b218583a8f49e0d1e7ca2a97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import MockAdapter from 'axios-mock-adapter';
import * as alertManagementAlertsApi from '~/api/alert_management_alerts_api';
import axios from '~/lib/utils/axios_utils';
import {
  HTTP_STATUS_CREATED,
  HTTP_STATUS_NO_CONTENT,
  HTTP_STATUS_OK,
} from '~/lib/utils/http_status';

describe('~/api/alert_management_alerts_api.js', () => {
  let mock;
  let originalGon;

  const projectId = 1;
  const alertIid = 2;

  const imageData = { filePath: 'test', filename: 'hello', id: 5, url: null };

  beforeEach(() => {
    mock = new MockAdapter(axios);

    originalGon = window.gon;
    window.gon = { api_version: 'v4' };
  });

  afterEach(() => {
    mock.restore();
    window.gon = originalGon;
  });

  describe('fetchAlertMetricImages', () => {
    beforeEach(() => {
      jest.spyOn(axios, 'get');
    });

    it('retrieves metric images from the correct URL and returns them in the response data', () => {
      const expectedUrl = `/api/v4/projects/${projectId}/alert_management_alerts/${alertIid}/metric_images`;
      const expectedData = [imageData];
      const options = { alertIid, id: projectId };

      mock.onGet(expectedUrl).reply(HTTP_STATUS_OK, { data: expectedData });

      return alertManagementAlertsApi.fetchAlertMetricImages(options).then(({ data }) => {
        expect(axios.get).toHaveBeenCalledWith(expectedUrl);
        expect(data.data).toEqual(expectedData);
      });
    });
  });

  describe('uploadAlertMetricImage', () => {
    beforeEach(() => {
      jest.spyOn(axios, 'post');
    });

    it('uploads a metric image to the correct URL and returns it in the response data', () => {
      const expectedUrl = `/api/v4/projects/${projectId}/alert_management_alerts/${alertIid}/metric_images`;
      const expectedData = [imageData];

      const file = new File(['zip contents'], 'hello');
      const url = 'https://www.example.com';
      const urlText = 'Example website';

      const expectedFormData = new FormData();
      expectedFormData.append('file', file);
      expectedFormData.append('url', url);
      expectedFormData.append('url_text', urlText);

      mock.onPost(expectedUrl).reply(HTTP_STATUS_CREATED, { data: expectedData });

      return alertManagementAlertsApi
        .uploadAlertMetricImage({
          alertIid,
          id: projectId,
          file,
          url,
          urlText,
        })
        .then(({ data }) => {
          expect(data).toEqual({ data: expectedData });
          expect(axios.post).toHaveBeenCalledWith(expectedUrl, expectedFormData, {
            headers: { 'Content-Type': 'multipart/form-data' },
          });
        });
    });
  });

  describe('updateAlertMetricImage', () => {
    beforeEach(() => {
      jest.spyOn(axios, 'put');
    });

    it('updates a metric image to the correct URL and returns it in the response data', () => {
      const imageIid = 3;
      const expectedUrl = `/api/v4/projects/${projectId}/alert_management_alerts/${alertIid}/metric_images/${imageIid}`;
      const expectedData = [imageData];

      const url = 'https://www.example.com';
      const urlText = 'Example website';

      const expectedFormData = new FormData();
      expectedFormData.append('url', url);
      expectedFormData.append('url_text', urlText);

      mock.onPut(expectedUrl).reply(HTTP_STATUS_OK, { data: expectedData });

      return alertManagementAlertsApi
        .updateAlertMetricImage({
          alertIid,
          id: projectId,
          imageId: imageIid,
          url,
          urlText,
        })
        .then(({ data }) => {
          expect(data).toEqual({ data: expectedData });
          expect(axios.put).toHaveBeenCalledWith(expectedUrl, expectedFormData);
        });
    });
  });

  describe('deleteAlertMetricImage', () => {
    beforeEach(() => {
      jest.spyOn(axios, 'delete');
    });

    it('deletes a metric image to the correct URL and returns it in the response data', () => {
      const imageIid = 3;
      const expectedUrl = `/api/v4/projects/${projectId}/alert_management_alerts/${alertIid}/metric_images/${imageIid}`;
      const expectedData = [imageData];

      mock.onDelete(expectedUrl).reply(HTTP_STATUS_NO_CONTENT, { data: expectedData });

      return alertManagementAlertsApi
        .deleteAlertMetricImage({
          alertIid,
          id: projectId,
          imageId: imageIid,
        })
        .then(({ data }) => {
          expect(data).toEqual({ data: expectedData });
          expect(axios.delete).toHaveBeenCalledWith(expectedUrl);
        });
    });
  });
});