summaryrefslogtreecommitdiff
path: root/spec/frontend/import_entities/import_projects/components/import_projects_table_spec.js
blob: 16adf88700fc060a535fc70ebbd88ef7080a9803 (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
import { GlLoadingIcon, GlButton, GlIntersectionObserver, GlFormInput } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { STATUSES } from '~/import_entities/constants';
import ImportProjectsTable from '~/import_entities/import_projects/components/import_projects_table.vue';
import ProviderRepoTableRow from '~/import_entities/import_projects/components/provider_repo_table_row.vue';
import * as getters from '~/import_entities/import_projects/store/getters';
import state from '~/import_entities/import_projects/store/state';

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

  const USER_NAMESPACE = 'root';

  const findFilterField = () =>
    wrapper
      .findAllComponents(GlFormInput)
      .wrappers.find((w) => w.attributes('placeholder') === 'Filter by name');

  const providerTitle = 'THE PROVIDER';
  const providerRepo = {
    importSource: {
      id: 10,
      sanitizedName: 'sanitizedName',
      fullName: 'fullName',
    },
    importedProject: null,
  };

  const findImportAllButton = () =>
    wrapper
      .findAll(GlButton)
      .filter((w) => w.props().variant === 'success')
      .at(0);
  const findImportAllModal = () => wrapper.find({ ref: 'importAllModal' });

  const importAllFn = jest.fn();
  const importAllModalShowFn = jest.fn();
  const fetchReposFn = jest.fn();

  function createComponent({
    state: initialState,
    getters: customGetters,
    slots,
    filterable,
    paginatable,
  } = {}) {
    Vue.use(Vuex);

    const store = new Vuex.Store({
      state: { ...state(), defaultTargetNamespace: USER_NAMESPACE, ...initialState },
      getters: {
        ...getters,
        ...customGetters,
      },
      actions: {
        fetchRepos: fetchReposFn,
        fetchJobs: jest.fn(),
        fetchNamespaces: jest.fn(),
        importAll: importAllFn,
        stopJobsPolling: jest.fn(),
        clearJobsEtagPoll: jest.fn(),
        setFilter: jest.fn(),
      },
    });

    wrapper = shallowMount(ImportProjectsTable, {
      store,
      propsData: {
        providerTitle,
        filterable,
        paginatable,
      },
      slots,
      stubs: {
        GlModal: { template: '<div>Modal!</div>', methods: { show: importAllModalShowFn } },
      },
    });
  }

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

  it('renders a loading icon while repos are loading', () => {
    createComponent({ state: { isLoadingRepos: true } });

    expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
  });

  it('renders a loading icon while namespaces are loading', () => {
    createComponent({ state: { isLoadingNamespaces: true } });

    expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
  });

  it('renders a table with provider repos', () => {
    const repositories = [
      { importSource: { id: 1 }, importedProject: null },
      { importSource: { id: 2 }, importedProject: { importStatus: STATUSES.FINISHED } },
      { importSource: { id: 3, incompatible: true }, importedProject: {} },
    ];

    createComponent({
      state: { namespaces: [{ fullPath: 'path' }], repositories },
    });

    expect(wrapper.find(GlLoadingIcon).exists()).toBe(false);
    expect(wrapper.find('table').exists()).toBe(true);
    expect(
      wrapper
        .findAll('th')
        .filter((w) => w.text() === `From ${providerTitle}`)
        .exists(),
    ).toBe(true);

    expect(wrapper.findAll(ProviderRepoTableRow)).toHaveLength(repositories.length);
  });

  it.each`
    hasIncompatibleRepos | count | buttonText
    ${false}             | ${1}  | ${'Import 1 repository'}
    ${true}              | ${1}  | ${'Import 1 compatible repository'}
    ${false}             | ${5}  | ${'Import 5 repositories'}
    ${true}              | ${5}  | ${'Import 5 compatible repositories'}
  `(
    'import all button has "$buttonText" text when hasIncompatibleRepos is $hasIncompatibleRepos and repos count is $count',
    ({ hasIncompatibleRepos, buttonText, count }) => {
      createComponent({
        state: {
          providerRepos: [providerRepo],
        },
        getters: {
          hasIncompatibleRepos: () => hasIncompatibleRepos,
          importAllCount: () => count,
        },
      });

      expect(findImportAllButton().text()).toBe(buttonText);
    },
  );

  it.each`
    importingRepoCount | buttonMessage
    ${1}               | ${'Importing 1 repository'}
    ${5}               | ${'Importing 5 repositories'}
  `(
    'sets the button text to "$buttonMessage" when importing repos',
    ({ importingRepoCount, buttonMessage }) => {
      createComponent({
        state: {
          providerRepos: [providerRepo],
        },
        getters: {
          hasIncompatibleRepos: () => false,
          importAllCount: () => 10,
          isImportingAnyRepo: () => true,
          importingRepoCount: () => importingRepoCount,
        },
      });

      expect(findImportAllButton().text()).toBe(buttonMessage);
    },
  );

  it('renders an empty state if there are no repositories available', () => {
    createComponent({ state: { repositories: [] } });

    expect(wrapper.find(ProviderRepoTableRow).exists()).toBe(false);
    expect(wrapper.text()).toContain(`No ${providerTitle} repositories found`);
  });

  it('opens confirmation modal when import all button is clicked', async () => {
    createComponent({ state: { repositories: [providerRepo] } });

    findImportAllButton().vm.$emit('click');
    await nextTick();

    expect(importAllModalShowFn).toHaveBeenCalled();
  });

  it('triggers importAll action when modal is confirmed', async () => {
    createComponent({ state: { providerRepos: [providerRepo] } });

    findImportAllModal().vm.$emit('ok');
    await nextTick();

    expect(importAllFn).toHaveBeenCalled();
  });

  it('shows loading spinner when import is in progress', () => {
    createComponent({ getters: { isImportingAnyRepo: () => true, importallCount: () => 1 } });

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

  it('renders filtering input field by default', () => {
    createComponent();

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

  it('does not render filtering input field when filterable is false', () => {
    createComponent({ filterable: false });

    expect(findFilterField()).toBeUndefined();
  });

  describe('when paginatable is set to true', () => {
    const pageInfo = { page: 1 };

    beforeEach(() => {
      createComponent({
        state: {
          namespaces: [{ fullPath: 'path' }],
          pageInfo,
          repositories: [
            { importSource: { id: 1 }, importedProject: null, importStatus: STATUSES.NONE },
          ],
        },
        paginatable: true,
      });
    });

    it('does not call fetchRepos on mount', () => {
      expect(fetchReposFn).not.toHaveBeenCalled();
    });

    it('renders intersection observer component', () => {
      expect(wrapper.find(GlIntersectionObserver).exists()).toBe(true);
    });

    it('calls fetchRepos when intersection observer appears', async () => {
      wrapper.find(GlIntersectionObserver).vm.$emit('appear');

      await nextTick();

      expect(fetchReposFn).toHaveBeenCalled();
    });
  });

  it('calls fetchRepos on mount', () => {
    createComponent();

    expect(fetchReposFn).toHaveBeenCalled();
  });

  it.each`
    hasIncompatibleRepos | shouldRenderSlot | action
    ${false}             | ${false}         | ${'does not render'}
    ${true}              | ${true}          | ${'render'}
  `(
    '$action incompatible-repos-warning slot if hasIncompatibleRepos is $hasIncompatibleRepos',
    ({ hasIncompatibleRepos, shouldRenderSlot }) => {
      const INCOMPATIBLE_TEXT = 'INCOMPATIBLE!';

      createComponent({
        getters: {
          hasIncompatibleRepos: () => hasIncompatibleRepos,
        },

        slots: {
          'incompatible-repos-warning': INCOMPATIBLE_TEXT,
        },
      });

      expect(wrapper.text().includes(INCOMPATIBLE_TEXT)).toBe(shouldRenderSlot);
    },
  );
});