summaryrefslogtreecommitdiff
path: root/spec/frontend/import_entities/import_groups/components/import_table_row_spec.js
blob: aa6a40cad18a4fd8da1c11fc0f63a63edff1c34b (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
import { GlButton, GlDropdown, GlDropdownItem, GlLink, GlFormInput } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import { STATUSES } from '~/import_entities/constants';
import ImportTableRow from '~/import_entities/import_groups/components/import_table_row.vue';
import addValidationErrorMutation from '~/import_entities/import_groups/graphql/mutations/add_validation_error.mutation.graphql';
import removeValidationErrorMutation from '~/import_entities/import_groups/graphql/mutations/remove_validation_error.mutation.graphql';
import groupAndProjectQuery from '~/import_entities/import_groups/graphql/queries/groupAndProject.query.graphql';
import { availableNamespacesFixture } from '../graphql/fixtures';

Vue.use(VueApollo);

const { i18n: I18N } = ImportTableRow;

const getFakeGroup = (status) => ({
  web_url: 'https://fake.host/',
  full_path: 'fake_group_1',
  full_name: 'fake_name_1',
  import_target: {
    target_namespace: 'root',
    new_name: 'group1',
  },
  id: 1,
  validation_errors: [],
  progress: { status },
});

const EXISTING_GROUP_TARGET_NAMESPACE = 'existing-group';
const EXISTING_GROUP_PATH = 'existing-path';
const EXISTING_PROJECT_PATH = 'existing-project-path';

describe('import table row', () => {
  let wrapper;
  let apolloProvider;
  let group;

  const findByText = (cmp, text) => {
    return wrapper.findAll(cmp).wrappers.find((node) => node.text().indexOf(text) === 0);
  };
  const findImportButton = () => findByText(GlButton, 'Import');
  const findNameInput = () => wrapper.find(GlFormInput);
  const findNamespaceDropdown = () => wrapper.find(GlDropdown);

  const createComponent = (props) => {
    apolloProvider = createMockApollo([
      [
        groupAndProjectQuery,
        ({ fullPath }) => {
          const existingGroup =
            fullPath === `${EXISTING_GROUP_TARGET_NAMESPACE}/${EXISTING_GROUP_PATH}`
              ? { id: 1 }
              : null;

          const existingProject =
            fullPath === `${EXISTING_GROUP_TARGET_NAMESPACE}/${EXISTING_PROJECT_PATH}`
              ? { id: 1 }
              : null;

          return Promise.resolve({ data: { existingGroup, existingProject } });
        },
      ],
    ]);

    wrapper = shallowMount(ImportTableRow, {
      apolloProvider,
      propsData: {
        availableNamespaces: availableNamespacesFixture,
        groupPathRegex: /.*/,
        ...props,
      },
    });
  };

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

  describe('events', () => {
    beforeEach(() => {
      group = getFakeGroup(STATUSES.NONE);
      createComponent({ group });
    });

    it.each`
      selector            | sourceEvent | payload      | event
      ${findNameInput}    | ${'input'}  | ${'demo'}    | ${'update-new-name'}
      ${findImportButton} | ${'click'}  | ${undefined} | ${'import-group'}
    `('invokes $event', ({ selector, sourceEvent, payload, event }) => {
      selector().vm.$emit(sourceEvent, payload);
      expect(wrapper.emitted(event)).toBeDefined();
      expect(wrapper.emitted(event)[0][0]).toBe(payload);
    });

    it('emits update-target-namespace when dropdown option is clicked', () => {
      const dropdownItem = findNamespaceDropdown().findAllComponents(GlDropdownItem).at(2);
      const dropdownItemText = dropdownItem.text();

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

      expect(wrapper.emitted('update-target-namespace')).toBeDefined();
      expect(wrapper.emitted('update-target-namespace')[0][0]).toBe(dropdownItemText);
    });
  });

  describe('when entity status is NONE', () => {
    beforeEach(() => {
      group = getFakeGroup(STATUSES.NONE);
      createComponent({ group });
    });

    it('renders Import button', () => {
      expect(findByText(GlButton, 'Import').exists()).toBe(true);
    });

    it('renders namespace dropdown as not disabled', () => {
      expect(findNamespaceDropdown().attributes('disabled')).toBe(undefined);
    });
  });

  it('renders only no parent option if available namespaces list is empty', () => {
    createComponent({
      group: getFakeGroup(STATUSES.NONE),
      availableNamespaces: [],
    });

    const items = findNamespaceDropdown()
      .findAllComponents(GlDropdownItem)
      .wrappers.map((w) => w.text());

    expect(items[0]).toBe('No parent');
    expect(items).toHaveLength(1);
  });

  it('renders both no parent option and available namespaces list when available namespaces list is not empty', () => {
    createComponent({
      group: getFakeGroup(STATUSES.NONE),
      availableNamespaces: availableNamespacesFixture,
    });

    const [firstItem, ...rest] = findNamespaceDropdown()
      .findAllComponents(GlDropdownItem)
      .wrappers.map((w) => w.text());

    expect(firstItem).toBe('No parent');
    expect(rest).toHaveLength(availableNamespacesFixture.length);
  });

  describe('when entity status is SCHEDULING', () => {
    beforeEach(() => {
      group = getFakeGroup(STATUSES.SCHEDULING);
      createComponent({ group });
    });

    it('does not render Import button', () => {
      expect(findByText(GlButton, 'Import')).toBe(undefined);
    });

    it('renders namespace dropdown as disabled', () => {
      expect(findNamespaceDropdown().attributes('disabled')).toBe('true');
    });
  });

  describe('when entity status is FINISHED', () => {
    beforeEach(() => {
      group = getFakeGroup(STATUSES.FINISHED);
      createComponent({ group });
    });

    it('does not render Import button', () => {
      expect(findByText(GlButton, 'Import')).toBe(undefined);
    });

    it('does not render namespace dropdown', () => {
      expect(findNamespaceDropdown().exists()).toBe(false);
    });

    it('renders target as link', () => {
      const TARGET_LINK = `${group.import_target.target_namespace}/${group.import_target.new_name}`;
      expect(findByText(GlLink, TARGET_LINK).exists()).toBe(true);
    });
  });

  describe('validations', () => {
    it('reports invalid group name when name is not matching regex', () => {
      createComponent({
        group: {
          ...getFakeGroup(STATUSES.NONE),
          import_target: {
            target_namespace: 'root',
            new_name: 'very`bad`name',
          },
        },
        groupPathRegex: /^[a-zA-Z]+$/,
      });

      expect(wrapper.text()).toContain('Please choose a group URL with no special characters.');
    });

    it('reports invalid group name if relevant validation error exists', async () => {
      const FAKE_ERROR_MESSAGE = 'fake error';

      createComponent({
        group: {
          ...getFakeGroup(STATUSES.NONE),
          validation_errors: [
            {
              field: 'new_name',
              message: FAKE_ERROR_MESSAGE,
            },
          ],
        },
      });

      jest.runOnlyPendingTimers();
      await nextTick();

      expect(wrapper.text()).toContain(FAKE_ERROR_MESSAGE);
    });

    it('sets validation error when targetting existing group', async () => {
      const testGroup = getFakeGroup(STATUSES.NONE);

      createComponent({
        group: {
          ...testGroup,
          import_target: {
            target_namespace: EXISTING_GROUP_TARGET_NAMESPACE,
            new_name: EXISTING_GROUP_PATH,
          },
        },
      });

      jest.spyOn(wrapper.vm.$apollo, 'mutate');

      jest.runOnlyPendingTimers();
      await nextTick();

      expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
        mutation: addValidationErrorMutation,
        variables: {
          field: 'new_name',
          message: I18N.NAME_ALREADY_EXISTS,
          sourceGroupId: testGroup.id,
        },
      });
    });

    it('sets validation error when targetting existing project', async () => {
      const testGroup = getFakeGroup(STATUSES.NONE);

      createComponent({
        group: {
          ...testGroup,
          import_target: {
            target_namespace: EXISTING_GROUP_TARGET_NAMESPACE,
            new_name: EXISTING_PROJECT_PATH,
          },
        },
      });

      jest.spyOn(wrapper.vm.$apollo, 'mutate');

      jest.runOnlyPendingTimers();
      await nextTick();

      expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
        mutation: addValidationErrorMutation,
        variables: {
          field: 'new_name',
          message: I18N.NAME_ALREADY_EXISTS,
          sourceGroupId: testGroup.id,
        },
      });
    });

    it('clears validation error when target is updated', async () => {
      const testGroup = getFakeGroup(STATUSES.NONE);

      createComponent({
        group: {
          ...testGroup,
          import_target: {
            target_namespace: EXISTING_GROUP_TARGET_NAMESPACE,
            new_name: EXISTING_PROJECT_PATH,
          },
        },
      });

      jest.runOnlyPendingTimers();
      await nextTick();

      jest.spyOn(wrapper.vm.$apollo, 'mutate');

      await wrapper.setProps({
        group: {
          ...testGroup,
          import_target: {
            target_namespace: 'valid_namespace',
            new_name: 'valid_path',
          },
        },
      });

      jest.runOnlyPendingTimers();
      await nextTick();

      expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
        mutation: removeValidationErrorMutation,
        variables: {
          field: 'new_name',
          sourceGroupId: testGroup.id,
        },
      });
    });
  });
});