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

related_issues_root_spec.js « components « related_issues « issuable « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3099e0b639b9df7b0a8a906dd00272b0b3dd2be4 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { mount, shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises';
import {
  defaultProps,
  issuable1,
  issuable2,
} from 'jest/vue_shared/components/issue/related_issuable_mock_data';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import RelatedIssuesRoot from '~/related_issues/components/related_issues_root.vue';
import { linkedIssueTypesMap } from '~/related_issues/constants';
import relatedIssuesService from '~/related_issues/services/related_issues_service';

jest.mock('~/flash');

describe('RelatedIssuesRoot', () => {
  let wrapper;
  let mock;

  beforeEach(() => {
    mock = new MockAdapter(axios);
    mock.onGet(defaultProps.endpoint).reply(200, []);
  });

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

  const createComponent = (mountFn = mount) => {
    wrapper = mountFn(RelatedIssuesRoot, {
      propsData: defaultProps,
    });

    // Wait for fetch request `fetchRelatedIssues` to complete before starting to test
    return waitForPromises();
  };

  describe('methods', () => {
    describe('onRelatedIssueRemoveRequest', () => {
      beforeEach(() => {
        jest
          .spyOn(relatedIssuesService.prototype, 'fetchRelatedIssues')
          .mockReturnValue(Promise.reject());

        return createComponent().then(() => {
          wrapper.vm.store.setRelatedIssues([issuable1]);
        });
      });

      it('remove related issue and succeeds', () => {
        mock.onDelete(issuable1.referencePath).reply(200, { issues: [] });

        wrapper.vm.onRelatedIssueRemoveRequest(issuable1.id);

        return axios.waitForAll().then(() => {
          expect(wrapper.vm.state.relatedIssues).toEqual([]);
        });
      });

      it('remove related issue, fails, and restores to related issues', () => {
        mock.onDelete(issuable1.referencePath).reply(422, {});

        wrapper.vm.onRelatedIssueRemoveRequest(issuable1.id);

        return axios.waitForAll().then(() => {
          expect(wrapper.vm.state.relatedIssues).toHaveLength(1);
          expect(wrapper.vm.state.relatedIssues[0].id).toEqual(issuable1.id);
        });
      });
    });

    describe('onToggleAddRelatedIssuesForm', () => {
      beforeEach(() => createComponent(shallowMount));

      it('toggle related issues form to visible', () => {
        wrapper.vm.onToggleAddRelatedIssuesForm();

        expect(wrapper.vm.isFormVisible).toEqual(true);
      });

      it('show add related issues form to hidden', () => {
        wrapper.vm.isFormVisible = true;

        wrapper.vm.onToggleAddRelatedIssuesForm();

        expect(wrapper.vm.isFormVisible).toEqual(false);
      });
    });

    describe('onPendingIssueRemoveRequest', () => {
      beforeEach(() =>
        createComponent().then(() => {
          wrapper.vm.store.setPendingReferences([issuable1.reference]);
        }),
      );

      it('remove pending related issue', () => {
        expect(wrapper.vm.state.pendingReferences).toHaveLength(1);

        wrapper.vm.onPendingIssueRemoveRequest(0);

        expect(wrapper.vm.state.pendingReferences).toHaveLength(0);
      });
    });

    describe('onPendingFormSubmit', () => {
      beforeEach(() => {
        jest
          .spyOn(relatedIssuesService.prototype, 'fetchRelatedIssues')
          .mockReturnValue(Promise.reject());

        return createComponent().then(() => {
          jest.spyOn(wrapper.vm, 'processAllReferences');
          jest.spyOn(wrapper.vm.service, 'addRelatedIssues');
          createFlash.mockClear();
        });
      });

      it('processes references before submitting', () => {
        const input = '#123';
        const linkedIssueType = linkedIssueTypesMap.RELATES_TO;
        const emitObj = {
          pendingReferences: input,
          linkedIssueType,
        };

        wrapper.vm.onPendingFormSubmit(emitObj);

        expect(wrapper.vm.processAllReferences).toHaveBeenCalledWith(input);
        expect(wrapper.vm.service.addRelatedIssues).toHaveBeenCalledWith([input], linkedIssueType);
      });

      it('submit zero pending issue as related issue', () => {
        wrapper.vm.store.setPendingReferences([]);
        wrapper.vm.onPendingFormSubmit({});

        return waitForPromises().then(() => {
          expect(wrapper.vm.state.pendingReferences).toHaveLength(0);
          expect(wrapper.vm.state.relatedIssues).toHaveLength(0);
        });
      });

      it('submit pending issue as related issue', () => {
        mock.onPost(defaultProps.endpoint).reply(200, {
          issuables: [issuable1],
          result: {
            message: 'something was successfully related',
            status: 'success',
          },
        });

        wrapper.vm.store.setPendingReferences([issuable1.reference]);
        wrapper.vm.onPendingFormSubmit({});

        return waitForPromises().then(() => {
          expect(wrapper.vm.state.pendingReferences).toHaveLength(0);
          expect(wrapper.vm.state.relatedIssues).toHaveLength(1);
          expect(wrapper.vm.state.relatedIssues[0].id).toEqual(issuable1.id);
        });
      });

      it('submit multiple pending issues as related issues', () => {
        mock.onPost(defaultProps.endpoint).reply(200, {
          issuables: [issuable1, issuable2],
          result: {
            message: 'something was successfully related',
            status: 'success',
          },
        });

        wrapper.vm.store.setPendingReferences([issuable1.reference, issuable2.reference]);
        wrapper.vm.onPendingFormSubmit({});

        return waitForPromises().then(() => {
          expect(wrapper.vm.state.pendingReferences).toHaveLength(0);
          expect(wrapper.vm.state.relatedIssues).toHaveLength(2);
          expect(wrapper.vm.state.relatedIssues[0].id).toEqual(issuable1.id);
          expect(wrapper.vm.state.relatedIssues[1].id).toEqual(issuable2.id);
        });
      });

      it('displays a message from the backend upon error', () => {
        const input = '#123';
        const message = 'error';

        mock.onPost(defaultProps.endpoint).reply(409, { message });
        wrapper.vm.store.setPendingReferences([issuable1.reference, issuable2.reference]);

        expect(createFlash).not.toHaveBeenCalled();
        wrapper.vm.onPendingFormSubmit(input);

        return waitForPromises().then(() => {
          expect(createFlash).toHaveBeenCalledWith({
            message,
          });
        });
      });
    });

    describe('onPendingFormCancel', () => {
      beforeEach(() =>
        createComponent().then(() => {
          wrapper.vm.isFormVisible = true;
          wrapper.vm.inputValue = 'foo';
        }),
      );

      it('when canceling and hiding add issuable form', () => {
        wrapper.vm.onPendingFormCancel();

        return wrapper.vm.$nextTick().then(() => {
          expect(wrapper.vm.isFormVisible).toEqual(false);
          expect(wrapper.vm.inputValue).toEqual('');
          expect(wrapper.vm.state.pendingReferences).toHaveLength(0);
        });
      });
    });

    describe('fetchRelatedIssues', () => {
      beforeEach(() => createComponent());

      it('sets isFetching while fetching', () => {
        wrapper.vm.fetchRelatedIssues();

        expect(wrapper.vm.isFetching).toEqual(true);

        return waitForPromises().then(() => {
          expect(wrapper.vm.isFetching).toEqual(false);
        });
      });

      it('should fetch related issues', () => {
        mock.onGet(defaultProps.endpoint).reply(200, [issuable1, issuable2]);

        wrapper.vm.fetchRelatedIssues();

        return waitForPromises().then(() => {
          expect(wrapper.vm.state.relatedIssues).toHaveLength(2);
          expect(wrapper.vm.state.relatedIssues[0].id).toEqual(issuable1.id);
          expect(wrapper.vm.state.relatedIssues[1].id).toEqual(issuable2.id);
        });
      });
    });

    describe('onInput', () => {
      beforeEach(() => createComponent());

      it('fill in issue number reference and adds to pending related issues', () => {
        const input = '#123 ';
        wrapper.vm.onInput({
          untouchedRawReferences: [input.trim()],
          touchedReference: input,
        });

        expect(wrapper.vm.state.pendingReferences).toHaveLength(1);
        expect(wrapper.vm.state.pendingReferences[0]).toEqual('#123');
      });

      it('fill in with full reference', () => {
        const input = 'asdf/qwer#444 ';
        wrapper.vm.onInput({ untouchedRawReferences: [input.trim()], touchedReference: input });

        expect(wrapper.vm.state.pendingReferences).toHaveLength(1);
        expect(wrapper.vm.state.pendingReferences[0]).toEqual('asdf/qwer#444');
      });

      it('fill in with issue link', () => {
        const link = 'http://localhost:3000/foo/bar/issues/111';
        const input = `${link} `;
        wrapper.vm.onInput({ untouchedRawReferences: [input.trim()], touchedReference: input });

        expect(wrapper.vm.state.pendingReferences).toHaveLength(1);
        expect(wrapper.vm.state.pendingReferences[0]).toEqual(link);
      });

      it('fill in with multiple references', () => {
        const input = 'asdf/qwer#444 #12 ';
        wrapper.vm.onInput({
          untouchedRawReferences: input.trim().split(/\s/),
          touchedReference: '2',
        });

        expect(wrapper.vm.state.pendingReferences).toHaveLength(2);
        expect(wrapper.vm.state.pendingReferences[0]).toEqual('asdf/qwer#444');
        expect(wrapper.vm.state.pendingReferences[1]).toEqual('#12');
      });

      it('fill in with some invalid things', () => {
        const input = 'something random ';
        wrapper.vm.onInput({
          untouchedRawReferences: input.trim().split(/\s/),
          touchedReference: '2',
        });

        expect(wrapper.vm.state.pendingReferences).toHaveLength(2);
        expect(wrapper.vm.state.pendingReferences[0]).toEqual('something');
        expect(wrapper.vm.state.pendingReferences[1]).toEqual('random');
      });

      it('prepends # when user enters a numeric value [0-9]', async () => {
        const input = '23';

        wrapper.vm.onInput({
          untouchedRawReferences: input.trim().split(/\s/),
          touchedReference: input,
        });

        expect(wrapper.vm.inputValue).toBe(`#${input}`);
      });

      it('prepends # when user enters a number', async () => {
        const input = 23;

        wrapper.vm.onInput({
          untouchedRawReferences: String(input).trim().split(/\s/),
          touchedReference: input,
        });

        expect(wrapper.vm.inputValue).toBe(`#${input}`);
      });
    });

    describe('onBlur', () => {
      beforeEach(() =>
        createComponent().then(() => {
          jest.spyOn(wrapper.vm, 'processAllReferences').mockImplementation(() => {});
        }),
      );

      it('add any references to pending when blurring', () => {
        const input = '#123';

        wrapper.vm.onBlur(input);

        expect(wrapper.vm.processAllReferences).toHaveBeenCalledWith(input);
      });
    });

    describe('processAllReferences', () => {
      beforeEach(() => createComponent());

      it('add valid reference to pending', () => {
        const input = '#123';
        wrapper.vm.processAllReferences(input);

        expect(wrapper.vm.state.pendingReferences).toHaveLength(1);
        expect(wrapper.vm.state.pendingReferences[0]).toEqual('#123');
      });

      it('add any valid references to pending', () => {
        const input = 'asdf #123';
        wrapper.vm.processAllReferences(input);

        expect(wrapper.vm.state.pendingReferences).toHaveLength(2);
        expect(wrapper.vm.state.pendingReferences[0]).toEqual('asdf');
        expect(wrapper.vm.state.pendingReferences[1]).toEqual('#123');
      });
    });
  });
});