summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_merge_request_widget/extentions/code_quality/index_spec.js
blob: 67b327217effeb34d4fadc5399eeffdbcedadd8d (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import MockAdapter from 'axios-mock-adapter';
import { GlBadge } from '@gitlab/ui';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import { trimText } from 'helpers/text_helper';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
import extensionsContainer from '~/vue_merge_request_widget/components/extensions/container';
import { registerExtension } from '~/vue_merge_request_widget/components/extensions';
import codeQualityExtension from '~/vue_merge_request_widget/extensions/code_quality';
import {
  HTTP_STATUS_INTERNAL_SERVER_ERROR,
  HTTP_STATUS_NO_CONTENT,
  HTTP_STATUS_OK,
} from '~/lib/utils/http_status';
import {
  i18n,
  codeQualityPrefixes,
} from '~/vue_merge_request_widget/extensions/code_quality/constants';
import {
  codeQualityResponseNewErrors,
  codeQualityResponseResolvedErrors,
  codeQualityResponseResolvedAndNewErrors,
  codeQualityResponseNoErrors,
} from './mock_data';

describe('Code Quality extension', () => {
  let wrapper;
  let mock;

  registerExtension(codeQualityExtension);

  const endpoint = '/root/repo/-/merge_requests/4/accessibility_reports.json';

  const mockApi = (statusCode, data) => {
    mock.onGet(endpoint).reply(statusCode, data);
  };

  const findToggleCollapsedButton = () => wrapper.findByTestId('toggle-button');
  const findAllExtensionListItems = () => wrapper.findAllByTestId('extension-list-item');
  const isCollapsable = () => wrapper.findByTestId('toggle-button').exists();
  const getNeutralIcon = () => wrapper.findByTestId('status-neutral-icon').exists();
  const getAlertIcon = () => wrapper.findByTestId('status-alert-icon').exists();
  const getSuccessIcon = () => wrapper.findByTestId('status-success-icon').exists();

  const createComponent = () => {
    wrapper = mountExtended(extensionsContainer, {
      propsData: {
        mr: {
          codeQuality: endpoint,
          blobPath: {
            head_path: 'example/path',
            base_path: 'example/path',
          },
        },
      },
    });
  };

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

  afterEach(() => {
    wrapper.destroy();
    mock.restore();
  });

  describe('summary', () => {
    it('displays loading text', () => {
      mockApi(HTTP_STATUS_OK, codeQualityResponseNewErrors);

      createComponent();

      expect(wrapper.text()).toBe(i18n.loading);
    });

    it('with a 204 response, continues to display loading state', async () => {
      mockApi(HTTP_STATUS_NO_CONTENT, '');
      createComponent();

      await waitForPromises();

      expect(wrapper.text()).toBe(i18n.loading);
    });

    it('displays failed loading text', async () => {
      mockApi(HTTP_STATUS_INTERNAL_SERVER_ERROR);

      createComponent();

      await waitForPromises();

      expect(wrapper.text()).toBe(i18n.error);
      expect(isCollapsable()).toBe(false);
    });

    it('displays new Errors finding', async () => {
      mockApi(HTTP_STATUS_OK, codeQualityResponseNewErrors);

      createComponent();

      await waitForPromises();
      expect(wrapper.text()).toBe(
        i18n
          .singularCopy(
            i18n.findings(codeQualityResponseNewErrors.new_errors, codeQualityPrefixes.new),
          )
          .replace(/%{strong_start}/g, '')
          .replace(/%{strong_end}/g, ''),
      );
      expect(isCollapsable()).toBe(true);
      expect(getAlertIcon()).toBe(true);
    });

    it('displays resolved Errors finding', async () => {
      mockApi(HTTP_STATUS_OK, codeQualityResponseResolvedErrors);

      createComponent();

      await waitForPromises();
      expect(wrapper.text()).toBe(
        i18n
          .singularCopy(
            i18n.findings(
              codeQualityResponseResolvedErrors.resolved_errors,
              codeQualityPrefixes.fixed,
            ),
          )
          .replace(/%{strong_start}/g, '')
          .replace(/%{strong_end}/g, ''),
      );
      expect(isCollapsable()).toBe(true);
      expect(getSuccessIcon()).toBe(true);
    });

    it('displays quality improvement and degradation', async () => {
      mockApi(HTTP_STATUS_OK, codeQualityResponseResolvedAndNewErrors);

      createComponent();
      await waitForPromises();

      // replacing strong tags because they will not be found in the rendered text
      expect(wrapper.text()).toBe(
        i18n
          .improvementAndDegradationCopy(
            i18n.findings(
              codeQualityResponseResolvedAndNewErrors.resolved_errors,
              codeQualityPrefixes.fixed,
            ),
            i18n.findings(
              codeQualityResponseResolvedAndNewErrors.new_errors,
              codeQualityPrefixes.new,
            ),
          )
          .replace(/%{strong_start}/g, '')
          .replace(/%{strong_end}/g, ''),
      );
      expect(isCollapsable()).toBe(true);
      expect(getAlertIcon()).toBe(true);
    });

    it('displays no detected errors', async () => {
      mockApi(HTTP_STATUS_OK, codeQualityResponseNoErrors);

      createComponent();

      await waitForPromises();

      expect(wrapper.text()).toBe(i18n.noChanges);
      expect(isCollapsable()).toBe(false);
      expect(getNeutralIcon()).toBe(true);
    });
  });

  describe('expanded data', () => {
    beforeEach(async () => {
      mockApi(HTTP_STATUS_OK, codeQualityResponseResolvedAndNewErrors);

      createComponent();

      await waitForPromises();

      findToggleCollapsedButton().trigger('click');

      await waitForPromises();
    });

    it('displays all report list items in viewport', async () => {
      expect(findAllExtensionListItems()).toHaveLength(2);
    });

    it('displays report list item formatted', () => {
      const text = {
        newError: trimText(findAllExtensionListItems().at(0).text().replace(/\s+/g, ' ').trim()),
        resolvedError: findAllExtensionListItems().at(1).text().replace(/\s+/g, ' ').trim(),
      };

      expect(text.newError).toContain(
        "Minor - Parsing error: 'return' outside of function in index.js:12",
      );
      expect(text.resolvedError).toContain(
        "Minor - Parsing error: 'return' outside of function Fixed in index.js:12",
      );
    });

    it('adds fixed indicator (badge) when error is resolved', () => {
      expect(findAllExtensionListItems().at(1).findComponent(GlBadge).exists()).toBe(true);
      expect(findAllExtensionListItems().at(1).findComponent(GlBadge).text()).toEqual(i18n.fixed);
    });

    it('should not add fixed indicator (badge) when error is new', () => {
      expect(findAllExtensionListItems().at(0).findComponent(GlBadge).exists()).toBe(false);
    });
  });
});