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

failed_job_details_spec.js « failure_widget « components « pipelines_page « ci « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6967a3693385abad432ae353fa091d3675caa89c (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
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { GlIcon, GlLink } from '@gitlab/ui';
import createMockApollo from 'helpers/mock_apollo_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import FailedJobDetails from '~/ci/pipelines_page/components/failure_widget/failed_job_details.vue';
import RetryMrFailedJobMutation from '~/ci/merge_requests/graphql/mutations/retry_mr_failed_job.mutation.graphql';
import { BRIDGE_KIND } from '~/ci/pipeline_details/graph/constants';
import { job } from './mock';

Vue.use(VueApollo);
jest.mock('~/alert');

const createFakeEvent = () => ({ stopPropagation: jest.fn() });

describe('FailedJobDetails component', () => {
  let wrapper;
  let mockRetryResponse;

  const retrySuccessResponse = {
    data: {
      jobRetry: {
        errors: [],
      },
    },
  };

  const defaultProps = {
    job,
  };

  const createComponent = ({ props = {} } = {}) => {
    const handlers = [[RetryMrFailedJobMutation, mockRetryResponse]];

    wrapper = shallowMountExtended(FailedJobDetails, {
      propsData: {
        ...defaultProps,
        ...props,
      },
      apolloProvider: createMockApollo(handlers),
    });
  };

  const findArrowIcon = () => wrapper.findComponent(GlIcon);
  const findJobId = () => wrapper.findComponent(GlLink);
  const findJobLog = () => wrapper.findByTestId('job-log');
  const findJobName = () => wrapper.findByText(defaultProps.job.name);
  const findRetryButton = () => wrapper.findByLabelText('Retry');
  const findRow = () => wrapper.findByTestId('widget-row');
  const findStageName = () => wrapper.findByText(defaultProps.job.stage.name);

  beforeEach(() => {
    mockRetryResponse = jest.fn();
    mockRetryResponse.mockResolvedValue(retrySuccessResponse);
  });

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

    it('renders the job name', () => {
      expect(findJobName().exists()).toBe(true);
    });

    it('renders the stage name', () => {
      expect(findStageName().exists()).toBe(true);
    });

    it('renders the job id as a link', () => {
      const jobId = getIdFromGraphQLId(defaultProps.job.id);

      expect(findJobId().exists()).toBe(true);
      expect(findJobId().text()).toContain(String(jobId));
    });

    it('does not renders the job lob', () => {
      expect(findJobLog().exists()).toBe(false);
    });
  });

  describe('Retry action', () => {
    describe('when the job is not retryable', () => {
      beforeEach(() => {
        createComponent({ props: { job: { ...job, retryable: false } } });
      });

      it('disables the retry button', () => {
        expect(findRetryButton().props().disabled).toBe(true);
      });
    });

    describe('when the job is a bridge', () => {
      beforeEach(() => {
        createComponent({ props: { job: { ...job, kind: BRIDGE_KIND } } });
      });

      it('disables the retry button', () => {
        expect(findRetryButton().props().disabled).toBe(true);
      });
    });

    describe('when the job is retryable', () => {
      describe('and user has permission to update the build', () => {
        beforeEach(() => {
          createComponent();
        });

        it('enables the retry button', () => {
          expect(findRetryButton().props().disabled).toBe(false);
        });

        describe('when clicking on the retry button', () => {
          it('passes the loading state to the button', async () => {
            await findRetryButton().vm.$emit('click', createFakeEvent());

            expect(findRetryButton().props().loading).toBe(true);
          });

          describe('and it succeeds', () => {
            beforeEach(async () => {
              findRetryButton().vm.$emit('click', createFakeEvent());
              await waitForPromises();
            });

            it('is no longer loading', () => {
              expect(findRetryButton().props().loading).toBe(false);
            });

            it('calls the retry mutation', () => {
              expect(mockRetryResponse).toHaveBeenCalled();
              expect(mockRetryResponse).toHaveBeenCalledWith({
                id: job.id,
              });
            });

            it('emits the `retried-job` event', () => {
              expect(wrapper.emitted('job-retried')).toStrictEqual([[job.name]]);
            });
          });

          describe('and it fails', () => {
            const customErrorMsg = 'Custom error message from API';

            beforeEach(async () => {
              mockRetryResponse.mockResolvedValue({
                data: { jobRetry: { errors: [customErrorMsg] } },
              });
              findRetryButton().vm.$emit('click', createFakeEvent());

              await waitForPromises();
            });

            it('shows an error message', () => {
              expect(createAlert).toHaveBeenCalledWith({ message: customErrorMsg });
            });

            it('does not emits the `refetch-jobs` event', () => {
              expect(wrapper.emitted('refetch-jobs')).toBeUndefined();
            });
          });
        });
      });

      describe('and user does not have permission to update the build', () => {
        beforeEach(() => {
          createComponent({
            props: { job: { ...job, retryable: true, userPermissions: { updateBuild: false } } },
          });
        });

        it('disables the retry button', () => {
          expect(findRetryButton().props().disabled).toBe(true);
        });
      });
    });
  });

  describe('Job log', () => {
    describe('without permissions', () => {
      beforeEach(async () => {
        createComponent({ props: { job: { ...job, userPermissions: { readBuild: false } } } });
        await findRow().trigger('click');
      });

      it('does not renders the received html of the job log', () => {
        expect(findJobLog().html()).not.toContain(defaultProps.job.trace.htmlSummary);
      });

      it('shows a permission error message', () => {
        expect(findJobLog().text()).toBe("You do not have permission to read this job's log.");
      });
    });

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

      describe('when clicking on the row', () => {
        beforeEach(async () => {
          await findRow().trigger('click');
        });

        describe('while collapsed', () => {
          it('expands the job log', () => {
            expect(findJobLog().exists()).toBe(true);
          });

          it('renders the down arrow', () => {
            expect(findArrowIcon().props().name).toBe('chevron-down');
          });

          it('renders the received html of the job log', () => {
            expect(findJobLog().html()).toContain(defaultProps.job.trace.htmlSummary);
          });
        });

        describe('while expanded', () => {
          it('collapes the job log', async () => {
            expect(findJobLog().exists()).toBe(true);

            await findRow().trigger('click');

            expect(findJobLog().exists()).toBe(false);
          });

          it('renders the right arrow', async () => {
            expect(findArrowIcon().props().name).toBe('chevron-down');

            await findRow().trigger('click');

            expect(findArrowIcon().props().name).toBe('chevron-right');
          });
        });
      });

      describe('when clicking on a link element within the row', () => {
        it('does not expands/collapse the job log', async () => {
          expect(findJobLog().exists()).toBe(false);
          expect(findArrowIcon().props().name).toBe('chevron-right');

          await findJobId().vm.$emit('click');

          expect(findJobLog().exists()).toBe(false);
          expect(findArrowIcon().props().name).toBe('chevron-right');
        });
      });
    });
  });
});