Debian Codegraph @

www/public/test/attributes/hx-swap.js

raw symbols SCIP
1 describe('hx-swap attribute', function() {
2 beforeEach(function() {
3 this.server = makeServer()
4 clearWorkArea()
5 })
6 afterEach(function() {
7 this.server.restore()
8 clearWorkArea()
9 })
10
11 it('swap innerHTML properly', function() {
12 this.server.respondWith('GET', '/test', '<a hx-get="/test2">Click Me</a>')
13 this.server.respondWith('GET', '/test2', 'Clicked!')
14
15 var div = make('<div hx-get="/test"></div>')
16 div.click()
17 this.server.respond()
18 div.innerHTML.should.equal('<a hx-get="/test2">Click Me</a>')
19 var a = div.querySelector('a')
20 a.click()
21 this.server.respond()
22 a.innerHTML.should.equal('Clicked!')
23 })
24
25 it('swap textContent properly with HTML tags', function() {
26 this.server.respondWith('GET', '/test', '<a id="a1" hx-get="/test2">Click Me</a>')
27
28 var d1 = make('<div id="d1" hx-get="/test" hx-swap="textContent"></div>')
29 d1.click()
30 should.equal(byId('d1'), d1)
31 this.server.respond()
32 d1.textContent.should.equal('<a id="a1" hx-get="/test2">Click Me</a>')
33 should.equal(byId('a1'), null)
34 })
35
36 it('swap textContent properly with HTML tags and text', function() {
37 this.server.respondWith('GET', '/test', 'text content <a id="a1" hx-get="/test2">Click Me</a>')
38
39 var d1 = make('<div id="d1" hx-get="/test" hx-swap="textContent"></div>')
40 d1.click()
41 should.equal(byId('d1'), d1)
42 this.server.respond()
43 d1.textContent.should.equal('text content <a id="a1" hx-get="/test2">Click Me</a>')
44 should.equal(byId('a1'), null)
45 })
46
47 it('swap textContent ignores OOB swaps', function() {
48 this.server.respondWith('GET', '/test', '<span id="d2" hx-swap-oob="true">hi</span> <a id="a1" hx-get="/test2">Click Me</a>')
49
50 var d1 = make('<div id="d1" hx-get="/test" hx-swap="textContent"></div>')
51 var d2 = make('<div id="d2">some text</div>')
52 d1.click()
53 should.equal(byId('d1'), d1)
54 should.equal(byId('d2'), d2)
55 this.server.respond()
56 d1.textContent.should.equal('<span id="d2" hx-swap-oob="true">hi</span> <a id="a1" hx-get="/test2">Click Me</a>')
57 d2.outerHTML.should.equal('<div id="d2">some text</div>')
58 should.equal(byId('a1'), null)
59 })
60
61 it('swap textContent properly with text', function() {
62 this.server.respondWith('GET', '/test', 'plain text')
63
64 var div = make('<div id="d1" hx-get="/test" hx-swap="textContent"></div>')
65 div.click()
66 should.equal(byId('d1'), div)
67 this.server.respond()
68 div.textContent.should.equal('plain text')
69 should.equal(byId('a1'), null)
70 })
71
72 it('swap outerHTML properly', function() {
73 this.server.respondWith('GET', '/test', '<a id="a1" hx-get="/test2">Click Me</a>')
74 this.server.respondWith('GET', '/test2', 'Clicked!')
75
76 var div = make('<div id="d1" hx-get="/test" hx-swap="outerHTML"></div>')
77 div.click()
78 should.equal(byId('d1'), div)
79 this.server.respond()
80 should.equal(byId('d1'), null)
81 byId('a1').click()
82 this.server.respond()
83 byId('a1').innerHTML.should.equal('Clicked!')
84 })
85
86 it('swap outerHTML on body falls back to innerHTML properly', function() {
87 var fakebody = htmx._('parseHTML')('<body id="b1">Old Content</body>')
88 var wa = getWorkArea()
89 var fragment = htmx._('makeFragment')('<body hx-get="/test" hx-swap="outerHTML">Changed!</body>')
90 wa.append(fakebody.querySelector('body'))
91 htmx._('swapOuterHTML')(byId('b1'), fragment, {})
92 byId('b1').innerHTML.should.equal('Changed!')
93 })
94
95 it('swap beforebegin properly', function() {
96 var i = 0
97 this.server.respondWith('GET', '/test', function(xhr) {
98 i++
99 xhr.respond(200, {}, '<a id="a' + i + '" hx-get="/test2" hx-swap="innerHTML">' + i + '</a>')
100 })
101 this.server.respondWith('GET', '/test2', '*')
102
103 var div = make('<div hx-get="/test" hx-swap="beforebegin">*</div>')
104 var parent = div.parentElement
105 div.click()
106 this.server.respond()
107 div.innerText.should.equal('*')
108 removeWhiteSpace(parent.innerText).should.equal('1*')
109
110 byId('a1').click()
111 this.server.respond()
112 removeWhiteSpace(parent.innerText).should.equal('**')
113
114 div.click()
115 this.server.respond()
116 div.innerText.should.equal('*')
117 removeWhiteSpace(parent.innerText).should.equal('*2*')
118
119 byId('a2').click()
120 this.server.respond()
121 removeWhiteSpace(parent.innerText).should.equal('***')
122 })
123
124 it('swap afterbegin properly', function() {
125 var i = 0
126 this.server.respondWith('GET', '/test', function(xhr) {
127 i++
128 xhr.respond(200, {}, '' + i)
129 })
130
131 var div = make('<div hx-get="/test" hx-swap="afterbegin">*</div>')
132
133 div.click()
134 this.server.respond()
135 div.innerText.should.equal('1*')
136
137 div.click()
138 this.server.respond()
139 div.innerText.should.equal('21*')
140
141 div.click()
142 this.server.respond()
143 div.innerText.should.equal('321*')
144 })
145
146 it('swap afterbegin properly with no initial content', function() {
147 var i = 0
148 this.server.respondWith('GET', '/test', function(xhr) {
149 i++
150 xhr.respond(200, {}, '' + i)
151 })
152
153 var div = make('<div hx-get="/test" hx-swap="afterbegin"></div>')
154
155 div.click()
156 this.server.respond()
157 div.innerText.should.equal('1')
158
159 div.click()
160 this.server.respond()
161 div.innerText.should.equal('21')
162
163 div.click()
164 this.server.respond()
165 div.innerText.should.equal('321')
166 })
167
168 it('swap afterend properly', function() {
169 var i = 0
170 this.server.respondWith('GET', '/test', function(xhr) {
171 i++
172 xhr.respond(200, {}, '<a id="a' + i + '" hx-get="/test2" hx-swap="innerHTML">' + i + '</a>')
173 })
174 this.server.respondWith('GET', '/test2', '*')
175
176 var div = make('<div hx-get="/test" hx-swap="afterend">*</div>')
177 var parent = div.parentElement
178 div.click()
179 this.server.respond()
180 div.innerText.should.equal('*')
181 removeWhiteSpace(parent.innerText).should.equal('*1')
182
183 byId('a1').click()
184 this.server.respond()
185 removeWhiteSpace(parent.innerText).should.equal('**')
186
187 div.click()
188 this.server.respond()
189 div.innerText.should.equal('*')
190 removeWhiteSpace(parent.innerText).should.equal('*2*')
191
192 byId('a2').click()
193 this.server.respond()
194 removeWhiteSpace(parent.innerText).should.equal('***')
195 })
196
197 it('handles beforeend properly', function() {
198 var i = 0
199 this.server.respondWith('GET', '/test', function(xhr) {
200 i++
201 xhr.respond(200, {}, '' + i)
202 })
203
204 var div = make('<div hx-get="/test" hx-swap="beforeend">*</div>')
205
206 div.click()
207 this.server.respond()
208 div.innerText.should.equal('*1')
209
210 div.click()
211 this.server.respond()
212 div.innerText.should.equal('*12')
213
214 div.click()
215 this.server.respond()
216 div.innerText.should.equal('*123')
217 })
218
219 it('handles beforeend properly with no initial content', function() {
220 var i = 0
221 this.server.respondWith('GET', '/test', function(xhr) {
222 i++
223 xhr.respond(200, {}, '' + i)
224 })
225
226 var div = make('<div hx-get="/test" hx-swap="beforeend"></div>')
227
228 div.click()
229 this.server.respond()
230 div.innerText.should.equal('1')
231
232 div.click()
233 this.server.respond()
234 div.innerText.should.equal('12')
235
236 div.click()
237 this.server.respond()
238 div.innerText.should.equal('123')
239 })
240
241 it('properly parses various swap specifications', function() {
242 var swapSpec = htmx._('getSwapSpecification') // internal function for swap spec
243 swapSpec(make('<div/>')).swapStyle.should.equal('innerHTML')
244 swapSpec(make("<div hx-swap='innerHTML'/>")).swapStyle.should.equal('innerHTML')
245 swapSpec(make("<div hx-swap='innerHTML'/>")).swapDelay.should.equal(0)
246 swapSpec(make("<div hx-swap='innerHTML'/>")).settleDelay.should.equal(0) // set to 0 in tests
247 swapSpec(make("<div hx-swap='innerHTML swap:10'/>")).swapDelay.should.equal(10)
248 swapSpec(make("<div hx-swap='innerHTML swap:0'/>")).swapDelay.should.equal(0)
249 swapSpec(make("<div hx-swap='innerHTML swap:0ms'/>")).swapDelay.should.equal(0)
250 swapSpec(make("<div hx-swap='innerHTML settle:10'/>")).settleDelay.should.equal(10)
251 swapSpec(make("<div hx-swap='innerHTML settle:0'/>")).settleDelay.should.equal(0)
252 swapSpec(make("<div hx-swap='innerHTML settle:0s'/>")).settleDelay.should.equal(0)
253 swapSpec(make("<div hx-swap='innerHTML swap:10 settle:11'/>")).swapDelay.should.equal(10)
254 swapSpec(make("<div hx-swap='innerHTML swap:10 settle:11'/>")).settleDelay.should.equal(11)
255 swapSpec(make("<div hx-swap='innerHTML settle:11 swap:10'/>")).swapDelay.should.equal(10)
256 swapSpec(make("<div hx-swap='innerHTML settle:11 swap:10'/>")).settleDelay.should.equal(11)
257 swapSpec(make("<div hx-swap='innerHTML settle:0 swap:0'/>")).settleDelay.should.equal(0)
258 swapSpec(make("<div hx-swap='innerHTML settle:0 swap:0'/>")).settleDelay.should.equal(0)
259 swapSpec(make("<div hx-swap='innerHTML settle:0s swap:0ms'/>")).settleDelay.should.equal(0)
260 swapSpec(make("<div hx-swap='innerHTML settle:0s swap:0ms'/>")).settleDelay.should.equal(0)
261 swapSpec(make("<div hx-swap='innerHTML nonsense settle:11 swap:10'/>")).settleDelay.should.equal(11)
262 swapSpec(make("<div hx-swap='innerHTML nonsense settle:11 swap:10 '/>")).settleDelay.should.equal(11)
263
264 swapSpec(make("<div hx-swap='swap:10'/>")).swapStyle.should.equal('innerHTML')
265 swapSpec(make("<div hx-swap='swap:10'/>")).swapDelay.should.equal(10)
266 swapSpec(make("<div hx-swap='swap:0'/>")).swapDelay.should.equal(0)
267 swapSpec(make("<div hx-swap='swap:0s'/>")).swapDelay.should.equal(0)
268
269 swapSpec(make("<div hx-swap='settle:10'/>")).swapStyle.should.equal('innerHTML')
270 swapSpec(make("<div hx-swap='settle:10'/>")).settleDelay.should.equal(10)
271 swapSpec(make("<div hx-swap='settle:0'/>")).settleDelay.should.equal(0)
272 swapSpec(make("<div hx-swap='settle:0s'/>")).settleDelay.should.equal(0)
273
274 swapSpec(make("<div hx-swap='swap:10 settle:11'/>")).swapStyle.should.equal('innerHTML')
275 swapSpec(make("<div hx-swap='swap:10 settle:11'/>")).swapDelay.should.equal(10)
276 swapSpec(make("<div hx-swap='swap:10 settle:11'/>")).settleDelay.should.equal(11)
277 swapSpec(make("<div hx-swap='swap:0s settle:0'/>")).swapDelay.should.equal(0)
278 swapSpec(make("<div hx-swap='swap:0s settle:0'/>")).settleDelay.should.equal(0)
279
280 swapSpec(make("<div hx-swap='settle:11 swap:10'/>")).swapStyle.should.equal('innerHTML')
281 swapSpec(make("<div hx-swap='settle:11 swap:10'/>")).swapDelay.should.equal(10)
282 swapSpec(make("<div hx-swap='settle:11 swap:10'/>")).settleDelay.should.equal(11)
283 swapSpec(make("<div hx-swap='settle:0s swap:10'/>")).swapDelay.should.equal(10)
284 swapSpec(make("<div hx-swap='settle:0s swap:10'/>")).settleDelay.should.equal(0)
285
286 swapSpec(make("<div hx-swap='transition:true'/>")).transition.should.equal(true)
287
288 swapSpec(make("<div hx-swap='customstyle settle:11 swap:10'/>")).swapStyle.should.equal('customstyle')
289 })
290
291 it('works with a swap delay', function(done) {
292 this.server.respondWith('GET', '/test', 'Clicked!')
293 var div = make("<div hx-get='/test' hx-swap='innerHTML swap:10ms'></div>")
294 div.click()
295 this.server.respond()
296 div.innerText.should.equal('')
297 setTimeout(function() {
298 div.innerText.should.equal('Clicked!')
299 done()
300 }, 30)
301 })
302
303 it('works immediately with no swap delay', function(done) {
304 this.server.respondWith('GET', '/test', 'Clicked!')
305 var div = make(
306 "<div hx-get='/test' hx-swap='innerHTML swap:0ms'></div>"
307 )
308 div.click()
309 this.server.respond()
310 div.innerText.should.equal('Clicked!')
311 done()
312 })
313
314 if (/chrome/i.test(navigator.userAgent)) {
315 it('works with transition:true', function(done) {
316 this.server.respondWith('GET', '/test', 'Clicked!')
317 var div = make(
318 "<div hx-get='/test' hx-swap='innerHTML transition:true'></div>"
319 )
320 div.click()
321 this.server.respond()
322 div.innerText.should.equal('')
323 setTimeout(function() {
324 div.innerText.should.equal('Clicked!')
325 done()
326 }, 50)
327 })
328 }
329 it('works with a settle delay', function(done) {
330 this.server.respondWith('GET', '/test', "<div id='d1' class='foo' hx-get='/test' hx-swap='outerHTML settle:10ms'></div>")
331 var div = make("<div id='d1' hx-get='/test' hx-swap='outerHTML settle:10ms'></div>")
332 div.click()
333 this.server.respond()
334 div.classList.contains('foo').should.equal(false)
335 setTimeout(function() {
336 byId('d1').classList.contains('foo').should.equal(true)
337 done()
338 }, 30)
339 })
340
341 it('works with no settle delay', function(done) {
342 this.server.respondWith(
343 'GET',
344 '/test',
345 "<div id='d1' class='foo' hx-get='/test' hx-swap='outerHTML settle:0ms'></div>"
346 )
347 var div = make(
348 "<div id='d1' hx-get='/test' hx-swap='outerHTML settle:0ms'></div>"
349 )
350 div.click()
351 this.server.respond()
352 div.classList.contains('foo').should.equal(false)
353 setTimeout(function() {
354 byId('d1').classList.contains('foo').should.equal(true)
355 done()
356 }, 30)
357 })
358
359 it('works with scroll:top', function(done) {
360 this.server.respondWith('GET', '/test', "<div id='d1' class='foo' hx-get='/test' hx-swap='outerHTML scroll:#container:top'></div>")
361 var div = make("<div id='d1' hx-get='/test' hx-swap='outerHTML scroll:#container:top'></div>")
362 var container = make('<div id="container" style="overflow: scroll; height: 150px; width: 150px;">' +
363 '<p>' +
364 'Far out in the uncharted backwaters of the unfashionable end of the western' +
365 'spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this' +
366 'at a distance of roughly ninety-two million miles is an utterly' +
367 'insignificant little blue green planet whose ape-descended life forms are so' +
368 'amazingly primitive that they still think digital watches are a pretty neat' +
369 'idea.' +
370 '</p>' +
371 '</div>')
372 container.scrollTop = 10
373 div.click()
374 this.server.respond()
375 div.classList.contains('foo').should.equal(false)
376 setTimeout(function() {
377 byId('d1').classList.contains('foo').should.equal(true)
378 container.scrollTop.should.equal(0)
379 done()
380 }, 30)
381 })
382
383 it('works with scroll:bottom', function(done) {
384 this.server.respondWith('GET', '/test', "<div id='d1' class='foo' hx-get='/test' hx-swap='outerHTML scroll:#container:bottom'></div>")
385 var div = make("<div id='d1' hx-get='/test' hx-swap='outerHTML scroll:#container:bottom'></div>")
386 var container = make('<div id="container" style="overflow: scroll; height: 150px; width: 150px;">' +
387 '<p>' +
388 'Far out in the uncharted backwaters of the unfashionable end of the western' +
389 'spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this' +
390 'at a distance of roughly ninety-two million miles is an utterly' +
391 'insignificant little blue green planet whose ape-descended life forms are so' +
392 'amazingly primitive that they still think digital watches are a pretty neat' +
393 'idea.' +
394 '</p>' +
395 '</div>')
396 container.scrollTop = 10
397 div.click()
398 this.server.respond()
399 div.classList.contains('foo').should.equal(false)
400 setTimeout(function() {
401 byId('d1').classList.contains('foo').should.equal(true)
402 container.scrollTop.should.not.equal(10)
403 done()
404 }, 30)
405 })
406
407 it('works with show:top', function(done) {
408 this.server.respondWith('GET', '/test', "<div id='d1' class='foo' hx-get='/test' hx-swap='outerHTML show:top'></div>")
409 var div = make("<div id='d1' hx-get='/test' hx-swap='outerHTML show:#d2:top'></div>")
410 var div2 = make("<div id='d2'></div>")
411 var scrollOptions
412 div2.scrollIntoView = function(options) { scrollOptions = options }
413 div.click()
414 this.server.respond()
415 div.classList.contains('foo').should.equal(false)
416 setTimeout(function() {
417 byId('d1').classList.contains('foo').should.equal(true)
418 scrollOptions.block.should.equal('start')
419 done()
420 }, 30)
421 })
422
423 it('works with show:bottom', function(done) {
424 this.server.respondWith('GET', '/test', "<div id='d1' class='foo' hx-get='/test' hx-swap='outerHTML show:bottom'></div>")
425 var div = make("<div id='d1' hx-get='/test' hx-swap='outerHTML show:#d2:bottom'></div>")
426 var div2 = make("<div id='d2'></div>")
427 var scrollOptions
428 div2.scrollIntoView = function(options) { scrollOptions = options }
429 div.click()
430 this.server.respond()
431 div.classList.contains('foo').should.equal(false)
432 setTimeout(function() {
433 byId('d1').classList.contains('foo').should.equal(true)
434 scrollOptions.block.should.equal('end')
435 done()
436 }, 30)
437 })
438
439 it('works with show:window:bottom', function(done) {
440 this.server.respondWith('GET', '/test', "<div id='d1' class='foo' hx-get='/test' hx-swap='outerHTML show:window:bottom'></div>")
441 var div = make("<div id='d1' hx-get='/test' hx-swap='outerHTML show:window:bottom'></div>")
442 var scrollOptions
443 document.body.scrollIntoView = function(options) { scrollOptions = options }
444 div.click()
445 this.server.respond()
446 div.classList.contains('foo').should.equal(false)
447 setTimeout(function() {
448 byId('d1').classList.contains('foo').should.equal(true)
449 scrollOptions.block.should.equal('end')
450 done()
451 }, 30)
452 })
453
454 it('works with focus-scroll:true', function(done) {
455 // no easy way to tell if the scroll worked as expected
456 this.server.respondWith('GET', '/test', "<div id='d1' class='foo' hx-get='/test' hx-swap='outerHTML focus-scroll:true'><input id='i2' type='text'></div>")
457 var div = make("<div id='d1' hx-get='/test' hx-swap='outerHTML focus-scroll:true'><input id='i2' type='text'></div>")
458 byId('i2').focus()
459 div.click()
460 this.server.respond()
461 div.classList.contains('foo').should.equal(false)
462 setTimeout(function() {
463 byId('d1').classList.contains('foo').should.equal(true)
464 done()
465 }, 30)
466 })
467
468 it('swap outerHTML properly w/ data-* prefix', function() {
469 this.server.respondWith('GET', '/test', '<a id="a1" data-hx-get="/test2">Click Me</a>')
470 this.server.respondWith('GET', '/test2', 'Clicked!')
471
472 var div = make('<div id="d1" data-hx-get="/test" data-hx-swap="outerHTML"></div>')
473 div.click()
474 should.equal(byId('d1'), div)
475 this.server.respond()
476 should.equal(byId('d1'), null)
477 byId('a1').click()
478 this.server.respond()
479 byId('a1').innerHTML.should.equal('Clicked!')
480 })
481
482 it('swap none works properly', function() {
483 this.server.respondWith('GET', '/test', 'Ooops, swapped')
484
485 var div = make('<div hx-swap="none" hx-get="/test">Foo</div>')
486 div.click()
487 this.server.respond()
488 div.innerHTML.should.equal('Foo')
489 })
490
491 it('swap outerHTML does not trigger htmx:afterSwap on original element', function() {
492 this.server.respondWith('GET', '/test', 'Clicked!')
493 var div = make('<div id="d1" hx-get="/test" hx-swap="outerHTML"></div>')
494 div.addEventListener('htmx:afterSwap', function() {
495 count++
496 })
497 div.click()
498 var count = 0
499 should.equal(byId('d1'), div)
500 this.server.respond()
501 should.equal(byId('d1'), null)
502 count.should.equal(0)
503 })
504 it('swap delete works properly', function() {
505 this.server.respondWith('GET', '/test', 'Oops, deleted!')
506
507 var div = make('<div id="d1" hx-swap="delete" hx-get="/test">Foo</div>')
508 div.click()
509 this.server.respond()
510 should.equal(byId('d1'), null)
511 })
512
513 it('in presence of bad swap spec, it uses the default swap strategy', function() {
514 var initialSwapStyle = htmx.config.defaultSwapStyle
515 htmx.config.defaultSwapStyle = 'outerHTML'
516 try {
517 this.server.respondWith('GET', '/test', 'Clicked!')
518
519 var div = make('<div><button id="b1" hx-swap="foo" hx-get="/test">Initial</button></div>')
520 var b1 = byId('b1')
521 b1.click()
522 this.server.respond()
523 div.innerHTML.should.equal('Clicked!')
524 } finally {
525 htmx.config.defaultSwapStyle = initialSwapStyle
526 }
527 })
528
529 it('hx-swap ignoreTitle works', function() {
530 window.document.title = 'Test Title'
531 this.server.respondWith('GET', '/test', function(xhr) {
532 xhr.respond(200, {}, "<title class=''>htmx rocks!</title>Clicked!")
533 })
534 var btn = make('<button hx-get="/test" hx-swap="innerHTML ignoreTitle:true">Click Me!</button>')
535 btn.click()
536 this.server.respond()
537 btn.innerText.should.equal('Clicked!')
538 window.document.title.should.equal('Test Title')
539 })
540
541 it('swapError fires if swap throws exception', function() {
542 try {
543 // override makeSettleInfo to cause swap function to throw exception
544 htmx._('htmx.backupMakeSettleInfo = makeSettleInfo')
545 htmx._('makeSettleInfo = function() { throw new Error("throw") }')
546 var error = false
547 var handler = htmx.on('htmx:swapError', function(evt) {
548 error = true
549 })
550
551 this.server.respondWith('GET', '/test', 'Clicked!')
552 var div = make("<div hx-get='/test'></div>")
553 div.click()
554 this.server.respond()
555 } catch (e) {
556 } finally {
557 div.innerHTML.should.equal('')
558 error.should.equal(true)
559 htmx.off('htmx:swapError', handler)
560 htmx._('makeSettleInfo = htmx.backupMakeSettleInfo')
561 }
562 })
563 })