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

table_spec.js « components « deploy_keys « admin « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49bda7100fb544c4c96173680cb5bdd81a7c31f6 (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
import { merge } from 'lodash';
import { GlLoadingIcon, GlEmptyState, GlPagination, GlModal } from '@gitlab/ui';
import { nextTick } from 'vue';

import responseBody from 'test_fixtures/api/deploy_keys/index.json';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { stubComponent } from 'helpers/stub_component';
import DeployKeysTable from '~/admin/deploy_keys/components/table.vue';
import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
import Api, { DEFAULT_PER_PAGE } from '~/api';
import createFlash from '~/flash';

jest.mock('~/api');
jest.mock('~/flash');
jest.mock('~/lib/utils/csrf', () => ({ token: 'mock-csrf-token' }));

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

  const defaultProvide = {
    createPath: '/admin/deploy_keys/new',
    deletePath: '/admin/deploy_keys/:id',
    editPath: '/admin/deploy_keys/:id/edit',
    emptyStateSvgPath: '/assets/illustrations/empty-state/empty-deploy-keys.svg',
  };

  const deployKey = responseBody[0];
  const deployKey2 = responseBody[1];

  const createComponent = (provide = {}) => {
    wrapper = mountExtended(DeployKeysTable, {
      provide: merge({}, defaultProvide, provide),
      stubs: {
        GlModal: stubComponent(GlModal, {
          template: `
            <div>
              <slot name="modal-title"></slot>
              <slot></slot>
              <slot name="modal-footer"></slot>
            </div>`,
        }),
      },
    });
  };

  const findEditButton = (index) =>
    wrapper.findAllByLabelText(DeployKeysTable.i18n.edit, { selector: 'a' }).at(index);
  const findRemoveButton = (index) =>
    wrapper.findAllByLabelText(DeployKeysTable.i18n.delete, { selector: 'button' }).at(index);
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findTimeAgoTooltip = (index) => wrapper.findAllComponents(TimeAgoTooltip).at(index);
  const findPagination = () => wrapper.findComponent(GlPagination);

  const expectDeployKeyIsRendered = (expectedDeployKey, expectedRowIndex) => {
    const editButton = findEditButton(expectedRowIndex);
    const timeAgoTooltip = findTimeAgoTooltip(expectedRowIndex);

    expect(wrapper.findByText(expectedDeployKey.title).exists()).toBe(true);
    expect(wrapper.findByText(expectedDeployKey.fingerprint, { selector: 'code' }).exists()).toBe(
      true,
    );
    expect(timeAgoTooltip.exists()).toBe(true);
    expect(timeAgoTooltip.props('time')).toBe(expectedDeployKey.created_at);
    expect(editButton.exists()).toBe(true);
    expect(editButton.attributes('href')).toBe(`/admin/deploy_keys/${expectedDeployKey.id}/edit`);
    expect(findRemoveButton(expectedRowIndex).exists()).toBe(true);
  };

  const itRendersTheEmptyState = () => {
    it('renders empty state', () => {
      const emptyState = wrapper.findComponent(GlEmptyState);

      expect(emptyState.exists()).toBe(true);
      expect(emptyState.props()).toMatchObject({
        svgPath: defaultProvide.emptyStateSvgPath,
        title: DeployKeysTable.i18n.emptyStateTitle,
        description: DeployKeysTable.i18n.emptyStateDescription,
        primaryButtonText: DeployKeysTable.i18n.newDeployKeyButtonText,
        primaryButtonLink: defaultProvide.createPath,
      });
    });
  };

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

  it('renders page title', () => {
    createComponent();

    expect(wrapper.findByText(DeployKeysTable.i18n.pageTitle).exists()).toBe(true);
  });

  it('renders `New deploy key` button', () => {
    createComponent();

    const newDeployKeyButton = wrapper.findByTestId('new-deploy-key-button');

    expect(newDeployKeyButton.exists()).toBe(true);
    expect(newDeployKeyButton.attributes('href')).toBe(defaultProvide.createPath);
  });

  describe('when `/deploy_keys` API request is pending', () => {
    beforeEach(() => {
      Api.deployKeys.mockImplementation(() => new Promise(() => {}));
    });

    it('shows loading icon', async () => {
      createComponent();

      await nextTick();

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

  describe('when `/deploy_keys` API request is successful', () => {
    describe('when there are deploy keys', () => {
      beforeEach(() => {
        Api.deployKeys.mockResolvedValue({
          data: responseBody,
          headers: { 'x-total': `${responseBody.length}` },
        });

        createComponent();
      });

      it('renders deploy keys in table', () => {
        expectDeployKeyIsRendered(deployKey, 0);
        expectDeployKeyIsRendered(deployKey2, 1);
      });

      describe('when delete button is clicked', () => {
        it('asks user to confirm', async () => {
          await findRemoveButton(0).trigger('click');

          const modal = wrapper.findComponent(GlModal);
          const form = modal.find('form');
          const submitSpy = jest.spyOn(form.element, 'submit');

          expect(modal.props('visible')).toBe(true);
          expect(form.attributes('action')).toBe(`/admin/deploy_keys/${deployKey.id}`);
          expect(form.find('input[name="_method"]').attributes('value')).toBe('delete');
          expect(form.find('input[name="authenticity_token"]').attributes('value')).toBe(
            'mock-csrf-token',
          );

          modal.vm.$emit('primary');

          expect(submitSpy).toHaveBeenCalled();
        });
      });
    });

    describe('pagination', () => {
      beforeEach(() => {
        Api.deployKeys.mockResolvedValueOnce({
          data: [deployKey],
          headers: { 'x-total': '2' },
        });

        createComponent();
      });

      it('renders pagination', () => {
        const pagination = findPagination();
        expect(pagination.exists()).toBe(true);
        expect(pagination.props()).toMatchObject({
          value: 1,
          perPage: DEFAULT_PER_PAGE,
          totalItems: responseBody.length,
          nextText: DeployKeysTable.i18n.pagination.next,
          prevText: DeployKeysTable.i18n.pagination.prev,
          align: 'center',
        });
      });

      describe('when pagination is changed', () => {
        it('calls API with `page` parameter', async () => {
          const pagination = findPagination();
          expectDeployKeyIsRendered(deployKey, 0);

          Api.deployKeys.mockResolvedValue({
            data: [deployKey2],
            headers: { 'x-total': '2' },
          });

          pagination.vm.$emit('input', 2);

          await nextTick();

          expect(findLoadingIcon().exists()).toBe(true);
          expect(pagination.exists()).toBe(false);

          await waitForPromises();

          expect(Api.deployKeys).toHaveBeenCalledWith({
            page: 2,
            public: true,
          });
          expectDeployKeyIsRendered(deployKey2, 0);
        });
      });
    });

    describe('when there are no deploy keys', () => {
      beforeEach(() => {
        Api.deployKeys.mockResolvedValue({
          data: [],
          headers: { 'x-total': '0' },
        });

        createComponent();
      });

      itRendersTheEmptyState();
    });
  });

  describe('when `deploy_keys` API request is unsuccessful', () => {
    const error = new Error('Network Error');

    beforeEach(() => {
      Api.deployKeys.mockRejectedValue(error);

      createComponent();
    });

    itRendersTheEmptyState();

    it('displays flash', () => {
      expect(createFlash).toHaveBeenCalledWith({
        message: DeployKeysTable.i18n.apiErrorMessage,
        captureError: true,
        error,
      });
    });
  });
});