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

custom_email_form_spec.js « components « settings_service_desk « projects « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b012995ea431da2bf374347bc0695ce97b95a6d (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
import { mount } from '@vue/test-utils';
import { GlLink } from '@gitlab/ui';
import { nextTick } from 'vue';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import { helpPagePath } from '~/helpers/help_page_helper';
import CustomEmailForm from '~/projects/settings_service_desk/components/custom_email_form.vue';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import { I18N_FORM_FORWARDING_CLIPBOARD_BUTTON_TITLE } from '~/projects/settings_service_desk/custom_email_constants';

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

  const defaultProps = {
    incomingEmail: 'incoming@example.com',
    isSubmitting: false,
  };

  const findForm = () => wrapper.find('form');
  const findClipboardButton = () => wrapper.findComponent(ClipboardButton);
  const findLink = () => wrapper.findComponent(GlLink);
  const findInputByTestId = (testId) => wrapper.findByTestId(testId).find('input');
  const findCustomEmailInput = () => findInputByTestId('form-custom-email');
  const findSmtpAddressInput = () => findInputByTestId('form-smtp-address');
  const findSmtpPortInput = () => findInputByTestId('form-smtp-port');
  const findSmtpUsernameInput = () => findInputByTestId('form-smtp-username');
  const findSmtpPasswordInput = () => findInputByTestId('form-smtp-password');
  const findSubmit = () => wrapper.findByTestId('form-submit');

  const clickButtonAndExpectNoSubmitEvent = async () => {
    await nextTick();
    findForm().trigger('submit');

    expect(findSubmit().find('button').attributes('disabled')).toBeDefined();
    expect(wrapper.emitted('submit')).toEqual(undefined);
  };

  const createWrapper = (props = {}) => {
    wrapper = extendedWrapper(mount(CustomEmailForm, { propsData: { ...defaultProps, ...props } }));
  };

  it('displays help page link', () => {
    createWrapper();

    expect(findLink().attributes('href')).toBe(
      helpPagePath('user/project/service_desk/configure.html', {
        anchor: 'custom-email-address',
      }),
    );
  });

  it('renders a copy to clipboard button', () => {
    createWrapper();

    expect(findClipboardButton().exists()).toBe(true);
    expect(findClipboardButton().props()).toEqual(
      expect.objectContaining({
        title: I18N_FORM_FORWARDING_CLIPBOARD_BUTTON_TITLE,
        text: defaultProps.incomingEmail,
      }),
    );
  });

  it('form inputs are disabled when submitting', () => {
    createWrapper({ isSubmitting: true });

    expect(findCustomEmailInput().attributes('disabled')).toBeDefined();
    expect(findSmtpAddressInput().attributes('disabled')).toBeDefined();
    expect(findSmtpPortInput().attributes('disabled')).toBeDefined();
    expect(findSmtpUsernameInput().attributes('disabled')).toBeDefined();
    expect(findSmtpPasswordInput().attributes('disabled')).toBeDefined();
    expect(findSubmit().props('loading')).toBe(true);
  });

  describe('form validation and submit event', () => {
    it('is invalid when form inputs are empty', async () => {
      createWrapper();

      await nextTick();
      findForm().trigger('submit');

      expect(wrapper.emitted('submit')).toEqual(undefined);
    });

    describe('with inputs set', () => {
      beforeEach(() => {
        createWrapper();

        findCustomEmailInput().setValue('user@example.com');
        findCustomEmailInput().trigger('change');

        findSmtpAddressInput().setValue('smtp.example.com');
        findSmtpAddressInput().trigger('change');

        findSmtpPortInput().setValue('587');
        findSmtpPortInput().trigger('change');

        findSmtpUsernameInput().setValue('user@example.com');
        findSmtpUsernameInput().trigger('change');

        findSmtpPasswordInput().setValue('supersecret');
        findSmtpPasswordInput().trigger('change');
      });

      it('is invalid when malformed email provided', async () => {
        findCustomEmailInput().setValue('userexample.com');
        findCustomEmailInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findCustomEmailInput().classes()).toContain('is-invalid');
      });

      it('is invalid when email is not set', async () => {
        findCustomEmailInput().setValue('');
        findCustomEmailInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findCustomEmailInput().classes()).toContain('is-invalid');
      });

      it('is invalid when smtp address is not set', async () => {
        findSmtpAddressInput().setValue('');
        findSmtpAddressInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findSmtpAddressInput().classes()).toContain('is-invalid');
      });

      it('is invalid when smtp port is not set', async () => {
        findSmtpPortInput().setValue('');
        findSmtpPortInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findSmtpPortInput().classes()).toContain('is-invalid');
      });

      it('is invalid when smtp port is not an integer', async () => {
        findSmtpPortInput().setValue('20m2');
        findSmtpPortInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findSmtpPortInput().classes()).toContain('is-invalid');
      });

      it('is invalid when smtp port is 0', async () => {
        findSmtpPortInput().setValue('0');
        findSmtpPortInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findSmtpPortInput().classes()).toContain('is-invalid');
      });

      it('is invalid when smtp username is not set', async () => {
        findSmtpUsernameInput().setValue('');
        findSmtpUsernameInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findSmtpUsernameInput().classes()).toContain('is-invalid');
      });

      it('is invalid when password is too short', async () => {
        findSmtpPasswordInput().setValue('2short');
        findSmtpPasswordInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findSmtpPasswordInput().classes()).toContain('is-invalid');
      });

      it('is invalid when password is not set', async () => {
        findSmtpPasswordInput().setValue('');
        findSmtpPasswordInput().trigger('change');

        await clickButtonAndExpectNoSubmitEvent();
        expect(findSmtpPasswordInput().classes()).toContain('is-invalid');
      });

      it('sets smtpUsername automatically when empty based on customEmail', async () => {
        const email = 'support@example.com';

        findSmtpUsernameInput().setValue('');
        findSmtpUsernameInput().trigger('change');

        findCustomEmailInput().setValue(email);
        findCustomEmailInput().trigger('change');

        await nextTick();

        expect(findSmtpUsernameInput().element.value).toBe(email);
        expect(wrapper.html()).not.toContain('is-invalid');
      });

      it('is valid and emits submit event with form data', async () => {
        await nextTick();

        expect(wrapper.html()).not.toContain('is-invalid');

        findForm().trigger('submit');

        expect(wrapper.emitted('submit')).toEqual([
          [
            {
              custom_email: 'user@example.com',
              smtp_address: 'smtp.example.com',
              smtp_password: 'supersecret',
              smtp_port: '587',
              smtp_username: 'user@example.com',
            },
          ],
        ]);
      });
    });
  });
});