summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/forms_spec.js
blob: 123d36ac5d58ea74ef1d306477685e88f1495602 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import {
  serializeForm,
  serializeFormObject,
  isEmptyValue,
  parseRailsFormFields,
} from '~/lib/utils/forms';

describe('lib/utils/forms', () => {
  const createDummyForm = (inputs) => {
    const form = document.createElement('form');

    form.innerHTML = inputs
      .map(({ type, name, value }) => {
        let str = ``;
        if (type === 'select') {
          str = `<select name="${name}">`;
          value.forEach((v) => {
            if (v.length > 0) {
              str += `<option value="${v}"></option> `;
            }
          });
          str += `</select>`;
        } else {
          str = `<input type="${type}" name="${name}" value="${value}" checked/>`;
        }
        return str;
      })
      .join('');

    return form;
  };

  describe('serializeForm', () => {
    it('returns an object of key values from inputs', () => {
      const form = createDummyForm([
        { type: 'text', name: 'foo', value: 'foo-value' },
        { type: 'text', name: 'bar', value: 'bar-value' },
      ]);

      const data = serializeForm(form);

      expect(data).toEqual({
        foo: 'foo-value',
        bar: 'bar-value',
      });
    });

    it('works with select', () => {
      const form = createDummyForm([
        { type: 'select', name: 'foo', value: ['foo-value1', 'foo-value2'] },
        { type: 'text', name: 'bar', value: 'bar-value1' },
      ]);

      const data = serializeForm(form);

      expect(data).toEqual({
        foo: 'foo-value1',
        bar: 'bar-value1',
      });
    });

    it('works with multiple inputs of the same name', () => {
      const form = createDummyForm([
        { type: 'checkbox', name: 'foo', value: 'foo-value3' },
        { type: 'checkbox', name: 'foo', value: 'foo-value2' },
        { type: 'checkbox', name: 'foo', value: 'foo-value1' },
        { type: 'text', name: 'bar', value: 'bar-value2' },
        { type: 'text', name: 'bar', value: 'bar-value1' },
      ]);

      const data = serializeForm(form);

      expect(data).toEqual({
        foo: ['foo-value3', 'foo-value2', 'foo-value1'],
        bar: ['bar-value2', 'bar-value1'],
      });
    });

    it('handles Microsoft Edge FormData.getAll() bug', () => {
      const formData = [
        { type: 'checkbox', name: 'foo', value: 'foo-value1' },
        { type: 'text', name: 'bar', value: 'bar-value2' },
      ];

      const form = createDummyForm(formData);

      jest
        .spyOn(FormData.prototype, 'getAll')
        .mockImplementation((name) =>
          formData.map((elem) => (elem.name === name ? elem.value : undefined)),
        );

      const data = serializeForm(form);

      expect(data).toEqual({
        foo: 'foo-value1',
        bar: 'bar-value2',
      });
    });
  });

  describe('isEmptyValue', () => {
    it.each`
      input        | returnValue
      ${''}        | ${true}
      ${[]}        | ${true}
      ${null}      | ${true}
      ${undefined} | ${true}
      ${'hello'}   | ${false}
      ${' '}       | ${false}
      ${0}         | ${false}
    `('returns $returnValue for value $input', ({ input, returnValue }) => {
      expect(isEmptyValue(input)).toBe(returnValue);
    });
  });

  describe('serializeFormObject', () => {
    it('returns an serialized object', () => {
      const form = {
        profileName: { value: 'hello', state: null, feedback: null },
        spiderTimeout: { value: 2, state: true, feedback: null },
        targetTimeout: { value: 12, state: true, feedback: null },
      };
      expect(serializeFormObject(form)).toEqual({
        profileName: 'hello',
        spiderTimeout: 2,
        targetTimeout: 12,
      });
    });

    it('returns only the entries with value', () => {
      const form = {
        profileName: { value: '', state: null, feedback: null },
        spiderTimeout: { value: 0, state: null, feedback: null },
        targetTimeout: { value: null, state: null, feedback: null },
        name: { value: undefined, state: null, feedback: null },
      };
      expect(serializeFormObject(form)).toEqual({
        spiderTimeout: 0,
      });
    });
  });

  describe('parseRailsFormFields', () => {
    let mountEl;

    beforeEach(() => {
      mountEl = document.createElement('div');
      mountEl.classList.add('js-foo-bar');
    });

    afterEach(() => {
      mountEl = null;
    });

    it('parses fields generated by Rails and returns object with HTML attributes', () => {
      mountEl.innerHTML = `
        <input type="text" placeholder="Name" value="Administrator" name="user[name]" id="user_name" data-js-name="name">
        <input type="text" placeholder="Email" value="foo@bar.com" name="user[contact_info][email]" id="user_contact_info_email" data-js-name="contactInfoEmail">
        <input type="text" placeholder="Phone" value="(123) 456-7890" name="user[contact_info][phone]" id="user_contact_info_phone" data-js-name="contact_info_phone">
        <input type="hidden" placeholder="Job title" value="" name="user[job_title]" id="user_job_title" data-js-name="jobTitle">
        <textarea name="user[bio]" id="user_bio" data-js-name="bio">Foo bar</textarea>
        <select name="user[timezone]" id="user_timezone" data-js-name="timezone">
          <option value="utc+12">[UTC - 12] International Date Line West</option>
          <option value="utc+11" selected>[UTC - 11] American Samoa</option>
        </select>
        <input type="checkbox" name="user[interests][]" id="user_interests_vue" value="Vue" checked data-js-name="interests">
        <input type="checkbox" name="user[interests][]" id="user_interests_graphql" value="GraphQL" data-js-name="interests">
        <input type="radio" name="user[access_level]" value="regular" id="user_access_level_regular" data-js-name="accessLevel">
        <input type="radio" name="user[access_level]" value="admin" id="user_access_level_admin" checked data-js-name="access_level">
        <input name="user[private_profile]" type="hidden" value="0">
        <input type="radio" name="user[private_profile]" id="user_private_profile" value="1" checked data-js-name="privateProfile">
        <input name="user[email_notifications]" type="hidden" value="0">
        <input type="radio" name="user[email_notifications]" id="user_email_notifications" value="1" data-js-name="emailNotifications">
      `;

      expect(parseRailsFormFields(mountEl)).toEqual({
        name: {
          name: 'user[name]',
          id: 'user_name',
          value: 'Administrator',
          placeholder: 'Name',
        },
        contactInfoEmail: {
          name: 'user[contact_info][email]',
          id: 'user_contact_info_email',
          value: 'foo@bar.com',
          placeholder: 'Email',
        },
        contactInfoPhone: {
          name: 'user[contact_info][phone]',
          id: 'user_contact_info_phone',
          value: '(123) 456-7890',
          placeholder: 'Phone',
        },
        jobTitle: {
          name: 'user[job_title]',
          id: 'user_job_title',
          value: '',
          placeholder: 'Job title',
        },
        bio: {
          name: 'user[bio]',
          id: 'user_bio',
          value: 'Foo bar',
        },
        timezone: {
          name: 'user[timezone]',
          id: 'user_timezone',
          value: 'utc+11',
        },
        interests: [
          {
            name: 'user[interests][]',
            id: 'user_interests_vue',
            value: 'Vue',
            checked: true,
          },
          {
            name: 'user[interests][]',
            id: 'user_interests_graphql',
            value: 'GraphQL',
            checked: false,
          },
        ],
        accessLevel: [
          {
            name: 'user[access_level]',
            id: 'user_access_level_regular',
            value: 'regular',
            checked: false,
          },
          {
            name: 'user[access_level]',
            id: 'user_access_level_admin',
            value: 'admin',
            checked: true,
          },
        ],
        privateProfile: [
          {
            name: 'user[private_profile]',
            id: 'user_private_profile',
            value: '1',
            checked: true,
          },
        ],
        emailNotifications: [
          {
            name: 'user[email_notifications]',
            id: 'user_email_notifications',
            value: '1',
            checked: false,
          },
        ],
      });
    });

    it('returns an empty object if there are no inputs', () => {
      expect(parseRailsFormFields(mountEl)).toEqual({});
    });

    it('returns an empty object if inputs do not have `name` attributes', () => {
      mountEl.innerHTML = `
        <input type="text" placeholder="Name" value="Administrator" id="user_name">
        <input type="text" placeholder="Email" value="foo@bar.com" id="user_contact_info_email">
        <input type="text" placeholder="Phone" value="(123) 456-7890" id="user_contact_info_phone">
      `;

      expect(parseRailsFormFields(mountEl)).toEqual({});
    });

    it('does not include field if `data-js-name` attribute is missing', () => {
      mountEl.innerHTML = `
        <input type="text" placeholder="Name" value="Administrator" name="user[name]" id="user_name" data-js-name="name">
        <input type="text" placeholder="Email" value="foo@bar.com" name="user[email]" id="email">
      `;

      expect(parseRailsFormFields(mountEl)).toEqual({
        name: {
          name: 'user[name]',
          id: 'user_name',
          value: 'Administrator',
          placeholder: 'Name',
        },
      });
    });

    it('throws error if `mountEl` argument is not passed', () => {
      expect(() => parseRailsFormFields()).toThrow(new TypeError('`mountEl` argument is required'));
    });

    it('throws error if `mountEl` argument is `null`', () => {
      expect(() => parseRailsFormFields(null)).toThrow(
        new TypeError('`mountEl` argument is required'),
      );
    });
  });
});