summaryrefslogtreecommitdiff
path: root/spec/frontend/issuable/related_issues/components/add_issuable_form_spec.js
blob: 17a195df494e8e7affea6323d5d9f68d732db79f (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
import { mount, shallowMount } from '@vue/test-utils';
import { issuableTypesMap, linkedIssueTypesMap, PathIdSeparator } from '~/related_issues/constants';
import AddIssuableForm from '~/related_issues/components/add_issuable_form.vue';

const issuable1 = {
  id: 200,
  reference: 'foo/bar#123',
  displayReference: '#123',
  title: 'some title',
  path: '/foo/bar/issues/123',
  state: 'opened',
};

const issuable2 = {
  id: 201,
  reference: 'foo/bar#124',
  displayReference: '#124',
  title: 'some other thing',
  path: '/foo/bar/issues/124',
  state: 'opened',
};

const pathIdSeparator = PathIdSeparator.Issue;

const findFormInput = wrapper => wrapper.find('.js-add-issuable-form-input').element;

const findRadioInput = (inputs, value) => inputs.filter(input => input.element.value === value)[0];

const findRadioInputs = wrapper => wrapper.findAll('[name="linked-issue-type-radio"]');

const constructWrapper = props => {
  return shallowMount(AddIssuableForm, {
    propsData: {
      inputValue: '',
      pendingReferences: [],
      pathIdSeparator,
      ...props,
    },
  });
};

describe('AddIssuableForm', () => {
  let wrapper;

  afterEach(() => {
    // Jest doesn't blur an item even if it is destroyed,
    // so blur the input manually after each test
    const input = findFormInput(wrapper);
    if (input) input.blur();

    if (wrapper) {
      wrapper.destroy();
      wrapper = null;
    }
  });

  describe('with data', () => {
    describe('without references', () => {
      describe('without any input text', () => {
        beforeEach(() => {
          wrapper = shallowMount(AddIssuableForm, {
            propsData: {
              inputValue: '',
              pendingReferences: [],
              pathIdSeparator,
            },
          });
        });

        it('should have disabled submit button', () => {
          expect(wrapper.vm.$refs.addButton.disabled).toBe(true);
          expect(wrapper.vm.$refs.loadingIcon).toBeUndefined();
        });
      });

      describe('with input text', () => {
        beforeEach(() => {
          wrapper = shallowMount(AddIssuableForm, {
            propsData: {
              inputValue: 'foo',
              pendingReferences: [],
              pathIdSeparator,
            },
          });
        });

        it('should not have disabled submit button', () => {
          expect(wrapper.vm.$refs.addButton.disabled).toBe(false);
        });
      });
    });

    describe('with references', () => {
      const inputValue = 'foo #123';

      beforeEach(() => {
        wrapper = mount(AddIssuableForm, {
          propsData: {
            inputValue,
            pendingReferences: [issuable1.reference, issuable2.reference],
            pathIdSeparator,
          },
        });
      });

      it('should put input value in place', () => {
        expect(findFormInput(wrapper).value).toEqual(inputValue);
      });

      it('should render pending issuables items', () => {
        expect(wrapper.findAll('.js-add-issuable-form-token-list-item').length).toEqual(2);
      });

      it('should not have disabled submit button', () => {
        expect(wrapper.vm.$refs.addButton.disabled).toBe(false);
      });
    });

    describe('when issuable type is "issue"', () => {
      beforeEach(() => {
        wrapper = mount(AddIssuableForm, {
          propsData: {
            inputValue: '',
            issuableType: issuableTypesMap.ISSUE,
            pathIdSeparator,
            pendingReferences: [],
          },
        });
      });

      it('does not show radio inputs', () => {
        expect(findRadioInputs(wrapper).length).toBe(0);
      });
    });

    describe('when issuable type is "epic"', () => {
      beforeEach(() => {
        wrapper = shallowMount(AddIssuableForm, {
          propsData: {
            inputValue: '',
            issuableType: issuableTypesMap.EPIC,
            pathIdSeparator,
            pendingReferences: [],
          },
        });
      });

      it('does not show radio inputs', () => {
        expect(findRadioInputs(wrapper).length).toBe(0);
      });
    });

    describe('when it is a Linked Issues form', () => {
      beforeEach(() => {
        wrapper = mount(AddIssuableForm, {
          propsData: {
            inputValue: '',
            showCategorizedIssues: true,
            issuableType: issuableTypesMap.ISSUE,
            pathIdSeparator,
            pendingReferences: [],
          },
        });
      });

      it('shows radio inputs to allow categorisation of blocking issues', () => {
        expect(findRadioInputs(wrapper).length).toBeGreaterThan(0);
      });

      describe('form radio buttons', () => {
        let radioInputs;

        beforeEach(() => {
          radioInputs = findRadioInputs(wrapper);
        });

        it('shows "relates to" option', () => {
          expect(findRadioInput(radioInputs, linkedIssueTypesMap.RELATES_TO)).not.toBeNull();
        });

        it('shows "blocks" option', () => {
          expect(findRadioInput(radioInputs, linkedIssueTypesMap.BLOCKS)).not.toBeNull();
        });

        it('shows "is blocked by" option', () => {
          expect(findRadioInput(radioInputs, linkedIssueTypesMap.IS_BLOCKED_BY)).not.toBeNull();
        });

        it('shows 3 options in total', () => {
          expect(radioInputs.length).toBe(3);
        });
      });

      describe('when the form is submitted', () => {
        it('emits an event with a "relates_to" link type when the "relates to" radio input selected', done => {
          jest.spyOn(wrapper.vm, '$emit').mockImplementation(() => {});

          wrapper.vm.linkedIssueType = linkedIssueTypesMap.RELATES_TO;
          wrapper.vm.onFormSubmit();

          wrapper.vm.$nextTick(() => {
            expect(wrapper.vm.$emit).toHaveBeenCalledWith('addIssuableFormSubmit', {
              pendingReferences: '',
              linkedIssueType: linkedIssueTypesMap.RELATES_TO,
            });
            done();
          });
        });

        it('emits an event with a "blocks" link type when the "blocks" radio input selected', done => {
          jest.spyOn(wrapper.vm, '$emit').mockImplementation(() => {});

          wrapper.vm.linkedIssueType = linkedIssueTypesMap.BLOCKS;
          wrapper.vm.onFormSubmit();

          wrapper.vm.$nextTick(() => {
            expect(wrapper.vm.$emit).toHaveBeenCalledWith('addIssuableFormSubmit', {
              pendingReferences: '',
              linkedIssueType: linkedIssueTypesMap.BLOCKS,
            });
            done();
          });
        });

        it('emits an event with a "is_blocked_by" link type when the "is blocked by" radio input selected', done => {
          jest.spyOn(wrapper.vm, '$emit').mockImplementation(() => {});

          wrapper.vm.linkedIssueType = linkedIssueTypesMap.IS_BLOCKED_BY;
          wrapper.vm.onFormSubmit();

          wrapper.vm.$nextTick(() => {
            expect(wrapper.vm.$emit).toHaveBeenCalledWith('addIssuableFormSubmit', {
              pendingReferences: '',
              linkedIssueType: linkedIssueTypesMap.IS_BLOCKED_BY,
            });
            done();
          });
        });

        it('shows error message when error is present', done => {
          const itemAddFailureMessage = 'Something went wrong while submitting.';
          wrapper.setProps({
            hasError: true,
            itemAddFailureMessage,
          });

          wrapper.vm.$nextTick(() => {
            expect(wrapper.find('.gl-field-error').exists()).toBe(true);
            expect(wrapper.find('.gl-field-error').text()).toContain(itemAddFailureMessage);
            done();
          });
        });
      });
    });
  });

  describe('computed', () => {
    describe('transformedAutocompleteSources', () => {
      const autoCompleteSources = {
        issues: 'http://localhost/autocomplete/issues',
        epics: 'http://localhost/autocomplete/epics',
      };

      it('returns autocomplete object', () => {
        wrapper = constructWrapper({
          autoCompleteSources,
        });

        expect(wrapper.vm.transformedAutocompleteSources).toBe(autoCompleteSources);

        wrapper = constructWrapper({
          autoCompleteSources,
          confidential: false,
        });

        expect(wrapper.vm.transformedAutocompleteSources).toBe(autoCompleteSources);
      });

      it('returns autocomplete sources with query `confidential_only`, when it is confidential', () => {
        wrapper = constructWrapper({
          autoCompleteSources,
          confidential: true,
        });

        const actualSources = wrapper.vm.transformedAutocompleteSources;

        expect(actualSources.epics).toContain('?confidential_only=true');
        expect(actualSources.issues).toContain('?confidential_only=true');
      });
    });
  });
});