Debian Codegraph @

www/public/test/core/parameters.js

raw symbols SCIP
1 describe('Core htmx Parameter Handling', function() {
2 beforeEach(function() {
3 this.server = makeServer()
4 clearWorkArea()
5 })
6
7 afterEach(function() {
8 this.server.restore()
9 clearWorkArea()
10 })
11
12 it('Input includes value', function() {
13 var input = make('<input name="foo" value="bar"/>')
14 var vals = htmx._('getInputValues')(input).values
15 vals.foo.should.equal('bar')
16 })
17
18 it('Input includes value on get', function() {
19 var input = make('<input name="foo" value="bar"/>')
20 var vals = htmx._('getInputValues')(input, 'get').values
21 vals.foo.should.equal('bar')
22 })
23
24 it('Input includes form', function() {
25 var form = make('<form><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/></form>')
26 var input = byId('i1')
27 var vals = htmx._('getInputValues')(input).values
28 vals.foo.should.equal('bar')
29 vals.do.should.equal('rey')
30 })
31
32 it('Input doesnt include form on get', function() {
33 var form = make('<form><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/></form>')
34 var input = byId('i1')
35 var vals = htmx._('getInputValues')(input, 'get').values
36 vals.foo.should.equal('bar')
37 should.equal(vals.do, undefined)
38 })
39
40 it('non-input includes form', function() {
41 var form = make('<form><div id="d1"/><input id="i2" name="do" value="rey"/></form>')
42 var div = byId('d1')
43 var vals = htmx._('getInputValues')(div, 'post').values
44 vals.do.should.equal('rey')
45 })
46
47 it('non-input doesnt include form on get', function() {
48 var form = make('<form><div id="d1"/><input id="i2" name="do" value="rey"/></form>')
49 var div = byId('d1')
50 var vals = htmx._('getInputValues')(div, 'get').values
51 should.equal(vals.do, undefined)
52 })
53
54 it('Basic form works on get', function() {
55 var form = make('<form><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/></form>')
56 var vals = htmx._('getInputValues')(form, 'get').values
57 vals.foo.should.equal('bar')
58 vals.do.should.equal('rey')
59 })
60
61 it('Basic form works on non-get', function() {
62 var form = make('<form><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/></form>')
63 var vals = htmx._('getInputValues')(form, 'post').values
64 vals.foo.should.equal('bar')
65 vals.do.should.equal('rey')
66 })
67
68 it('Double values are included as array', function() {
69 var form = make('<form><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input id="i2" name="do" value="rey"/></form>')
70 var vals = htmx._('getInputValues')(form).values
71 vals.foo.should.equal('bar')
72 vals.do.should.deep.equal(['rey', 'rey'])
73 })
74
75 it('Double values are included as array in correct order', function() {
76 var form = make('<form><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey1"/><input id="i3" name="do" value="rey2"/></form>')
77 var vals = htmx._('getInputValues')(byId('i3')).values
78 vals.foo.should.equal('bar')
79 vals.do.should.deep.equal(['rey1', 'rey2'])
80 })
81
82 it('Double empty values are included as array in correct order', function() {
83 var form = make('<form><input id="i1" name="do" value=""/><input id="i2" name="do" value="rey"/><input id="i3" name="do" value=""/></form>')
84 var vals = htmx._('getInputValues')(byId('i3')).values
85 vals.do.should.deep.equal(['', 'rey', ''])
86 })
87
88 it('hx-include works with form', function() {
89 var form = make('<form id="f1"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input id="i2" name="do" value="rey"/></form>')
90 var div = make('<div hx-include="#f1"></div>')
91 var vals = htmx._('getInputValues')(div).values
92 vals.foo.should.equal('bar')
93 vals.do.should.deep.equal(['rey', 'rey'])
94 })
95
96 it('hx-include works with input', function() {
97 var form = make('<form id="f1"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input id="i2" name="do" value="rey"/></form>')
98 var div = make('<div hx-include="#i1"></div>')
99 var vals = htmx._('getInputValues')(div).values
100 vals.foo.should.equal('bar')
101 should.equal(vals.do, undefined)
102 })
103
104 it('hx-include works with two inputs', function() {
105 var form = make('<form id="f1"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input id="i2" name="do" value="rey"/></form>')
106 var div = make('<div hx-include="#i1, #i2"></div>')
107 var vals = htmx._('getInputValues')(div).values
108 vals.foo.should.equal('bar')
109 vals.do.should.deep.equal(['rey', 'rey'])
110 })
111
112 it('hx-include works with two inputs, plus form', function() {
113 var form = make('<form id="f1"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input id="i2" name="do" value="rey"/></form>')
114 var div = make('<div hx-include="#i1, #i2, #f1"></div>')
115 var vals = htmx._('getInputValues')(div).values
116 vals.foo.should.equal('bar')
117 vals.do.should.deep.equal(['rey', 'rey'])
118 })
119
120 it('correctly URL escapes values', function() {
121 htmx._('urlEncode')({}).should.equal('')
122 htmx._('urlEncode')({ foo: 'bar' }).should.equal('foo=bar')
123 htmx._('urlEncode')({ foo: 'bar', do: 'rey' }).should.equal('foo=bar&do=rey')
124 htmx._('urlEncode')({ foo: 'bar', do: ['rey', 'blah'] }).should.equal('foo=bar&do=rey&do=blah')
125 })
126
127 it('form includes last focused button', function(done) {
128 var form = make('<form hx-get="/foo"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><button id="b1" name="btn" value="bar"></button></form>')
129 var input = byId('i1')
130 var button = byId('b1')
131 // Listen for focusin on form as it'll bubble up from the button, and htmx binds on the form itself
132 form.addEventListener('focusin', function() {
133 var vals = htmx._('getInputValues')(form).values
134 vals.foo.should.equal('bar')
135 vals.do.should.equal('rey')
136 vals.btn.should.equal('bar')
137 done()
138 }, { once: true })
139 button.focus()
140 // Headless / Hardly-throttled CPU might result in 'focusin' not being fired, double it just in case
141 htmx.trigger(button, 'focusin')
142 })
143
144 it('form includes last focused submit', function(done) {
145 var form = make('<form hx-get="/foo"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input type="submit" id="s1" name="s1" value="bar"/></form>')
146 var input = byId('i1')
147 var button = byId('s1')
148 // Listen for focusin on form as it'll bubble up from the button, and htmx binds on the form itself
149 form.addEventListener('focusin', function() {
150 var vals = htmx._('getInputValues')(form).values
151 vals.foo.should.equal('bar')
152 vals.do.should.equal('rey')
153 vals.s1.should.equal('bar')
154 done()
155 }, { once: true })
156 button.focus()
157 // Headless / Hardly-throttled CPU might result in 'focusin' not being fired, double it just in case
158 htmx.trigger(button, 'focusin')
159 })
160
161 it('form does not include button when focus is lost', function() {
162 var form = make('<form hx-get="/foo"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input type="submit" id="s1" name="s1" value="bar"/></form>')
163 var input = byId('i1')
164 var button = byId('s1')
165 button.focus()
166 input.focus()
167 var vals = htmx._('getInputValues')(form).values
168 vals.foo.should.equal('bar')
169 vals.do.should.equal('rey')
170 should.equal(vals.s1, undefined)
171 })
172
173 it('form does not include button when focus is lost outside of form', function() {
174 var form = make('<form hx-get="/foo"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input type="submit" id="s1" name="s1" value="bar"/></form>')
175 var anchor = make('<button id="a1"></button>')
176 var button = byId('s1')
177 button.focus()
178 anchor.focus()
179 var vals = htmx._('getInputValues')(form).values
180 vals.foo.should.equal('bar')
181 vals.do.should.equal('rey')
182 should.equal(vals.s1, undefined)
183 })
184
185 it('form includes button name and value if button has nested elements when clicked', function() {
186 var form = make('<form hx-get="/foo"><input id="i1" name="foo" value="bar"/><button type="submit" id="btn1" name="do" value="rey"><div id="div1"><span id="span1"></span></div></button></form>')
187 var nestedElt = byId('span1')
188 nestedElt.click()
189 var vals = htmx._('getInputValues')(form).values
190 vals.do.should.equal('rey')
191 })
192
193 it('it puts GET params in the URL by default', function() {
194 this.server.respondWith('GET', '/test?i1=value', function(xhr) {
195 xhr.respond(200, {}, 'Clicked!')
196 })
197 var form = make('<form hx-trigger="click" hx-get="/test"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>')
198 form.click()
199 this.server.respond()
200 form.innerHTML.should.equal('Clicked!')
201 })
202
203 it('it puts GET params in the body if methodsThatUseUrlParams is empty', function() {
204 this.server.respondWith('GET', '/test', function(xhr) {
205 xhr.requestBody.should.equal('i1=value')
206 xhr.respond(200, {}, 'Clicked!')
207 })
208 var form = make('<form hx-trigger="click" hx-get="/test"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>')
209
210 try {
211 htmx.config.methodsThatUseUrlParams = []
212 form.click()
213 this.server.respond()
214 form.innerHTML.should.equal('Clicked!')
215 } finally {
216 htmx.config.methodsThatUseUrlParams = ['get']
217 }
218 })
219
220 it('it puts DELETE params in the body by default', function() {
221 this.server.respondWith('DELETE', '/test', function(xhr) {
222 xhr.requestBody.should.equal('i1=value')
223 xhr.respond(200, {}, 'Clicked!')
224 })
225 var form = make('<form hx-trigger="click" hx-delete="/test"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>')
226 form.click()
227 this.server.respond()
228 form.innerHTML.should.equal('Clicked!')
229 })
230
231 it('it puts DELETE params in the URL if methodsThatUseUrlParams contains "delete"', function() {
232 this.server.respondWith('DELETE', '/test?i1=value', function(xhr) {
233 xhr.respond(200, {}, 'Clicked!')
234 })
235 var form = make('<form hx-trigger="click" hx-delete="/test"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>')
236
237 try {
238 htmx.config.methodsThatUseUrlParams.push('delete')
239 form.click()
240 this.server.respond()
241 form.innerHTML.should.equal('Clicked!')
242 } finally {
243 htmx.config.methodsThatUseUrlParams = ['get']
244 }
245 })
246
247 it('Input within disabled fieldset is excluded', function() {
248 var input = make('<form><input name="foo" value="bar"/><fieldset disabled><input name="do" value="rey"/></fieldset></form>')
249 var vals = htmx._('getInputValues')(input, 'get').values
250 vals.foo.should.equal('bar')
251 should.equal(vals.do, undefined)
252 })
253
254 it('formdata works along web components', function() {
255 // See https://web.dev/articles/more-capable-form-controls
256 class TestElement extends HTMLElement {
257 static formAssociated = true
258 constructor() {
259 super()
260 this._form = null
261 }
262
263 formAssociatedCallback(form) {
264 if (this._form) {
265 this._form.removeEventListener('formdata', this.handleFormData)
266 }
267 this._form = form
268 this._form.addEventListener('formdata', this.handleFormData)
269 }
270
271 handleFormData({
272 formData
273 }) {
274 formData.append('foo', 'bar')
275 }
276 }
277 customElements.define('test-element', TestElement)
278
279 var form = make('<form hx-post="/test"><test-element></test-element></form>')
280 var vals = htmx._('getInputValues')(form, 'get').values
281 vals.foo.should.equal('bar')
282 })
283
284 it('formdata works with null values', function() {
285 var form = make('<form hx-post="/test"><input name="foo" value="bar"/></form>')
286 var vals = htmx._('getInputValues')(form, 'get').values
287 function updateToNull() { vals.foo = null }
288 updateToNull.should.not.throw()
289 vals.foo.should.equal('null')
290 })
291
292 it('formdata can be used to construct a URLSearchParams instance', function() {
293 var form = make('<input name="foo" value="bar"/>')
294 var vals = htmx._('getInputValues')(form, 'get').values
295 function makeSearchParams() { return new URLSearchParams(vals).toString() }
296 makeSearchParams.should.not.throw()
297 makeSearchParams().should.equal('foo=bar')
298 })
299
300 it('order of parameters follows order of input elements', function() {
301 this.server.respondWith('GET', '/test?foo=bar&bar=foo&foo=bar&foo2=bar2', function(xhr) {
302 xhr.respond(200, {}, 'Clicked!')
303 })
304
305 var form = make('<form hx-get="/test">' +
306 '<input name="foo" value="bar">' +
307 '<input name="bar" value="foo">' +
308 '<input name="foo" value="bar">' +
309 '<input name="foo2" value="bar2">' +
310 '<button id="b1">Click Me!</button>' +
311 '</form>')
312
313 byId('b1').click()
314 this.server.respond()
315 form.innerHTML.should.equal('Clicked!')
316 })
317
318 it('order of parameters follows order of input elements with POST', function() {
319 this.server.respondWith('POST', '/test', function(xhr) {
320 xhr.requestBody.should.equal('foo=bar&bar=foo&foo=bar&foo2=bar2')
321 xhr.respond(200, {}, 'Clicked!')
322 })
323
324 var form = make('<form hx-post="/test">' +
325 '<input name="foo" value="bar">' +
326 '<input name="bar" value="foo">' +
327 '<input name="foo" value="bar">' +
328 '<input name="foo2" value="bar2">' +
329 '<button id="b1">Click Me!</button>' +
330 '</form>')
331
332 byId('b1').click()
333 this.server.respond()
334 form.innerHTML.should.equal('Clicked!')
335 })
336
337 it('file is correctly uploaded with file input', function() {
338 this.server.respondWith('POST', '/test', function(xhr) {
339 should.equal(xhr.requestHeaders['Content-Type'], undefined)
340
341 const file = xhr.requestBody.get('file')
342 file.should.instanceOf(File)
343 file.name.should.equal('test.txt')
344
345 xhr.respond(200, {}, 'OK')
346 })
347
348 const form = make('<form hx-post="/test" hx-target="#result" hx-encoding="multipart/form-data">' +
349 '<input type="file" name="file">' +
350 '<button type="submit"></button>' +
351 '</form>')
352 const input = form.querySelector('input')
353 const file = new File(['Test'], 'test.txt', { type: 'text/plain' })
354 const dataTransfer = new DataTransfer()
355 dataTransfer.items.add(file)
356 input.files = dataTransfer.files
357
358 const result = make('<div id="result"></div>')
359
360 form.querySelector('button').click()
361 this.server.respond()
362 result.innerHTML.should.equal('OK')
363 })
364
365 it('file is not uploaded with blank filename', function() {
366 this.server.respondWith('POST', '/test', function(xhr) {
367 should.equal(xhr.requestHeaders['Content-Type'], undefined)
368
369 const file = xhr.requestBody.get('file')
370 should.equal(file, null)
371
372 xhr.respond(200, {}, 'OK')
373 })
374
375 const form = make('<form hx-post="/test" hx-target="#result" hx-encoding="multipart/form-data">' +
376 '<input type="file" name="file">' +
377 '<button type="submit"></button>' +
378 '</form>')
379 const input = form.querySelector('input')
380 const file = new File(['Test'], '', { type: 'text/plain' })
381 const dataTransfer = new DataTransfer()
382 dataTransfer.items.add(file)
383 input.files = dataTransfer.files
384
385 const result = make('<div id="result"></div>')
386
387 form.querySelector('button').click()
388 this.server.respond()
389 result.innerHTML.should.equal('OK')
390 })
391
392 it('file is correctly uploaded with htmx.ajax', function() {
393 this.server.respondWith('POST', '/test', function(xhr) {
394 should.equal(xhr.requestHeaders['Content-Type'], undefined)
395
396 const file = xhr.requestBody.get('file')
397 file.should.instanceOf(File)
398 file.name.should.equal('test.txt')
399
400 xhr.respond(200, {}, 'OK')
401 })
402
403 const div = make('<div hx-encoding="multipart/form-data"></div>')
404
405 htmx.ajax('POST', '/test', {
406 source: div,
407 values: {
408 file: new File(['Test'], 'test.txt', { type: 'text/plain' })
409 }
410 })
411
412 this.server.respond()
413 div.innerHTML.should.equal('OK')
414 })
415 })