summaryrefslogtreecommitdiff
path: root/spec/frontend/access_tokens/components/new_access_token_app_spec.js
blob: b4af11169ad656938b1a8308b180d1e8a3ae3cab (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
import { GlAlert } from '@gitlab/ui';
import { nextTick } from 'vue';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import NewAccessTokenApp from '~/access_tokens/components/new_access_token_app.vue';
import { EVENT_ERROR, EVENT_SUCCESS, FORM_SELECTOR } from '~/access_tokens/components/constants';
import { createAlert, VARIANT_INFO } from '~/flash';
import { __, sprintf } from '~/locale';
import DomElementListener from '~/vue_shared/components/dom_element_listener.vue';
import InputCopyToggleVisibility from '~/vue_shared/components/form/input_copy_toggle_visibility.vue';

jest.mock('~/flash');

describe('~/access_tokens/components/new_access_token_app', () => {
  let wrapper;

  const accessTokenType = 'personal access token';

  const createComponent = (provide = { accessTokenType }) => {
    wrapper = mountExtended(NewAccessTokenApp, {
      provide,
    });
  };

  const findButtonEl = () => document.querySelector('[type=submit]');

  const triggerSuccess = async (newToken = 'new token') => {
    wrapper
      .findComponent(DomElementListener)
      .vm.$emit(EVENT_SUCCESS, { detail: [{ new_token: newToken }] });
    await nextTick();
  };

  const triggerError = async (errors = ['1', '2']) => {
    wrapper.findComponent(DomElementListener).vm.$emit(EVENT_ERROR, { detail: [{ errors }] });
    await nextTick();
  };

  beforeEach(() => {
    // NewAccessTokenApp observes a form element
    setHTMLFixture(
      `<form id="${FORM_SELECTOR.slice(1)}">
        <input type="text" id="expires_at" value="2022-01-01"/>
        <input type="text" value='1'/>
        <input type="checkbox" checked/>
        <button type="submit" value="Create" class="disabled" disabled="disabled"/>
      </form>`,
    );

    createComponent();
  });

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

  it('should render nothing', () => {
    expect(wrapper.findComponent(InputCopyToggleVisibility).exists()).toBe(false);
    expect(wrapper.findComponent(GlAlert).exists()).toBe(false);
  });

  describe('on success', () => {
    it('should render `InputCopyToggleVisibility` component', async () => {
      const newToken = '12345';
      await triggerSuccess(newToken);

      expect(wrapper.findComponent(GlAlert).exists()).toBe(false);

      const InputCopyToggleVisibilityComponent = wrapper.findComponent(InputCopyToggleVisibility);
      expect(InputCopyToggleVisibilityComponent.props('value')).toBe(newToken);
      expect(InputCopyToggleVisibilityComponent.props('copyButtonTitle')).toBe(
        sprintf(__('Copy %{accessTokenType}'), { accessTokenType }),
      );
      expect(InputCopyToggleVisibilityComponent.props('initialVisibility')).toBe(true);
      expect(InputCopyToggleVisibilityComponent.attributes('label')).toBe(
        sprintf(__('Your new %{accessTokenType}'), { accessTokenType }),
      );
    });

    it('input field should contain QA-related selectors', async () => {
      const newToken = '12345';
      await triggerSuccess(newToken);

      expect(wrapper.findComponent(GlAlert).exists()).toBe(false);

      const inputAttributes = wrapper
        .findByLabelText(sprintf(__('Your new %{accessTokenType}'), { accessTokenType }))
        .attributes();
      expect(inputAttributes).toMatchObject({
        'data-qa-selector': 'created_access_token_field',
      });
    });

    it('should render an info alert', async () => {
      await triggerSuccess();

      expect(createAlert).toHaveBeenCalledWith({
        message: sprintf(__('Your new %{accessTokenType} has been created.'), {
          accessTokenType,
        }),
        variant: VARIANT_INFO,
      });
    });

    describe('when resetting the form', () => {
      it('should reset selectively some input fields', async () => {
        expect(document.querySelector('input[type=text]:not([id$=expires_at])').value).toBe('1');
        expect(document.querySelector('input[type=checkbox]').checked).toBe(true);
        await triggerSuccess();

        expect(document.querySelector('input[type=text]:not([id$=expires_at])').value).toBe('');
        expect(document.querySelector('input[type=checkbox]').checked).toBe(false);
      });

      it('should not reset the date field', async () => {
        expect(document.querySelector('input[type=text][id$=expires_at]').value).toBe('2022-01-01');
        await triggerSuccess();

        expect(document.querySelector('input[type=text][id$=expires_at]').value).toBe('2022-01-01');
      });

      it('should not reset the submit button value', async () => {
        expect(findButtonEl().value).toBe('Create');
        await triggerSuccess();

        expect(findButtonEl().value).toBe('Create');
      });
    });
  });

  describe('on error', () => {
    it('should render an error alert', async () => {
      await triggerError(['first', 'second']);

      expect(wrapper.findComponent(InputCopyToggleVisibility).exists()).toBe(false);

      let GlAlertComponent = wrapper.findComponent(GlAlert);
      expect(GlAlertComponent.props('title')).toBe(__('The form contains the following errors:'));
      expect(GlAlertComponent.props('variant')).toBe('danger');
      let itemEls = wrapper.findAll('li');
      expect(itemEls).toHaveLength(2);
      expect(itemEls.at(0).text()).toBe('first');
      expect(itemEls.at(1).text()).toBe('second');

      await triggerError(['one']);

      GlAlertComponent = wrapper.findComponent(GlAlert);
      expect(GlAlertComponent.props('title')).toBe(__('The form contains the following error:'));
      expect(GlAlertComponent.props('variant')).toBe('danger');
      itemEls = wrapper.findAll('li');
      expect(itemEls).toHaveLength(1);
    });

    it('the error alert should be dismissible', async () => {
      await triggerError();

      const GlAlertComponent = wrapper.findComponent(GlAlert);
      expect(GlAlertComponent.exists()).toBe(true);

      GlAlertComponent.vm.$emit('dismiss');
      await nextTick();

      expect(wrapper.findComponent(GlAlert).exists()).toBe(false);
    });

    it('should enable the submit button', async () => {
      const button = findButtonEl();
      expect(button).toBeDisabled();
      expect(button.className).toBe('disabled');

      await triggerError();

      expect(button).not.toBeDisabled();
      expect(button.className).toBe('');
    });
  });

  describe('before error or success', () => {
    it('should scroll to the container', async () => {
      const containerEl = wrapper.vm.$refs.container;
      const scrollIntoViewSpy = jest.spyOn(containerEl, 'scrollIntoView');

      await triggerSuccess();

      expect(scrollIntoViewSpy).toHaveBeenCalledWith(false);
      expect(scrollIntoViewSpy).toHaveBeenCalledTimes(1);

      await triggerError();

      expect(scrollIntoViewSpy).toHaveBeenCalledWith(false);
      expect(scrollIntoViewSpy).toHaveBeenCalledTimes(2);
    });

    it('should dismiss the info alert', async () => {
      const dismissSpy = jest.fn();
      createAlert.mockReturnValue({ dismiss: dismissSpy });

      await triggerSuccess();
      await triggerError();

      expect(dismissSpy).toHaveBeenCalled();
      expect(dismissSpy).toHaveBeenCalledTimes(1);
    });
  });
});