-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathutils.serializeForm.test.js
506 lines (471 loc) · 19.6 KB
/
utils.serializeForm.test.js
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import assert from 'assert'
import { JSDOM } from 'jsdom'
import { serializeForm } from '../utils'
describe('formSerialize', () => {
context('basic', () => {
it('should serialize empty form', () => {
const dom = new JSDOM('<form></form>')
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = ''
assert.deepStrictEqual(actual, expected)
})
it('should serialize basic form with single input', () => {
const dom = new JSDOM(
'<form><input type="text" name="foo" value="bar"/></form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=bar'
assert.deepStrictEqual(actual, expected)
})
it('should serialize inputs with no values', () => {
const dom = new JSDOM('<form><input type="text" name="foo"/></form>')
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo='
assert.deepStrictEqual(actual, expected)
})
it('should serialize text input with spaces in name', () => {
const dom = new JSDOM(
'<form><input type="text" name="name 1" value="StimulusReflex"></form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'name 1=StimulusReflex'
assert.deepStrictEqual(actual, expected)
})
it('should serialize from with multiple inputs', () => {
const dom = new JSDOM(
'<form>' +
'<input type="text" name="foo" value="bar 1"/>' +
'<input type="text" name="foo.bar" value="bar 2"/>' +
'<input type="text" name="baz.foo" value="bar 3"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=bar 1&foo.bar=bar 2&baz.foo=bar 3'
assert.deepStrictEqual(actual, expected)
})
it('should serialize text input and textarea', () => {
const dom = new JSDOM(
'<form>' +
'<input type="text" name="name" value="StimulusReflex">' +
'<textarea name="description">An exciting new way to build modern, reactive, real-time apps with Ruby on Rails.</textarea>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected =
'name=StimulusReflex&description=An exciting new way to build modern, reactive, real-time apps with Ruby on Rails.'
assert.deepStrictEqual(actual, expected)
})
it('should ignore disabled inputs', () => {
const dom = new JSDOM(
'<form>' +
'<input type="text" name="foo" value="bar 1"/>' +
'<input type="text" name="foo.bar" value="bar 2" disabled/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=bar 1'
assert.deepStrictEqual(actual, expected)
})
})
context('<input type="checkbox">', () => {
it('should serialize checkboxes', () => {
const dom = new JSDOM(
'<form>' +
'<input type="checkbox" name="foo" checked/>' +
'<input type="checkbox" name="bar"/>' +
'<input type="checkbox" name="baz" checked/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=on&baz=on'
assert.deepStrictEqual(actual, expected)
})
it('should serialize checkbox array', () => {
const dom = new JSDOM(
'<form>' +
'<input type="checkbox" name="foo[]" value="bar" checked/>' +
'<input type="checkbox" name="foo[]" value="baz" checked/>' +
'<input type="checkbox" name="foo[]" value="baz"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo[]=bar&foo[]=baz'
assert.deepStrictEqual(actual, expected)
})
it('should serialize checkbox array with one input', () => {
const dom = new JSDOM(
'<form>' +
'<input type="checkbox" name="foo[]" value="bar" checked/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo[]=bar'
assert.deepStrictEqual(actual, expected)
})
})
context('<input type="radio">', () => {
it('should serialize radio button with no checked input', () => {
const dom = new JSDOM(
'<form>' +
'<input type="radio" name="foo" value="bar1"/>' +
'<input type="radio" name="foo" value="bar2"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = ''
assert.deepStrictEqual(actual, expected)
})
it('should serialize radio button', () => {
const dom = new JSDOM(
'<form>' +
'<input type="radio" name="foo" value="bar1" checked="checked"/>' +
'<input type="radio" name="foo" value="bar2"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=bar1'
assert.deepStrictEqual(actual, expected)
})
it('should serialize radio button with empty input', () => {
const dom = new JSDOM(
'<form>' +
'<input type="radio" name="foo" value="" checked="checked"/>' +
'<input type="radio" name="foo" value="bar2"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo='
assert.deepStrictEqual(actual, expected)
})
it('should serialize radio and checkbox with the same key', () => {
const dom = new JSDOM(
'<form>' +
'<input type="radio" name="foo" value="bar1" checked="checked"/>' +
'<input type="radio" name="foo" value="bar2"/>' +
'<input type="checkbox" name="foo" value="bar3" checked="checked"/>' +
'<input type="checkbox" name="foo" value="bar4"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=bar1&foo=bar3'
assert.deepStrictEqual(actual, expected)
})
})
context('<input> - buttons', () => {
it('should not serialize buttons', () => {
const dom = new JSDOM(
'<form>' +
'<input type="submit" name="submit" value="submit"/>' +
'<input type="reset" name="reset" value="reset"/>' +
'<input type="button" name="button" value="button"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const element = dom.window.document.querySelector('input[type="submit"]')
const actual = serializeForm(form, { w: dom.window })
const expected = ''
assert.deepStrictEqual(actual, expected)
})
})
context('<input> - brackets notation', () => {
it('should serialize text inputs with brackets notation', () => {
const dom = new JSDOM(
'<form>' +
'<input type="text" name="name[]" value="StimulusReflex">' +
'<input type="text" name="name[]" value="CableReady">' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'name[]=StimulusReflex&name[]=CableReady'
assert.deepStrictEqual(actual, expected)
})
it('should serialize text inputs with nested brackets notation', () => {
const dom = new JSDOM(
'<form>' +
'<input type="email" name="account[name]" value="Foo Dude">' +
'<input type="text" name="account[email]" value="foobar@example.org">' +
'<input type="text" name="account[address][city]" value="Qux">' +
'<input type="text" name="account[address][state]" value="CA">' +
'<input type="text" name="account[address][empty]" value="">' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected =
'account[name]=Foo Dude&account[email]=foobar@example.org&account[address][city]=Qux&account[address][state]=CA&account[address][empty]='
assert.deepStrictEqual(actual, expected)
})
it('should serialize text inputs with brackets notation and nested numbered index', () => {
const dom = new JSDOM(
'<form>' +
'<input id="person_address_23_city" name="person[address][23][city]" type="text" value="Paris"/>' +
'<input id="person_address_45_city" name="person[address][45][city]" type="text" value="London" /> ' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected =
'person[address][23][city]=Paris&person[address][45][city]=London'
assert.deepStrictEqual(actual, expected)
})
it('should serialize text inputs with brackets notation and nested non-numbered index', () => {
const dom = new JSDOM(
'<form>' +
'<input id="person_address_23_city" name="person[address][23_id][city]" type="text" value="Paris"/>' +
'<input id="person_address_45_city" name="person[address][45_id][city]" type="text" value="London" />' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected =
'person[address][23_id][city]=Paris&person[address][45_id][city]=London'
assert.deepStrictEqual(actual, expected)
})
it('should serialize text inputs with non-indexed bracket notation', () => {
const dom = new JSDOM(
'<form>' +
'<input name="people[][name]" value="fred" />' +
'<input name="people[][name]" value="bob" />' +
'<input name="people[][name]" value="bubba" />' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected =
'people[][name]=fred&people[][name]=bob&people[][name]=bubba'
assert.deepStrictEqual(actual, expected)
})
it('should serialize text inputs with non-indexed nested bracket notation', () => {
const dom = new JSDOM(
'<form>' +
'<input name="user[tags][]" value="cow" />' +
'<input name="user[tags][]" value="milk" />' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'user[tags][]=cow&user[tags][]=milk'
assert.deepStrictEqual(actual, expected)
})
it('should serialize text inputs with indexed bracket notation', () => {
const dom = new JSDOM(
'<form>' +
'<input name="people[2][name]" value="bubba" />' +
'<input name="people[2][age]" value="15" />' +
'<input name="people[0][name]" value="fred" />' +
'<input name="people[0][age]" value="12" />' +
'<input name="people[1][name]" value="bob" />' +
'<input name="people[1][age]" value="14" />' +
'<input name="people[][name]" value="frank">' +
'<input name="people[3][age]" value="2">' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected =
'people[2][name]=bubba&people[2][age]=15&people[0][name]=fred&people[0][age]=12&people[1][name]=bob&people[1][age]=14&people[][name]=frank&people[3][age]=2'
assert.deepStrictEqual(actual, expected)
})
})
context('<select>', () => {
it('should serialize select', () => {
const dom = new JSDOM(
'<form>' +
'<select name="foo">' +
'<option value="bar">bar</option>' +
'<option value="baz" selected>baz</option>' +
'</select>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=baz'
assert.deepStrictEqual(actual, expected)
})
it('should serialize select with empty option', () => {
const dom = new JSDOM(
'<form>' +
'<select name="foo">' +
'<option value="">empty</option>' +
'<option value="bar">bar</option>' +
'<option value="baz">baz</option>' +
'</select>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo='
assert.deepStrictEqual(actual, expected)
})
})
context('<select multiple>', () => {
it('should serialize select multiple', () => {
const dom = new JSDOM(
'<form>' +
'<select name="foo" multiple>' +
'<option value="bar" selected>bar</option>' +
'<option value="baz">baz</option>' +
'<option value="cat" selected>cat</option>' +
'</select>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=bar&foo=cat'
assert.deepStrictEqual(actual, expected)
})
it('should serialize select multiple with empty option', () => {
const dom = new JSDOM(
'<form>' +
'<select name="foo" multiple>' +
'<option value="" selected>empty</option>' +
'<option value="bar" selected>bar</option>' +
'<option value="baz">baz</option>' +
'<option value="cat">cat</option>' +
'</select>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo=&foo=bar'
assert.deepStrictEqual(actual, expected)
})
it('should serialize select multiple with bracket notation', () => {
const dom = new JSDOM(
'<form>' +
'<select name="foo[]" multiple>' +
'<option value="bar" selected>Bar</option>' +
'<option value="baz">Baz</option>' +
'<option value="qux" selected>Qux</option>' +
'</select>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo[]=bar&foo[]=qux'
assert.deepStrictEqual(actual, expected)
})
it('should serialize select multiple with bracket notation and empty option', () => {
const dom = new JSDOM(
'<form>' +
'<select name="foo[bar]" multiple>' +
'<option selected>Default value</option>' +
'<option value="" selected>Empty value</option>' +
'<option value="baz" selected>Baz</option>' +
'<option value="qux">Qux</option>' +
'<option value="norf" selected>Norf</option>' +
'</select>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected =
'foo[bar]=Default value&foo[bar]=&foo[bar]=baz&foo[bar]=norf'
assert.deepStrictEqual(actual, expected)
})
it('should serialize select multiple with nested bracket notation', () => {
const dom = new JSDOM(
'<form>' +
'<select name="foo[bar]" multiple>' +
'<option value="baz" selected>Baz</option>' +
'<option value="qux">Qux</option>' +
'<option value="norf" selected>Norf</option>' +
'</select>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const actual = serializeForm(form, { w: dom.window })
const expected = 'foo[bar]=baz&foo[bar]=norf'
assert.deepStrictEqual(actual, expected)
})
})
context('attach input name if input triggered action', () => {
it('should serialize button if button triggered action', () => {
const dom = new JSDOM(
'<form>' +
'<input type="submit" name="commit" value="Create Post"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const element = dom.window.document.querySelector('input[type="submit"]')
const actual = serializeForm(form, { w: dom.window, element })
const expected = 'commit=Create Post'
assert.deepStrictEqual(actual, expected)
})
it('should serialize inputs and button which triggered the action', () => {
const dom = new JSDOM(
'<form>' +
'<input type="text" name="title" value="Post"/>' +
'<input type="submit" name="commit" value="Create Post"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const element = dom.window.document.querySelector('input[type="submit"]')
const actual = serializeForm(form, { w: dom.window, element })
const expected = 'title=Post&commit=Create Post'
assert.deepStrictEqual(actual, expected)
})
it('should serialize empty button if button triggered action', () => {
const dom = new JSDOM(
'<form>' + '<input type="submit" name="commit" value=""/>' + '</form>'
)
const form = dom.window.document.querySelector('form')
const element = dom.window.document.querySelector('input[type="submit"]')
const actual = serializeForm(form, { w: dom.window, element })
const expected = 'commit='
assert.deepStrictEqual(actual, expected)
})
it('should not serialize if input has no name', () => {
const dom = new JSDOM(
'<form>' +
'<input type="submit" name="" value="Create Post"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const element = dom.window.document.querySelector('input')
const actual = serializeForm(form, { w: dom.window, element })
const expected = ''
assert.deepStrictEqual(actual, expected)
})
it('should not serialize if element is no input', () => {
const dom = new JSDOM(
'<form>' +
'<input type="submit" name="commit" value="Create Post"/>' +
'<div class="foo">bar</div>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const element = dom.window.document.querySelector('div')
const actual = serializeForm(form, { w: dom.window, element })
const expected = ''
assert.deepStrictEqual(actual, expected)
})
it('should not serialize input twice if input also triggered the action', () => {
const dom = new JSDOM(
'<form>' +
'<input data-action="change->post#create" type="text" name="commit" value="Create Post"/>' +
'</form>'
)
const form = dom.window.document.querySelector('form')
const element = dom.window.document.querySelector('input')
const actual = serializeForm(form, { w: dom.window, element })
const expected = 'commit=Create Post'
assert.deepStrictEqual(actual, expected)
})
})
})