summaryrefslogtreecommitdiff
path: root/spec/frontend/jira_import/components/jira_import_form_spec.js
blob: 41d3cd46d012af6401cf5283a84bd14ac65c9855 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import {
  GlAlert,
  GlButton,
  GlDropdown,
  GlDropdownItem,
  GlFormSelect,
  GlLabel,
  GlSearchBoxByType,
  GlTableLite,
} from '@gitlab/ui';
import { getByRole } from '@testing-library/dom';
import { mount, shallowMount } from '@vue/test-utils';
import AxiosMockAdapter from 'axios-mock-adapter';
import JiraImportForm from '~/jira_import/components/jira_import_form.vue';
import getJiraUserMappingMutation from '~/jira_import/queries/get_jira_user_mapping.mutation.graphql';
import initiateJiraImportMutation from '~/jira_import/queries/initiate_jira_import.mutation.graphql';
import searchProjectMembersQuery from '~/jira_import/queries/search_project_members.query.graphql';
import axios from '~/lib/utils/axios_utils';
import {
  imports,
  issuesPath,
  jiraProjects,
  jiraUsersResponse,
  projectId,
  projectPath,
  userMappings as defaultUserMappings,
} from '../mock_data';

describe('JiraImportForm', () => {
  let axiosMock;
  let mutateSpy;
  let querySpy;
  let wrapper;

  const currentUsername = 'mrgitlab';

  const getAlert = () => wrapper.findComponent(GlAlert);

  const getSelectDropdown = () => wrapper.findComponent(GlFormSelect);

  const getContinueButton = () => wrapper.findComponent(GlButton);

  const getCancelButton = () => wrapper.findAllComponents(GlButton).at(1);

  const getLabel = () => wrapper.findComponent(GlLabel);

  const getTable = () => wrapper.findComponent(GlTableLite);

  const getUserDropdown = () => getTable().findComponent(GlDropdown);

  const getHeader = (name) => getByRole(wrapper.element, 'columnheader', { name });

  const findLoadMoreUsersButton = () => wrapper.find('[data-testid="load-more-users-button"]');

  const mountComponent = ({
    hasMoreUsers = false,
    isSubmitting = false,
    loading = false,
    mutate = mutateSpy,
    selectedProject = 'MTG',
    userMappings = defaultUserMappings,
    mountFunction = shallowMount,
  } = {}) =>
    mountFunction(JiraImportForm, {
      propsData: {
        issuesPath,
        jiraImports: imports,
        jiraProjects,
        projectId,
        projectPath,
      },
      data: () => ({
        hasMoreUsers,
        isFetching: false,
        isSubmitting,
        searchTerm: '',
        selectedProject,
        selectState: null,
        users: [],
        userMappings,
      }),
      mocks: {
        $apollo: {
          loading,
          mutate,
          query: querySpy,
        },
      },
      currentUsername,
    });

  beforeEach(() => {
    axiosMock = new AxiosMockAdapter(axios);
    mutateSpy = jest.fn().mockResolvedValue({
      data: {
        jiraImportStart: { errors: [] },
        jiraImportUsers: { jiraUsers: [], errors: [] },
      },
    });
    querySpy = jest.fn().mockResolvedValue({
      data: { project: { projectMembers: { nodes: [] } } },
    });
  });

  afterEach(() => {
    axiosMock.restore();
    mutateSpy.mockRestore();
    querySpy.mockRestore();
    wrapper.destroy();
  });

  describe('select dropdown project selection', () => {
    it('is shown', () => {
      wrapper = mountComponent();

      expect(getSelectDropdown().exists()).toBe(true);
    });

    it('contains a list of Jira projects to select from', () => {
      wrapper = mountComponent({ mountFunction: mount });

      getSelectDropdown()
        .findAll('option')
        .wrappers.forEach((optionEl, index) => {
          expect(optionEl.text()).toBe(jiraProjects[index].text);
        });
    });

    describe('when selected project has been imported before', () => {
      it('shows jira-import::MTG-3 label since project MTG has been imported 2 time before', () => {
        wrapper = mountComponent();

        expect(getLabel().props('title')).toBe('jira-import::MTG-3');
      });

      it('shows warning alert to explain project MTG has been imported 2 times before', () => {
        wrapper = mountComponent({ mountFunction: mount });

        expect(getAlert().text()).toBe(
          'You have imported from this project 2 times before. Each new import will create duplicate issues.',
        );
      });
    });

    describe('when selected project has not been imported before', () => {
      beforeEach(() => {
        wrapper = mountComponent({ selectedProject: 'MJP' });
      });

      it('shows jira-import::MJP-1 label since project MJP has not been imported before', () => {
        expect(getLabel().props('title')).toBe('jira-import::MJP-1');
      });

      it('does not show warning alert since project MJP has not been imported before', () => {
        expect(getAlert().exists()).toBe(false);
      });
    });
  });

  describe('form information', () => {
    beforeEach(() => {
      wrapper = mountComponent();
    });

    it('shows a heading for the user mapping section', () => {
      expect(
        getByRole(wrapper.element, 'heading', { name: 'Jira-GitLab user mapping template' }),
      ).toBeTruthy();
    });

    it('shows information to the user', () => {
      expect(wrapper.find('p').text()).toBe(
        'Jira users have been imported from the configured Jira instance. They can be mapped by selecting a GitLab user from the dropdown in the "GitLab username" column. When the form appears, the dropdown defaults to the user conducting the import.',
      );
    });
  });

  describe('table', () => {
    describe('headers', () => {
      beforeEach(() => {
        wrapper = mountComponent({ mountFunction: mount });
      });

      it('has a "Jira display name" column', () => {
        expect(getHeader('Jira display name')).toBeTruthy();
      });

      it('has an "arrow" column', () => {
        expect(getHeader('Arrow')).toBeTruthy();
      });

      it('has a "GitLab username" column', () => {
        expect(getHeader('GitLab username')).toBeTruthy();
      });
    });

    describe('body', () => {
      it('shows all user mappings', () => {
        wrapper = mountComponent({ mountFunction: mount });

        expect(getTable().findAll('tbody tr')).toHaveLength(2);
      });

      it('shows correct information in each cell', () => {
        wrapper = mountComponent({ mountFunction: mount });

        expect(getTable().element).toMatchSnapshot();
      });

      describe('when there is no Jira->GitLab user mapping', () => {
        it('shows the logged in user in the dropdown', () => {
          wrapper = mountComponent({
            mountFunction: mount,
            userMappings: [
              {
                jiraAccountId: 'aei23f98f-q23fj98qfj',
                jiraDisplayName: 'Jane Doe',
                jiraEmail: 'janedoe@example.com',
                gitlabId: undefined,
                gitlabUsername: undefined,
              },
            ],
          });

          expect(getUserDropdown().text()).toContain(currentUsername);
        });
      });

      describe('when there is a Jira->GitLab user mapping', () => {
        it('shows the mapped user in the dropdown', () => {
          const gitlabUsername = 'mai';

          wrapper = mountComponent({
            mountFunction: mount,
            userMappings: [
              {
                jiraAccountId: 'aei23f98f-q23fj98qfj',
                jiraDisplayName: 'Jane Doe',
                jiraEmail: 'janedoe@example.com',
                gitlabId: 14,
                gitlabUsername,
              },
            ],
          });

          expect(getUserDropdown().text()).toContain(gitlabUsername);
        });
      });
    });
  });

  describe('member search', () => {
    describe('when searching for a member', () => {
      beforeEach(() => {
        querySpy = jest.fn().mockResolvedValue({
          data: {
            project: {
              projectMembers: {
                nodes: [
                  {
                    user: {
                      id: 7,
                      name: 'Frederic Chopin',
                      username: 'fchopin',
                    },
                  },
                ],
              },
            },
          },
        });

        wrapper = mountComponent({ mountFunction: mount });

        wrapper.findComponent(GlSearchBoxByType).vm.$emit('input', 'fred');
      });

      it('makes a GraphQL call', () => {
        const queryArgument = {
          query: searchProjectMembersQuery,
          variables: {
            fullPath: projectPath,
            search: 'fred',
          },
        };

        expect(querySpy).toHaveBeenCalledWith(expect.objectContaining(queryArgument));
      });

      it('updates the user list', () => {
        expect(getUserDropdown().findAll(GlDropdownItem)).toHaveLength(1);
        expect(getUserDropdown().find(GlDropdownItem).text()).toContain(
          'fchopin (Frederic Chopin)',
        );
      });
    });
  });

  describe('buttons', () => {
    describe('"Continue" button', () => {
      it('is shown', () => {
        wrapper = mountComponent();

        expect(getContinueButton().text()).toBe('Continue');
      });

      it('is in loading state when the form is submitting', async () => {
        wrapper = mountComponent({ isSubmitting: true });

        expect(getContinueButton().props('loading')).toBe(true);
      });
    });

    describe('"Cancel" button', () => {
      beforeEach(() => {
        wrapper = mountComponent();
      });

      it('is shown', () => {
        expect(getCancelButton().text()).toBe('Cancel');
      });

      it('links to the Issues page', () => {
        expect(getCancelButton().attributes('href')).toBe(issuesPath);
      });
    });
  });

  describe('submitting the form', () => {
    it('initiates the Jira import mutation with the expected arguments', () => {
      wrapper = mountComponent();

      const mutationArguments = {
        mutation: initiateJiraImportMutation,
        variables: {
          input: {
            jiraProjectKey: 'MTG',
            projectPath,
            usersMapping: [
              {
                jiraAccountId: 'aei23f98f-q23fj98qfj',
                gitlabId: 15,
              },
              {
                jiraAccountId: 'fu39y8t34w-rq3u289t3h4i',
                gitlabId: undefined,
              },
            ],
          },
        },
      };

      wrapper.find('form').trigger('submit');

      expect(mutateSpy).toHaveBeenCalledWith(expect.objectContaining(mutationArguments));
    });
  });

  describe('on mount GraphQL user mapping mutation', () => {
    it('is called with the expected arguments', () => {
      wrapper = mountComponent();

      const mutationArguments = {
        mutation: getJiraUserMappingMutation,
        variables: {
          input: {
            projectPath,
            startAt: 0,
          },
        },
      };

      expect(mutateSpy).toHaveBeenCalledWith(expect.objectContaining(mutationArguments));
    });

    describe('when there is an error when called', () => {
      beforeEach(() => {
        const mutate = jest.fn(() => Promise.reject());
        wrapper = mountComponent({ mutate });
      });

      it('shows error message', () => {
        expect(getAlert().exists()).toBe(true);
      });
    });
  });

  describe('load more users button', () => {
    describe('when all users have been loaded', () => {
      it('is not shown', () => {
        wrapper = mountComponent();

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

    describe('when all users have not been loaded', () => {
      it('is shown', () => {
        wrapper = mountComponent({ hasMoreUsers: true });

        expect(findLoadMoreUsersButton().exists()).toBe(true);
      });
    });

    describe('when clicked', () => {
      beforeEach(() => {
        mutateSpy = jest.fn(() =>
          Promise.resolve({
            data: {
              jiraImportStart: { errors: [] },
              jiraImportUsers: { jiraUsers: jiraUsersResponse, errors: [] },
            },
          }),
        );

        wrapper = mountComponent({ hasMoreUsers: true });
      });

      it('calls the GraphQL user mapping mutation', async () => {
        const mutationArguments = {
          mutation: getJiraUserMappingMutation,
          variables: {
            input: {
              projectPath,
              startAt: 0,
            },
          },
        };

        findLoadMoreUsersButton().vm.$emit('click');

        expect(mutateSpy).toHaveBeenCalledWith(expect.objectContaining(mutationArguments));
      });
    });
  });
});