-
Notifications
You must be signed in to change notification settings - Fork 5
/
serialize.spec.js
231 lines (192 loc) · 5.42 KB
/
serialize.spec.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
/* global beforeEach, describe, it */
import 'jsdom-global/register'
import {expect} from 'chai'
import serialize from '../lib/serialize'
import domify from 'domify'
describe('serialize', () => {
describe('when serializing a text input', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<input type="text" name="foo" value="bar">' +
'</form>'
)
result = serialize(form)
})
it('should return an object with a key from the text input name', () => {
expect(result).to.have.have.ownProperty('foo')
})
it('should have the input\'s value', () => {
expect(result.foo).to.equal('bar')
})
})
describe('when serializing a text input', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<input type="text" value="bar">' +
'</form>'
)
result = serialize(form)
})
it('should not serialize the value to the target object', () => {
expect(result).to.be.ok
})
})
describe('when serializing a textarea', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<textarea name="foo">bar</textarea>' +
'</form>'
)
result = serialize(form)
})
it('should have the textarea\'s value', () => {
expect(result.foo).to.equal('bar')
})
})
describe('when serializing a select box', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<select name="foo">' +
'<option value="bar">bar</option>' +
'</select>' +
'</form>'
)
result = serialize(form)
})
it('should have the options\'s value', function () {
expect(result.foo).to.equal('bar')
})
})
describe('when serializing a multiple select box', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<select name="foo" multiple>' +
'<option value="foo">foo</option>' +
'<option value="bar" selected>bar</option>' +
'</select>' +
'</form>'
)
result = serialize(form)
})
it('should have the options\'s value as array', function () {
expect(result.foo).to.deep.equal(['bar'])
})
})
describe('when serializing a checkbox', () => {
let form
beforeEach(() => {
form = domify(
'<form>' +
'<input type="checkbox" id="the-checkbox" name="chk">' +
'</form>'
)
})
describe('and the checkbox is checked', () => {
let result
beforeEach(() => {
form.querySelector('#the-checkbox').checked = true
result = serialize(form)
})
it('should return an object with a value of true', () => {
expect(result.chk).to.be.true
})
})
describe('and the checkbox is not checked', () => {
let result
beforeEach(() => {
result = serialize(form)
})
it('should return an object with a value of false', () => {
expect(result.chk).to.be.false
})
})
})
describe('when serializing a button', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<button name="btn" value="foo">foo</button>' +
'</form>'
)
result = serialize(form)
})
it('should return an object with a value of false', () => {
expect(result.hasOwnProperty('btn')).to.be.false
})
})
describe('when serializing an input with type of "submit"', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<input type="submit" name="btn" value="foo" text="Foo">' +
'</form>'
)
result = serialize(form)
})
it('should not have the button\'s value', () => {
expect(result.hasOwnProperty('btn')).to.be.false
})
})
describe('when serializing a radio button group', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<input type="radio" name="foo" value="foo">' +
'<input type="radio" name="foo" value="bar" checked>' +
'<input type="radio" name="foo" value="baz">' +
'</form>'
)
result = serialize(form)
})
it('should only return the value of the selected radio button', () => {
expect(result.foo).to.equal('bar')
})
})
describe('when serializing an array input', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<input type="checkbox" name="foo[bar][]" value="baz" checked="checked">' +
'<input type="checkbox" name="foo[bar][]" value="biz">' +
'<input type="checkbox" name="foo[bar][]" value="qux" checked="checked">' +
'</form>'
)
result = serialize(form)
})
it('should return result as array', () => {
expect(result.foo.bar).to.deep.equal(['baz', 'qux'])
})
})
describe('when ignoring a field by selector', () => {
let result
beforeEach(() => {
let form = domify(
'<form>' +
'<input name="a">' +
'<input name="b" class="doNotSerializeMe">' +
'<input name="c">' +
'<input name="d" class="doNotSerializeMe">' +
'<button name="e">' +
'</form>'
)
result = serialize(form, {ignoredTypes: ['.doNotSerializeMe']})
})
it('should not include fields excluded by selector', () => {
expect(result).to.eql({a: '', c: ''})
})
})
})