summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/stage_spec.js
blob: 87b435582524e3382b094781916028fc422da537 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import 'bootstrap/js/dist/dropdown';
import { GlDropdown } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
import axios from '~/lib/utils/axios_utils';
import StageComponent from '~/pipelines/components/pipelines_list/stage.vue';
import eventHub from '~/pipelines/event_hub';
import { stageReply } from './mock_data';

describe('Pipelines stage component', () => {
  let wrapper;
  let mock;
  let glFeatures;

  const defaultProps = {
    stage: {
      status: {
        group: 'success',
        icon: 'status_success',
        title: 'success',
      },
      dropdown_path: 'path.json',
    },
    updateDropdown: false,
  };

  const createComponent = (props = {}) => {
    wrapper = mount(StageComponent, {
      attachTo: document.body,
      propsData: {
        ...defaultProps,
        ...props,
      },
      provide: {
        glFeatures,
      },
    });
  };

  beforeEach(() => {
    mock = new MockAdapter(axios);
    jest.spyOn(eventHub, '$emit');
    glFeatures = {};
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;

    eventHub.$emit.mockRestore();
    mock.restore();
  });

  describe('when ci_mini_pipeline_gl_dropdown feature flag is disabled', () => {
    const isDropdownOpen = () => wrapper.classes('show');

    describe('default', () => {
      beforeEach(() => {
        createComponent();
      });

      it('should render a dropdown with the status icon', () => {
        expect(wrapper.attributes('class')).toEqual('dropdown');
        expect(wrapper.find('svg').exists()).toBe(true);
        expect(wrapper.find('button').attributes('data-toggle')).toEqual('dropdown');
      });
    });

    describe('with successful request', () => {
      beforeEach(() => {
        mock.onGet('path.json').reply(200, stageReply);
        createComponent();
      });

      it('should render the received data and emit `clickedDropdown` event', async () => {
        wrapper.find('button').trigger('click');

        await axios.waitForAll();
        expect(wrapper.find('.js-builds-dropdown-container ul').text()).toContain(
          stageReply.latest_statuses[0].name,
        );

        expect(eventHub.$emit).toHaveBeenCalledWith('clickedDropdown');
      });
    });

    it('when request fails should close the dropdown', async () => {
      mock.onGet('path.json').reply(500);
      createComponent();
      wrapper.find({ ref: 'dropdown' }).trigger('click');

      expect(isDropdownOpen()).toBe(true);

      wrapper.find('button').trigger('click');
      await axios.waitForAll();

      expect(isDropdownOpen()).toBe(false);
    });

    describe('update endpoint correctly', () => {
      beforeEach(() => {
        const copyStage = { ...stageReply };
        copyStage.latest_statuses[0].name = 'this is the updated content';
        mock.onGet('bar.json').reply(200, copyStage);
        createComponent({
          stage: {
            status: {
              group: 'running',
              icon: 'status_running',
              title: 'running',
            },
            dropdown_path: 'bar.json',
          },
        });
        return axios.waitForAll();
      });

      it('should update the stage to request the new endpoint provided', async () => {
        wrapper.find('button').trigger('click');
        await axios.waitForAll();

        expect(wrapper.find('.js-builds-dropdown-container ul').text()).toContain(
          'this is the updated content',
        );
      });
    });

    describe('pipelineActionRequestComplete', () => {
      beforeEach(() => {
        mock.onGet('path.json').reply(200, stageReply);
        mock.onPost(`${stageReply.latest_statuses[0].status.action.path}.json`).reply(200);
      });

      const clickCiAction = async () => {
        wrapper.find('button').trigger('click');
        await axios.waitForAll();

        wrapper.find('.js-ci-action').trigger('click');
        await axios.waitForAll();
      };

      describe('within pipeline table', () => {
        it('emits `refreshPipelinesTable` event when `pipelineActionRequestComplete` is triggered', async () => {
          createComponent({ type: 'PIPELINES_TABLE' });

          await clickCiAction();

          expect(eventHub.$emit).toHaveBeenCalledWith('refreshPipelinesTable');
        });
      });

      describe('in MR widget', () => {
        beforeEach(() => {
          jest.spyOn($.fn, 'dropdown');
        });

        it('closes the dropdown when `pipelineActionRequestComplete` is triggered', async () => {
          createComponent();

          await clickCiAction();

          expect($.fn.dropdown).toHaveBeenCalledWith('toggle');
        });
      });
    });
  });

  describe('when ci_mini_pipeline_gl_dropdown feature flag is enabled', () => {
    const findDropdown = () => wrapper.find(GlDropdown);
    const findDropdownToggle = () => wrapper.find('button.gl-dropdown-toggle');
    const findDropdownMenu = () =>
      wrapper.find('[data-testid="mini-pipeline-graph-dropdown-menu-list"]');
    const findCiActionBtn = () => wrapper.find('.js-ci-action');

    const openGlDropdown = () => {
      findDropdownToggle().trigger('click');
      return new Promise((resolve) => {
        wrapper.vm.$root.$on('bv::dropdown::show', resolve);
      });
    };

    beforeEach(() => {
      glFeatures = { ciMiniPipelineGlDropdown: true };
    });

    describe('default', () => {
      beforeEach(() => {
        createComponent();
      });

      it('should render a dropdown with the status icon', () => {
        expect(findDropdown().exists()).toBe(true);
        expect(findDropdownToggle().classes('gl-dropdown-toggle')).toEqual(true);
        expect(wrapper.find('[data-testid="status_success_borderless-icon"]').exists()).toBe(true);
      });
    });

    describe('with successful request', () => {
      beforeEach(() => {
        mock.onGet('path.json').reply(200, stageReply);
        createComponent();
      });

      it('should render the received data and emit `clickedDropdown` event', async () => {
        await openGlDropdown();
        await axios.waitForAll();

        expect(findDropdownMenu().text()).toContain(stageReply.latest_statuses[0].name);
        expect(eventHub.$emit).toHaveBeenCalledWith('clickedDropdown');
      });
    });

    it('when request fails should close the dropdown', async () => {
      mock.onGet('path.json').reply(500);

      createComponent();

      await openGlDropdown();
      await axios.waitForAll();

      expect(findDropdown().classes('show')).toBe(false);
    });

    describe('update endpoint correctly', () => {
      beforeEach(async () => {
        const copyStage = { ...stageReply };
        copyStage.latest_statuses[0].name = 'this is the updated content';
        mock.onGet('bar.json').reply(200, copyStage);
        createComponent({
          stage: {
            status: {
              group: 'running',
              icon: 'status_running',
              title: 'running',
            },
            dropdown_path: 'bar.json',
          },
        });
        await axios.waitForAll();
      });

      it('should update the stage to request the new endpoint provided', async () => {
        await openGlDropdown();
        await axios.waitForAll();

        expect(findDropdownMenu().text()).toContain('this is the updated content');
      });
    });

    describe('pipelineActionRequestComplete', () => {
      beforeEach(() => {
        mock.onGet('path.json').reply(200, stageReply);
        mock.onPost(`${stageReply.latest_statuses[0].status.action.path}.json`).reply(200);
      });

      const clickCiAction = async () => {
        await openGlDropdown();
        await axios.waitForAll();

        findCiActionBtn().trigger('click');
        await axios.waitForAll();
      };

      describe('within pipeline table', () => {
        beforeEach(() => {
          createComponent({ type: 'PIPELINES_TABLE' });
        });

        it('emits `refreshPipelinesTable` event when `pipelineActionRequestComplete` is triggered', async () => {
          await clickCiAction();

          expect(eventHub.$emit).toHaveBeenCalledWith('refreshPipelinesTable');
        });
      });

      describe('in MR widget', () => {
        beforeEach(() => {
          jest.spyOn($.fn, 'dropdown');
          createComponent();
        });

        it('closes the dropdown when `pipelineActionRequestComplete` is triggered', async () => {
          const hidden = jest.fn();

          wrapper.vm.$root.$on('bv::dropdown::hide', hidden);

          expect(hidden).toHaveBeenCalledTimes(0);

          await clickCiAction();

          expect(hidden).toHaveBeenCalledTimes(1);
        });
      });
    });
  });
});