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

index_spec.js « access_tokens « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b6119f1d167f321ebd67789b97af6118413388a1 (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
/* eslint-disable vue/require-prop-types */
/* eslint-disable vue/one-component-per-file */
import { createWrapper } from '@vue/test-utils';
import Vue from 'vue';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';

import {
  initAccessTokenTableApp,
  initExpiresAtField,
  initNewAccessTokenApp,
  initProjectsField,
  initTokensApp,
} from '~/access_tokens';
import * as AccessTokenTableApp from '~/access_tokens/components/access_token_table_app.vue';
import * as ExpiresAtField from '~/access_tokens/components/expires_at_field.vue';
import * as NewAccessTokenApp from '~/access_tokens/components/new_access_token_app.vue';
import * as ProjectsField from '~/access_tokens/components/projects_field.vue';
import * as TokensApp from '~/access_tokens/components/tokens_app.vue';
import { FEED_TOKEN, INCOMING_EMAIL_TOKEN, STATIC_OBJECT_TOKEN } from '~/access_tokens/constants';
import { __, sprintf } from '~/locale';

describe('access tokens', () => {
  let wrapper;

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

  describe('initAccessTokenTableApp', () => {
    const accessTokenType = 'personal access token';
    const accessTokenTypePlural = 'personal access tokens';
    const initialActiveAccessTokens = [{ id: '1' }];

    const FakeAccessTokenTableApp = Vue.component('FakeComponent', {
      inject: [
        'accessTokenType',
        'accessTokenTypePlural',
        'initialActiveAccessTokens',
        'noActiveTokensMessage',
        'showRole',
      ],
      props: [
        'accessTokenType',
        'accessTokenTypePlural',
        'initialActiveAccessTokens',
        'noActiveTokensMessage',
        'showRole',
      ],
      render: () => null,
    });
    AccessTokenTableApp.default = FakeAccessTokenTableApp;

    it('mounts the component and provides required values', () => {
      setHTMLFixture(
        `<div id="js-access-token-table-app"
        data-access-token-type="${accessTokenType}"
        data-access-token-type-plural="${accessTokenTypePlural}"
        data-initial-active-access-tokens=${JSON.stringify(initialActiveAccessTokens)}
        >
        </div>`,
      );

      const vueInstance = initAccessTokenTableApp();

      wrapper = createWrapper(vueInstance);
      const component = wrapper.findComponent(FakeAccessTokenTableApp);

      expect(component.exists()).toBe(true);

      expect(component.props()).toMatchObject({
        // Required value
        accessTokenType,
        accessTokenTypePlural,
        initialActiveAccessTokens,

        // Default values
        noActiveTokensMessage: sprintf(__('This user has no active %{accessTokenTypePlural}.'), {
          accessTokenTypePlural,
        }),
        showRole: false,
      });
    });

    it('mounts the component and provides all values', () => {
      const noActiveTokensMessage = 'This group has no active access tokens.';
      setHTMLFixture(
        `<div id="js-access-token-table-app"
          data-access-token-type="${accessTokenType}"
          data-access-token-type-plural="${accessTokenTypePlural}"
          data-initial-active-access-tokens=${JSON.stringify(initialActiveAccessTokens)}
          data-no-active-tokens-message="${noActiveTokensMessage}"
          data-show-role
          >
        </div>`,
      );

      const vueInstance = initAccessTokenTableApp();

      wrapper = createWrapper(vueInstance);
      const component = wrapper.findComponent(FakeAccessTokenTableApp);

      expect(component.exists()).toBe(true);
      expect(component.props()).toMatchObject({
        accessTokenType,
        accessTokenTypePlural,
        initialActiveAccessTokens,
        noActiveTokensMessage,
        showRole: true,
      });
    });

    it('returns `null`', () => {
      expect(initNewAccessTokenApp()).toBe(null);
    });
  });

  describe.each`
    initFunction          | mountSelector                    | fieldName      | expectedComponent
    ${initExpiresAtField} | ${'js-access-tokens-expires-at'} | ${'expiresAt'} | ${ExpiresAtField}
    ${initProjectsField}  | ${'js-access-tokens-projects'}   | ${'projects'}  | ${ProjectsField}
  `('$initFunction', ({ initFunction, mountSelector, fieldName, expectedComponent }) => {
    describe('when mount element exists', () => {
      const FakeComponent = Vue.component('FakeComponent', {
        props: ['inputAttrs'],
        render: () => null,
      });

      const nameAttribute = `access_tokens[${fieldName}]`;
      const idAttribute = `access_tokens_${fieldName}`;

      beforeEach(() => {
        window.gon = { features: { personalAccessTokensScopedToProjects: true } };

        setHTMLFixture(
          `<div class="${mountSelector}">
            <input
              name="${nameAttribute}"
              data-js-name="${fieldName}"
              id="${idAttribute}"
              placeholder="Foo bar"
              value="1,2"
            />
          </div>`,
        );

        // Mock component so we don't have to deal with mocking Apollo
        // eslint-disable-next-line no-param-reassign
        expectedComponent.default = FakeComponent;
      });

      afterEach(() => {
        delete window.gon;
      });

      it('mounts component and sets `inputAttrs` prop', async () => {
        const vueInstance = await initFunction();

        wrapper = createWrapper(vueInstance);
        const component = wrapper.findComponent(FakeComponent);

        expect(component.exists()).toBe(true);
        expect(component.props('inputAttrs')).toEqual({
          name: nameAttribute,
          id: idAttribute,
          value: '1,2',
          placeholder: 'Foo bar',
        });
      });
    });

    describe('when mount element does not exist', () => {
      it('returns `null`', () => {
        expect(initFunction()).toBe(null);
      });
    });
  });

  describe('initNewAccessTokenApp', () => {
    it('mounts the component and sets `accessTokenType` prop', () => {
      const accessTokenType = 'personal access token';
      setHTMLFixture(
        `<div id="js-new-access-token-app" data-access-token-type="${accessTokenType}"></div>`,
      );

      const FakeNewAccessTokenApp = Vue.component('FakeComponent', {
        inject: ['accessTokenType'],
        props: ['accessTokenType'],
        render: () => null,
      });
      NewAccessTokenApp.default = FakeNewAccessTokenApp;

      const vueInstance = initNewAccessTokenApp();

      wrapper = createWrapper(vueInstance);
      const component = wrapper.findComponent(FakeNewAccessTokenApp);

      expect(component.exists()).toBe(true);
      expect(component.props('accessTokenType')).toEqual(accessTokenType);
    });

    it('returns `null`', () => {
      expect(initNewAccessTokenApp()).toBe(null);
    });
  });

  describe('initTokensApp', () => {
    it('mounts the component and provides`tokenTypes` ', () => {
      const tokensData = {
        [FEED_TOKEN]: FEED_TOKEN,
        [INCOMING_EMAIL_TOKEN]: INCOMING_EMAIL_TOKEN,
        [STATIC_OBJECT_TOKEN]: STATIC_OBJECT_TOKEN,
      };
      setHTMLFixture(
        `<div id="js-tokens-app" data-tokens-data=${JSON.stringify(tokensData)}></div>`,
      );

      const FakeTokensApp = Vue.component('FakeComponent', {
        inject: ['tokenTypes'],
        props: ['tokenTypes'],
        render: () => null,
      });
      TokensApp.default = FakeTokensApp;

      const vueInstance = initTokensApp();

      wrapper = createWrapper(vueInstance);
      const component = wrapper.findComponent(FakeTokensApp);

      expect(component.exists()).toBe(true);
      expect(component.props('tokenTypes')).toEqual(tokensData);
    });

    it('returns `null`', () => {
      expect(initNewAccessTokenApp()).toBe(null);
    });
  });
});