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

secure_files_list_spec.js « components « ci_secure_files « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 04d38a3281a6073619060598572fbef4d4c78d08 (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
import { GlLoadingIcon, GlModal } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import { mount } from '@vue/test-utils';
import axios from '~/lib/utils/axios_utils';
import SecureFilesList from '~/ci_secure_files/components/secure_files_list.vue';
import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import waitForPromises from 'helpers/wait_for_promises';
import Api from '~/api';

import { secureFiles } from '../mock_data';

const dummyApiVersion = 'v3000';
const dummyProjectId = 1;
const fileSizeLimit = 5;
const dummyUrlRoot = '/gitlab';
const dummyGon = {
  api_version: dummyApiVersion,
  relative_url_root: dummyUrlRoot,
};
let originalGon;
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/projects/${dummyProjectId}/secure_files`;

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

  beforeEach(() => {
    originalGon = window.gon;
    trackingSpy = mockTracking(undefined, undefined, jest.spyOn);
    window.gon = { ...dummyGon };
  });

  afterEach(() => {
    wrapper.destroy();
    mock.restore();
    unmockTracking();
    window.gon = originalGon;
  });

  const createWrapper = (admin = true, props = {}) => {
    wrapper = mount(SecureFilesList, {
      provide: {
        projectId: dummyProjectId,
        admin,
        fileSizeLimit,
      },
      ...props,
    });
  };

  const findRows = () => wrapper.findAll('tbody tr');
  const findRowAt = (i) => findRows().at(i);
  const findCell = (i, col) => findRowAt(i).findAll('td').at(col);
  const findHeaderAt = (i) => wrapper.findAll('thead th').at(i);
  const findPagination = () => wrapper.findAll('ul.pagination');
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findUploadButton = () => wrapper.findAll('span.gl-button-text');
  const findDeleteModal = () => wrapper.findComponent(GlModal);
  const findUploadInput = () => wrapper.findAll('input[type="file"]').at(0);
  const findDeleteButton = () => wrapper.findAll('[data-testid="delete-button"]');

  describe('when secure files exist in a project', () => {
    beforeEach(async () => {
      mock = new MockAdapter(axios);
      mock.onGet(expectedUrl).reply(200, secureFiles);

      createWrapper();
      await waitForPromises();
    });

    it('displays a table with expected headers', () => {
      const headers = ['File name', 'Uploaded date'];
      headers.forEach((header, i) => {
        expect(findHeaderAt(i).text()).toBe(header);
      });
    });

    it('displays a table with rows', () => {
      expect(findRows()).toHaveLength(secureFiles.length);

      const [secureFile] = secureFiles;

      expect(findCell(0, 0).text()).toBe(secureFile.name);
      expect(findCell(0, 1).find(TimeAgoTooltip).props('time')).toBe(secureFile.created_at);
    });

    describe('event tracking', () => {
      it('sends tracking information on list load', () => {
        expect(trackingSpy).toHaveBeenCalledWith(undefined, 'render_secure_files_list', {});
      });

      it('sends tracking information on file upload', async () => {
        Api.uploadProjectSecureFile = jest.fn().mockResolvedValue();
        Object.defineProperty(findUploadInput().element, 'files', { value: [{}] });
        findUploadInput().trigger('change');

        await waitForPromises();

        expect(trackingSpy).toHaveBeenCalledWith(undefined, 'upload_secure_file', {});
      });

      it('sends tracking information on file deletion', async () => {
        Api.deleteProjectSecureFile = jest.fn().mockResolvedValue();
        findDeleteModal().vm.$emit('ok');
        await waitForPromises();

        expect(trackingSpy).toHaveBeenCalledWith(undefined, 'delete_secure_file', {});
      });
    });
  });

  describe('when no secure files exist in a project', () => {
    beforeEach(async () => {
      mock = new MockAdapter(axios);
      mock.onGet(expectedUrl).reply(200, []);

      createWrapper();
      await waitForPromises();
    });

    it('displays a table with expected headers', () => {
      const headers = ['File name', 'Uploaded date'];
      headers.forEach((header, i) => {
        expect(findHeaderAt(i).text()).toBe(header);
      });
    });

    it('displays a table with a no records message', () => {
      expect(findCell(0, 0).text()).toBe('There are no secure files yet.');
    });
  });

  describe('pagination', () => {
    it('displays the pagination component with there are more than 20 items', async () => {
      mock = new MockAdapter(axios);
      mock.onGet(expectedUrl).reply(200, secureFiles, { 'x-total': 30 });

      createWrapper();
      await waitForPromises();

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

    it('does not display the pagination component with there are 20 items', async () => {
      mock = new MockAdapter(axios);
      mock.onGet(expectedUrl).reply(200, secureFiles, { 'x-total': 20 });

      createWrapper();
      await waitForPromises();

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

  describe('loading state', () => {
    it('displays the loading icon while waiting for the backend request', () => {
      mock = new MockAdapter(axios);
      mock.onGet(expectedUrl).reply(200, secureFiles);
      createWrapper();

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

    it('does not display the loading icon after the backend request has completed', async () => {
      mock = new MockAdapter(axios);
      mock.onGet(expectedUrl).reply(200, secureFiles);

      createWrapper();
      await waitForPromises();

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

  describe('admin permissions', () => {
    describe('with admin permissions', () => {
      beforeEach(async () => {
        mock = new MockAdapter(axios);
        mock.onGet(expectedUrl).reply(200, secureFiles);

        createWrapper();
        await waitForPromises();
      });

      it('displays the upload button', () => {
        expect(findUploadButton().exists()).toBe(true);
      });

      it('displays a delete button', () => {
        expect(findDeleteButton().exists()).toBe(true);
      });
    });

    describe('without admin permissions', () => {
      beforeEach(async () => {
        mock = new MockAdapter(axios);
        mock.onGet(expectedUrl).reply(200, secureFiles);

        createWrapper(false);
        await waitForPromises();
      });

      it('does not display the upload button', () => {
        expect(findUploadButton().exists()).toBe(false);
      });

      it('does not display a delete button', () => {
        expect(findDeleteButton().exists()).toBe(false);
      });
    });
  });
});