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

create_token_button_spec.js « components « agents « clusters « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb1a3aa29634617fbcc8191824ef9e486f57c527 (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
import { GlButton, GlTooltip, GlModal, GlFormInput, GlFormTextarea, GlAlert } from '@gitlab/ui';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { mockTracking } from 'helpers/tracking_helper';
import {
  EVENT_LABEL_MODAL,
  EVENT_ACTIONS_OPEN,
  TOKEN_NAME_LIMIT,
  TOKEN_STATUS_ACTIVE,
  MAX_LIST_COUNT,
  CREATE_TOKEN_MODAL,
} from '~/clusters/agents/constants';
import createNewAgentToken from '~/clusters/agents/graphql/mutations/create_new_agent_token.mutation.graphql';
import getClusterAgentQuery from '~/clusters/agents/graphql/queries/get_cluster_agent.query.graphql';
import AgentToken from '~/clusters_list/components/agent_token.vue';
import CreateTokenButton from '~/clusters/agents/components/create_token_button.vue';
import {
  clusterAgentToken,
  getTokenResponse,
  createAgentTokenErrorResponse,
} from '../../mock_data';

Vue.use(VueApollo);

describe('CreateTokenButton', () => {
  let wrapper;
  let apolloProvider;
  let trackingSpy;
  let createResponse;

  const clusterAgentId = 'cluster-agent-id';
  const cursor = {
    first: MAX_LIST_COUNT,
    last: null,
  };
  const agentName = 'cluster-agent';
  const projectPath = 'path/to/project';

  const defaultProvide = {
    agentName,
    projectPath,
    canAdminCluster: true,
  };
  const propsData = {
    clusterAgentId,
    cursor,
  };

  const findModal = () => wrapper.findComponent(GlModal);
  const findBtn = () => wrapper.findComponent(GlButton);
  const findInput = () => wrapper.findComponent(GlFormInput);
  const findTextarea = () => wrapper.findComponent(GlFormTextarea);
  const findAlert = () => wrapper.findComponent(GlAlert);
  const findTooltip = () => wrapper.findComponent(GlTooltip);
  const findAgentInstructions = () => findModal().findComponent(AgentToken);
  const findButtonByVariant = (variant) =>
    findModal()
      .findAll(GlButton)
      .wrappers.find((button) => button.props('variant') === variant);
  const findActionButton = () => findButtonByVariant('confirm');
  const findCancelButton = () => wrapper.findByTestId('agent-token-close-button');

  const expectDisabledAttribute = (element, disabled) => {
    if (disabled) {
      expect(element.attributes('disabled')).toBe('true');
    } else {
      expect(element.attributes('disabled')).toBeUndefined();
    }
  };

  const createMockApolloProvider = ({ mutationResponse }) => {
    createResponse = jest.fn().mockResolvedValue(mutationResponse);

    return createMockApollo([[createNewAgentToken, createResponse]]);
  };

  const writeQuery = () => {
    apolloProvider.clients.defaultClient.cache.writeQuery({
      query: getClusterAgentQuery,
      data: getTokenResponse.data,
      variables: {
        agentName,
        projectPath,
        tokenStatus: TOKEN_STATUS_ACTIVE,
        ...cursor,
      },
    });
  };

  const createWrapper = async ({ provideData = {} } = {}) => {
    wrapper = shallowMountExtended(CreateTokenButton, {
      apolloProvider,
      provide: {
        ...defaultProvide,
        ...provideData,
      },
      propsData,
      stubs: {
        GlModal,
        GlTooltip,
      },
    });
    wrapper.vm.$refs.modal.hide = jest.fn();

    trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);
  };

  const mockCreatedResponse = (mutationResponse) => {
    apolloProvider = createMockApolloProvider({ mutationResponse });
    writeQuery();

    createWrapper();

    findInput().vm.$emit('input', 'new-token');
    findTextarea().vm.$emit('input', 'new-token-description');
    findActionButton().vm.$emit('click');

    return waitForPromises();
  };

  beforeEach(() => {
    createWrapper();
  });

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

  describe('create agent token action', () => {
    it('displays create agent token button', () => {
      expect(findBtn().text()).toBe('Create token');
    });

    describe('when user cannot create token', () => {
      beforeEach(() => {
        createWrapper({ provideData: { canAdminCluster: false } });
      });

      it('disabled the button', () => {
        expect(findBtn().attributes('disabled')).toBe('true');
      });

      it('shows a disabled tooltip', () => {
        expect(findTooltip().attributes('title')).toBe(
          'Requires a Maintainer or greater role to perform these actions',
        );
      });
    });

    describe('when user can create a token and clicks the button', () => {
      beforeEach(() => {
        findBtn().vm.$emit('click');
      });

      it('displays a token creation modal', () => {
        expect(findModal().isVisible()).toBe(true);
      });

      describe('initial state', () => {
        it('renders an input for the token name', () => {
          expect(findInput().exists()).toBe(true);
          expectDisabledAttribute(findInput(), false);
          expect(findInput().attributes('max-length')).toBe(TOKEN_NAME_LIMIT.toString());
        });

        it('renders a textarea for the token description', () => {
          expect(findTextarea().exists()).toBe(true);
          expectDisabledAttribute(findTextarea(), false);
        });

        it('renders a cancel button', () => {
          expect(findCancelButton().isVisible()).toBe(true);
          expectDisabledAttribute(findCancelButton(), false);
        });

        it('renders a disabled next button', () => {
          expect(findActionButton().text()).toBe('Create token');
          expectDisabledAttribute(findActionButton(), true);
        });

        it('sends tracking event for modal shown', () => {
          findModal().vm.$emit('show');
          expect(trackingSpy).toHaveBeenCalledWith(undefined, EVENT_ACTIONS_OPEN, {
            label: EVENT_LABEL_MODAL,
          });
        });
      });

      describe('when user inputs the token name', () => {
        beforeEach(() => {
          expectDisabledAttribute(findActionButton(), true);
          findInput().vm.$emit('input', 'new-token');
        });

        it('enables the next button', () => {
          expectDisabledAttribute(findActionButton(), false);
        });
      });

      describe('when user clicks the create-token button', () => {
        beforeEach(async () => {
          const loadingResponse = new Promise(() => {});
          await mockCreatedResponse(loadingResponse);

          findInput().vm.$emit('input', 'new-token');
          findActionButton().vm.$emit('click');
        });

        it('disables the create-token button', () => {
          expectDisabledAttribute(findActionButton(), true);
        });

        it('hides the cancel button', () => {
          expect(findCancelButton().exists()).toBe(false);
        });
      });

      describe('creating a new token', () => {
        beforeEach(async () => {
          await mockCreatedResponse(clusterAgentToken);
        });

        it('creates a token', () => {
          expect(createResponse).toHaveBeenCalledWith({
            input: { clusterAgentId, name: 'new-token', description: 'new-token-description' },
          });
        });

        it('shows agent instructions', () => {
          expect(findAgentInstructions().props()).toMatchObject({
            agentName,
            agentToken: 'token-secret',
            modalId: CREATE_TOKEN_MODAL,
          });
        });

        it('renders a close button', () => {
          expect(findActionButton().isVisible()).toBe(true);
          expect(findActionButton().text()).toBe('Close');
          expectDisabledAttribute(findActionButton(), false);
        });
      });

      describe('error creating a new token', () => {
        beforeEach(async () => {
          await mockCreatedResponse(createAgentTokenErrorResponse);
        });

        it('displays the error message', async () => {
          expect(findAlert().text()).toBe(
            createAgentTokenErrorResponse.data.clusterAgentTokenCreate.errors[0],
          );
        });
      });
    });
  });
});