| 1 |
describe('Core htmx API test', function() { |
| 2 |
beforeEach(function() { |
| 3 |
this.server = makeServer() |
| 4 |
clearWorkArea() |
| 5 |
}) |
| 6 |
afterEach(function() { |
| 7 |
this.server.restore() |
| 8 |
clearWorkArea() |
| 9 |
}) |
| 10 |
|
| 11 |
it('should find properly', function() { |
| 12 |
var div = make("<div id='d1' class='c1 c2'>") |
| 13 |
div.should.equal(htmx.find('#d1')) |
| 14 |
div.should.equal(htmx.find('.c1')) |
| 15 |
div.should.equal(htmx.find('.c2')) |
| 16 |
div.should.equal(htmx.find('.c1.c2')) |
| 17 |
}) |
| 18 |
|
| 19 |
it('should find properly from elt', function() { |
| 20 |
var div = make("<div><a id='a1'></a><a id='a2'></a></div>") |
| 21 |
htmx.find(div, 'a').id.should.equal('a1') |
| 22 |
}) |
| 23 |
|
| 24 |
it('should find all properly', function() { |
| 25 |
var div = make("<div class='c1 c2 c3'><div class='c1 c2'><div class='c1'>") |
| 26 |
htmx.findAll('.c1').length.should.equal(3) |
| 27 |
htmx.findAll('.c2').length.should.equal(2) |
| 28 |
htmx.findAll('.c3').length.should.equal(1) |
| 29 |
}) |
| 30 |
|
| 31 |
it('should find all properly from elt', function() { |
| 32 |
var div = make("<div><div class='c1 c2 c3'><div class='c1 c2'><div class='c1'></div>") |
| 33 |
htmx.findAll(div, '.c1').length.should.equal(3) |
| 34 |
htmx.findAll(div, '.c2').length.should.equal(2) |
| 35 |
htmx.findAll(div, '.c3').length.should.equal(1) |
| 36 |
}) |
| 37 |
|
| 38 |
it('should find closest element properly', function() { |
| 39 |
var div = make("<div><a id='a1'></a><a id='a2'></a></div>") |
| 40 |
var a = htmx.find(div, 'a') |
| 41 |
htmx.closest(a, 'div').should.equal(div) |
| 42 |
}) |
| 43 |
|
| 44 |
it('should remove element properly', function() { |
| 45 |
var div = make('<div><a></a></div>') |
| 46 |
var a = htmx.find(div, 'a') |
| 47 |
htmx.remove(a) |
| 48 |
div.innerHTML.should.equal('') |
| 49 |
}) |
| 50 |
|
| 51 |
it('remove element with delay properly', function(done) { |
| 52 |
var div = make('<div><a></a></div>') |
| 53 |
var a = htmx.find(div, 'a') |
| 54 |
htmx.remove(a, 10) |
| 55 |
div.innerHTML.should.not.equal('') |
| 56 |
setTimeout(function() { |
| 57 |
div.innerHTML.should.equal('') |
| 58 |
done() |
| 59 |
}, 30) |
| 60 |
}) |
| 61 |
|
| 62 |
it('should remove element properly w/ selector', function() { |
| 63 |
var div = make("<div><a id='a1'></a></div>") |
| 64 |
var a = htmx.find(div, 'a') |
| 65 |
htmx.remove('#a1') |
| 66 |
div.innerHTML.should.equal('') |
| 67 |
}) |
| 68 |
|
| 69 |
it('should add class properly', function() { |
| 70 |
var div = make('<div></div>') |
| 71 |
div.classList.contains('foo').should.equal(false) |
| 72 |
htmx.addClass(div, 'foo') |
| 73 |
div.classList.contains('foo').should.equal(true) |
| 74 |
}) |
| 75 |
|
| 76 |
it('should add class properly w/ selector', function() { |
| 77 |
var div = make("<div id='div1'></div>") |
| 78 |
div.classList.contains('foo').should.equal(false) |
| 79 |
htmx.addClass('#div1', 'foo') |
| 80 |
div.classList.contains('foo').should.equal(true) |
| 81 |
}) |
| 82 |
|
| 83 |
it('should add class properly after delay', function(done) { |
| 84 |
var div = make('<div></div>') |
| 85 |
div.classList.contains('foo').should.equal(false) |
| 86 |
htmx.addClass(div, 'foo', 10) |
| 87 |
div.classList.contains('foo').should.equal(false) |
| 88 |
setTimeout(function() { |
| 89 |
div.classList.contains('foo').should.equal(true) |
| 90 |
done() |
| 91 |
}, 20) |
| 92 |
}) |
| 93 |
|
| 94 |
it('should remove class properly', function() { |
| 95 |
var div = make('<div></div>') |
| 96 |
htmx.addClass(div, 'foo') |
| 97 |
div.classList.contains('foo').should.equal(true) |
| 98 |
htmx.removeClass(div, 'foo') |
| 99 |
div.classList.contains('foo').should.equal(false) |
| 100 |
}) |
| 101 |
|
| 102 |
it('should not error if you remove class from invalid element', function() { |
| 103 |
htmx.removeClass(null, 'foo') |
| 104 |
}) |
| 105 |
|
| 106 |
it('should remove class properly w/ selector', function() { |
| 107 |
var div = make("<div id='div1'></div>") |
| 108 |
htmx.addClass(div, 'foo') |
| 109 |
div.classList.contains('foo').should.equal(true) |
| 110 |
htmx.removeClass('#div1', 'foo') |
| 111 |
div.classList.contains('foo').should.equal(false) |
| 112 |
}) |
| 113 |
|
| 114 |
it('should remove class properly after delay', function(done) { |
| 115 |
var div = make('<div></div>') |
| 116 |
htmx.addClass(div, 'foo') |
| 117 |
div.classList.contains('foo').should.equal(true) |
| 118 |
htmx.removeClass(div, 'foo', 10) |
| 119 |
div.classList.contains('foo').should.equal(true) |
| 120 |
setTimeout(function() { |
| 121 |
div.classList.contains('foo').should.equal(false) |
| 122 |
done() |
| 123 |
}, 20) |
| 124 |
}) |
| 125 |
|
| 126 |
it('should toggle class properly', function() { |
| 127 |
var div = make('<div></div>') |
| 128 |
div.classList.contains('foo').should.equal(false) |
| 129 |
htmx.toggleClass(div, 'foo') |
| 130 |
div.classList.contains('foo').should.equal(true) |
| 131 |
htmx.toggleClass(div, 'foo') |
| 132 |
div.classList.contains('foo').should.equal(false) |
| 133 |
}) |
| 134 |
|
| 135 |
it('should toggle class properly w/ selector', function() { |
| 136 |
var div = make("<div id='div1'></div>") |
| 137 |
div.classList.contains('foo').should.equal(false) |
| 138 |
htmx.toggleClass('#div1', 'foo') |
| 139 |
div.classList.contains('foo').should.equal(true) |
| 140 |
htmx.toggleClass('#div1', 'foo') |
| 141 |
div.classList.contains('foo').should.equal(false) |
| 142 |
}) |
| 143 |
|
| 144 |
it('should take class properly', function() { |
| 145 |
var div1 = make('<div></div>') |
| 146 |
var div2 = make('<div></div>') |
| 147 |
var div3 = make('<div></div>') |
| 148 |
|
| 149 |
div1.classList.contains('foo').should.equal(false) |
| 150 |
div2.classList.contains('foo').should.equal(false) |
| 151 |
div3.classList.contains('foo').should.equal(false) |
| 152 |
|
| 153 |
htmx.takeClass(div1, 'foo') |
| 154 |
|
| 155 |
div1.classList.contains('foo').should.equal(true) |
| 156 |
div2.classList.contains('foo').should.equal(false) |
| 157 |
div3.classList.contains('foo').should.equal(false) |
| 158 |
|
| 159 |
htmx.takeClass(div2, 'foo') |
| 160 |
|
| 161 |
div1.classList.contains('foo').should.equal(false) |
| 162 |
div2.classList.contains('foo').should.equal(true) |
| 163 |
div3.classList.contains('foo').should.equal(false) |
| 164 |
|
| 165 |
htmx.takeClass(div3, 'foo') |
| 166 |
|
| 167 |
div1.classList.contains('foo').should.equal(false) |
| 168 |
div2.classList.contains('foo').should.equal(false) |
| 169 |
div3.classList.contains('foo').should.equal(true) |
| 170 |
}) |
| 171 |
|
| 172 |
it('should take class properly w/ selector', function() { |
| 173 |
var div1 = make("<div id='div1'></div>") |
| 174 |
var div2 = make("<div id='div2'></div>") |
| 175 |
var div3 = make("<div id='div3'></div>") |
| 176 |
|
| 177 |
div1.classList.contains('foo').should.equal(false) |
| 178 |
div2.classList.contains('foo').should.equal(false) |
| 179 |
div3.classList.contains('foo').should.equal(false) |
| 180 |
|
| 181 |
htmx.takeClass('#div1', 'foo') |
| 182 |
|
| 183 |
div1.classList.contains('foo').should.equal(true) |
| 184 |
div2.classList.contains('foo').should.equal(false) |
| 185 |
div3.classList.contains('foo').should.equal(false) |
| 186 |
|
| 187 |
htmx.takeClass('#div2', 'foo') |
| 188 |
|
| 189 |
div1.classList.contains('foo').should.equal(false) |
| 190 |
div2.classList.contains('foo').should.equal(true) |
| 191 |
div3.classList.contains('foo').should.equal(false) |
| 192 |
|
| 193 |
htmx.takeClass('#div3', 'foo') |
| 194 |
|
| 195 |
div1.classList.contains('foo').should.equal(false) |
| 196 |
div2.classList.contains('foo').should.equal(false) |
| 197 |
div3.classList.contains('foo').should.equal(true) |
| 198 |
}) |
| 199 |
|
| 200 |
it('eval can be suppressed', function() { |
| 201 |
var calledEvent = false |
| 202 |
var handler = htmx.on('htmx:evalDisallowedError', function() { |
| 203 |
calledEvent = true |
| 204 |
}) |
| 205 |
try { |
| 206 |
htmx.config.allowEval = false |
| 207 |
should.equal(htmx._('tokenizeString'), undefined) |
| 208 |
} finally { |
| 209 |
htmx.config.allowEval = true |
| 210 |
htmx.off('htmx:evalDisallowedError', handler) |
| 211 |
} |
| 212 |
calledEvent.should.equal(true) |
| 213 |
}) |
| 214 |
|
| 215 |
it('ajax api works', function() { |
| 216 |
this.server.respondWith('GET', '/test', 'foo!') |
| 217 |
var div = make('<div></div>') |
| 218 |
htmx.ajax('GET', '/test', div) |
| 219 |
this.server.respond() |
| 220 |
div.innerHTML.should.equal('foo!') |
| 221 |
}) |
| 222 |
|
| 223 |
it('ajax api works by ID', function() { |
| 224 |
this.server.respondWith('GET', '/test', 'foo!') |
| 225 |
var div = make("<div id='d1'></div>") |
| 226 |
htmx.ajax('GET', '/test', '#d1') |
| 227 |
this.server.respond() |
| 228 |
div.innerHTML.should.equal('foo!') |
| 229 |
}) |
| 230 |
|
| 231 |
it('ajax api does not fall back to body when target invalid', function() { |
| 232 |
this.server.respondWith('GET', '/test', 'foo!') |
| 233 |
var div = make("<div id='d1'></div>") |
| 234 |
htmx.ajax('GET', '/test', '#d2') |
| 235 |
this.server.respond() |
| 236 |
document.body.innerHTML.should.not.equal('foo!') |
| 237 |
}) |
| 238 |
|
| 239 |
it('ajax api fails when target invalid', function(done) { |
| 240 |
this.server.respondWith('GET', '/test', 'foo!') |
| 241 |
var div = make("<div id='d1'></div>") |
| 242 |
htmx.ajax('GET', '/test', '#d2').then( |
| 243 |
(value) => { |
| 244 |
}, |
| 245 |
(reason) => { |
| 246 |
done() |
| 247 |
} |
| 248 |
) |
| 249 |
this.server.respond() |
| 250 |
div.innerHTML.should.equal('') |
| 251 |
}) |
| 252 |
|
| 253 |
it('ajax api fails when target invalid even if source set', function(done) { |
| 254 |
this.server.respondWith('GET', '/test', 'foo!') |
| 255 |
var div = make("<div id='d1'></div>") |
| 256 |
htmx.ajax('GET', '/test', { |
| 257 |
source: div, |
| 258 |
target: '#d2' |
| 259 |
}).then( |
| 260 |
(value) => { |
| 261 |
}, |
| 262 |
(reason) => { |
| 263 |
done() |
| 264 |
} |
| 265 |
) |
| 266 |
this.server.respond() |
| 267 |
div.innerHTML.should.equal('') |
| 268 |
}) |
| 269 |
|
| 270 |
it('ajax api fails when source invalid and no target set', function(done) { |
| 271 |
this.server.respondWith('GET', '/test', 'foo!') |
| 272 |
var div = make("<div id='d1'></div>") |
| 273 |
htmx.ajax('GET', '/test', { |
| 274 |
source: '#d2' |
| 275 |
}).then( |
| 276 |
(value) => { |
| 277 |
}, |
| 278 |
(reason) => { |
| 279 |
done() |
| 280 |
} |
| 281 |
) |
| 282 |
this.server.respond() |
| 283 |
div.innerHTML.should.equal('') |
| 284 |
}) |
| 285 |
|
| 286 |
it('ajax api falls back to targeting source if target not set', function() { |
| 287 |
this.server.respondWith('GET', '/test', 'foo!') |
| 288 |
var div = make("<div id='d1'></div>") |
| 289 |
htmx.ajax('GET', '/test', { |
| 290 |
source: div |
| 291 |
}) |
| 292 |
this.server.respond() |
| 293 |
div.innerHTML.should.equal('foo!') |
| 294 |
}) |
| 295 |
|
| 296 |
it('ajax api falls back to targeting body if target and source not set', function() { |
| 297 |
var target |
| 298 |
this.server.respondWith('GET', '/test', 'foo!') |
| 299 |
htmx.on(document.body, 'htmx:configRequest', function(evt) { |
| 300 |
target = evt.detail.target |
| 301 |
return false |
| 302 |
}) |
| 303 |
htmx.ajax('GET', '/test', { swap: 'none' }) |
| 304 |
this.server.respond() |
| 305 |
target.should.equal(document.body) |
| 306 |
}) |
| 307 |
|
| 308 |
it('ajax api works with swapSpec', function() { |
| 309 |
this.server.respondWith('GET', '/test', "<p class='test'>foo!</p>") |
| 310 |
var div = make("<div><div id='target'></div></div>") |
| 311 |
htmx.ajax('GET', '/test', { target: '#target', swap: 'outerHTML' }) |
| 312 |
this.server.respond() |
| 313 |
div.innerHTML.should.equal('<p class="test">foo!</p>') |
| 314 |
}) |
| 315 |
|
| 316 |
it('ajax api works with select', function() { |
| 317 |
this.server.respondWith('GET', '/test', "<div id='d1'>foo</div><div id='d2'>bar</div>") |
| 318 |
var div = make("<div id='target'></div>") |
| 319 |
htmx.ajax('GET', '/test', { target: '#target', select: '#d2' }) |
| 320 |
this.server.respond() |
| 321 |
div.innerHTML.should.equal('<div id="d2">bar</div>') |
| 322 |
}) |
| 323 |
|
| 324 |
it('ajax api works with selectOOB', function() { |
| 325 |
this.server.respondWith('GET', '/test', "<div id='oob'>OOB Content</div><div>Main Content</div>") |
| 326 |
var target = make("<div id='target'></div>") |
| 327 |
var oobDiv = make("<div id='oob'></div>") |
| 328 |
htmx.ajax('GET', '/test', { target: '#target', selectOOB: '#oob:innerHTML' }) |
| 329 |
this.server.respond() |
| 330 |
target.innerHTML.should.equal('<div>Main Content</div>') |
| 331 |
oobDiv.innerHTML.should.equal('OOB Content') |
| 332 |
}) |
| 333 |
|
| 334 |
it('ajax api works with Hx-Select overrides select', function() { |
| 335 |
this.server.respondWith('GET', '/test', [200, { 'HX-Reselect': '#d2' }, "<div id='d1'>foo</div><div id='d2'>bar</div>"]) |
| 336 |
var div = make("<div id='target'></div>") |
| 337 |
htmx.ajax('GET', '/test', { target: '#target', select: '#d1' }) |
| 338 |
this.server.respond() |
| 339 |
div.innerHTML.should.equal('<div id="d2">bar</div>') |
| 340 |
}) |
| 341 |
|
| 342 |
it('ajax returns a promise', function(done) { |
| 343 |
|
| 344 |
if (typeof Promise !== 'undefined') { |
| 345 |
this.server.respondWith('GET', '/test', 'foo!') |
| 346 |
var div = make("<div id='d1'></div>") |
| 347 |
var promise = htmx.ajax('GET', '/test', '#d1') |
| 348 |
this.server.respond() |
| 349 |
div.innerHTML.should.equal('foo!') |
| 350 |
promise.then(function() { |
| 351 |
done() |
| 352 |
}) |
| 353 |
} else { |
| 354 |
done() |
| 355 |
} |
| 356 |
}) |
| 357 |
|
| 358 |
it('ajax api can pass parameters', function() { |
| 359 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 360 |
var params = getParameters(xhr) |
| 361 |
params.i1.should.equal('test') |
| 362 |
xhr.respond(200, {}, 'Clicked!') |
| 363 |
}) |
| 364 |
var div = make("<div id='d1'></div>") |
| 365 |
htmx.ajax('POST', '/test', { target: '#d1', values: { i1: 'test' } }) |
| 366 |
this.server.respond() |
| 367 |
div.innerHTML.should.equal('Clicked!') |
| 368 |
}) |
| 369 |
|
| 370 |
it('ajax api Content-Type header is application/x-www-form-urlencoded', function() { |
| 371 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 372 |
var params = getParameters(xhr) |
| 373 |
xhr.requestHeaders['Content-Type'].should.equal('application/x-www-form-urlencoded;charset=utf-8') |
| 374 |
params.i1.should.equal('test') |
| 375 |
xhr.respond(200, {}, 'Clicked!') |
| 376 |
}) |
| 377 |
var div = make("<div id='d1'></div>") |
| 378 |
htmx.ajax('POST', '/test', { target: '#d1', values: { i1: 'test' } }) |
| 379 |
this.server.respond() |
| 380 |
div.innerHTML.should.equal('Clicked!') |
| 381 |
}) |
| 382 |
|
| 383 |
it('ajax api Content-Type header override to application/json', function() { |
| 384 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 385 |
var params = getParameters(xhr) |
| 386 |
xhr.requestHeaders['Content-Type'].should.equal('application/json;charset=utf-8') |
| 387 |
params.i1.should.equal('test') |
| 388 |
xhr.respond(200, {}, 'Clicked!') |
| 389 |
}) |
| 390 |
|
| 391 |
var div = make("<div id='d1'></div>") |
| 392 |
htmx.ajax('POST', '/test', { |
| 393 |
target: '#d1', |
| 394 |
swap: 'innerHTML', |
| 395 |
headers: { |
| 396 |
'Content-Type': 'application/json' |
| 397 |
}, |
| 398 |
values: { i1: 'test' } |
| 399 |
}) |
| 400 |
|
| 401 |
this.server.respond() |
| 402 |
div.innerHTML.should.equal('Clicked!') |
| 403 |
}) |
| 404 |
|
| 405 |
it('can re-init with new attributes', function() { |
| 406 |
this.server.respondWith('PATCH', '/test', 'patch') |
| 407 |
this.server.respondWith('DELETE', '/test', 'delete') |
| 408 |
|
| 409 |
var div = make('<div hx-patch="/test">click me</div>') |
| 410 |
div.click() |
| 411 |
this.server.respond() |
| 412 |
div.innerHTML.should.equal('patch') |
| 413 |
|
| 414 |
div.removeAttribute('hx-patch') |
| 415 |
div.setAttribute('hx-delete', '/test') |
| 416 |
htmx.process(div) |
| 417 |
|
| 418 |
div.click() |
| 419 |
this.server.respond() |
| 420 |
div.innerHTML.should.equal('delete') |
| 421 |
}) |
| 422 |
|
| 423 |
it('does not trigger load on re-init of an existing element', function() { |
| 424 |
this.server.respondWith('GET', '/test', 'test') |
| 425 |
var div = make('<div hx-get="/test" hx-trigger="load" hx-swap="beforeend"></div>') |
| 426 |
this.server.respond() |
| 427 |
div.innerHTML.should.equal('test') |
| 428 |
div.setAttribute('hx-swap', 'afterbegin') |
| 429 |
htmx.process(div) |
| 430 |
this.server.respond() |
| 431 |
div.innerHTML.should.equal('test') |
| 432 |
}) |
| 433 |
|
| 434 |
it('onLoad is called... onLoad', function() { |
| 435 |
|
| 436 |
this.server.respondWith('GET', '/test', "<div id='d1' hx-get='/test'></div>") |
| 437 |
var helper = htmx.onLoad(function(elt) { |
| 438 |
elt.setAttribute('foo', 'bar') |
| 439 |
}) |
| 440 |
|
| 441 |
try { |
| 442 |
var div = make("<div id='d1' hx-get='/test' hx-swap='outerHTML'></div>") |
| 443 |
div.click() |
| 444 |
this.server.respond() |
| 445 |
byId('d1').getAttribute('foo').should.equal('bar') |
| 446 |
htmx.off('htmx:load', helper) |
| 447 |
} catch (error) { |
| 448 |
|
| 449 |
htmx.off('htmx:load', helper) |
| 450 |
throw error |
| 451 |
} |
| 452 |
}) |
| 453 |
|
| 454 |
it('triggers properly', function() { |
| 455 |
var div = make('<div/>') |
| 456 |
var myEventCalled = false |
| 457 |
var detailStr = '' |
| 458 |
htmx.on('myEvent', function(evt) { |
| 459 |
myEventCalled = true |
| 460 |
detailStr = evt.detail.str |
| 461 |
}) |
| 462 |
htmx.trigger(div, 'myEvent', { str: 'foo' }) |
| 463 |
|
| 464 |
myEventCalled.should.equal(true) |
| 465 |
detailStr.should.equal('foo') |
| 466 |
}) |
| 467 |
|
| 468 |
it('triggers properly w/ selector', function() { |
| 469 |
var div = make("<div id='div1'/>") |
| 470 |
var myEventCalled = false |
| 471 |
var detailStr = '' |
| 472 |
htmx.on('myEvent', function(evt) { |
| 473 |
myEventCalled = true |
| 474 |
detailStr = evt.detail.str |
| 475 |
}) |
| 476 |
htmx.trigger('#div1', 'myEvent', { str: 'foo' }) |
| 477 |
|
| 478 |
myEventCalled.should.equal(true) |
| 479 |
detailStr.should.equal('foo') |
| 480 |
}) |
| 481 |
|
| 482 |
it('triggers with no details properly', function() { |
| 483 |
var div = make('<div/>') |
| 484 |
var myEventCalled = false |
| 485 |
htmx.on('myEvent', function(evt) { |
| 486 |
myEventCalled = true |
| 487 |
}) |
| 488 |
htmx.trigger(div, 'myEvent') |
| 489 |
myEventCalled.should.equal(true) |
| 490 |
}) |
| 491 |
|
| 492 |
it('swaps content properly (basic)', function() { |
| 493 |
var output = make('<output id="output"/>') |
| 494 |
htmx.swap('#output', '<div>Swapped!</div>', { swapStyle: 'innerHTML' }) |
| 495 |
output.innerHTML.should.be.equal('<div>Swapped!</div>') |
| 496 |
}) |
| 497 |
|
| 498 |
it('swap works with a swap delay', function(done) { |
| 499 |
var div = make("<div hx-get='/test'></div>") |
| 500 |
div.innerText.should.equal('') |
| 501 |
htmx.swap(div, 'jsswapped', { swapDelay: 10 }) |
| 502 |
div.innerText.should.equal('') |
| 503 |
setTimeout(function() { |
| 504 |
div.innerText.should.equal('jsswapped') |
| 505 |
done() |
| 506 |
}, 30) |
| 507 |
}) |
| 508 |
|
| 509 |
if (/chrome/i.test(navigator.userAgent)) { |
| 510 |
it('swap works with a view transition', function(done) { |
| 511 |
var div = make("<div hx-get='/test'></div>") |
| 512 |
div.innerText.should.equal('') |
| 513 |
htmx.swap(div, 'jsswapped', { transition: true }) |
| 514 |
div.innerText.should.equal('') |
| 515 |
setTimeout(function() { |
| 516 |
div.innerText.should.equal('jsswapped') |
| 517 |
done() |
| 518 |
}, 50) |
| 519 |
}) |
| 520 |
} |
| 521 |
it('swaps content properly (with select)', function() { |
| 522 |
var output = make('<output id="output"/>') |
| 523 |
htmx.swap('#output', '<div><p id="select-me">Swapped!</p></div>', { swapStyle: 'innerHTML' }, { select: '#select-me' }) |
| 524 |
output.innerHTML.should.be.equal('<p id="select-me">Swapped!</p>') |
| 525 |
}) |
| 526 |
|
| 527 |
it('swaps content properly (with oob)', function() { |
| 528 |
var output = make('<output id="output"/>') |
| 529 |
var oobDiv = make('<div id="oob"/>') |
| 530 |
htmx.swap('#output', '<div id="oob" hx-swap-oob="innerHTML">OOB Swapped!</div><div>Swapped!</div>', { swapStyle: 'innerHTML' }) |
| 531 |
output.innerHTML.should.be.equal('<div>Swapped!</div>') |
| 532 |
oobDiv.innerHTML.should.be.equal('OOB Swapped!') |
| 533 |
}) |
| 534 |
|
| 535 |
it('swaps content properly (with select oob)', function() { |
| 536 |
var output = make('<output id="output"/>') |
| 537 |
var oobDiv = make('<div id="oob"/>') |
| 538 |
htmx.swap('#output', '<div id="oob">OOB Swapped!</div><div>Swapped!</div>', { swapStyle: 'innerHTML' }, { selectOOB: '#oob:innerHTML' }) |
| 539 |
output.innerHTML.should.be.equal('<div>Swapped!</div>') |
| 540 |
oobDiv.innerHTML.should.be.equal('OOB Swapped!') |
| 541 |
}) |
| 542 |
|
| 543 |
it('swap delete works when parent is removed', function() { |
| 544 |
this.server.respondWith('DELETE', '/test', 'delete') |
| 545 |
|
| 546 |
var parent = make('<div><div id="d1" hx-swap="delete" hx-delete="/test">click me</div></div>') |
| 547 |
var div = htmx.find(parent, '#d1') |
| 548 |
div.click() |
| 549 |
div.remove() |
| 550 |
parent.remove() |
| 551 |
this.server.respond() |
| 552 |
parent.children.length.should.equal(0) |
| 553 |
}) |
| 554 |
|
| 555 |
it('swap outerHTML works when parent is removed', function() { |
| 556 |
this.server.respondWith('GET', '/test', 'delete') |
| 557 |
|
| 558 |
var parent = make('<div><div id="d1" hx-swap="outerHTML" hx-get="/test">click me</div></div>') |
| 559 |
var div = htmx.find(parent, '#d1') |
| 560 |
div.click() |
| 561 |
div.remove() |
| 562 |
parent.remove() |
| 563 |
this.server.respond() |
| 564 |
parent.children.length.should.equal(0) |
| 565 |
}) |
| 566 |
|
| 567 |
it('values api returns formDataProxy with correct form data even if clicked button removed', function() { |
| 568 |
make('<form id="valuesform" hx-post="/test">' + |
| 569 |
'<input type="text" name="t1" value="textValue">' + |
| 570 |
'<button id="submit" type="submit" name="b1" value="buttonValue">button</button>' + |
| 571 |
'</form>') |
| 572 |
|
| 573 |
var apiValues = htmx.values(byId('valuesform'), 'post') |
| 574 |
apiValues.get('t1').should.equal('textValue') |
| 575 |
should.equal(apiValues.get('b1'), null) |
| 576 |
byId('submit').click() |
| 577 |
apiValues = htmx.values(byId('valuesform'), 'post') |
| 578 |
apiValues.get('t1').should.equal('textValue') |
| 579 |
apiValues.get('b1').should.equal('buttonValue') |
| 580 |
byId('submit').remove() |
| 581 |
apiValues = htmx.values(byId('valuesform'), 'post') |
| 582 |
JSON.stringify(apiValues).should.equal('{"t1":"textValue"}') |
| 583 |
var assign = Object.assign({}, apiValues) |
| 584 |
JSON.stringify(assign).should.equal('{"t1":"textValue"}') |
| 585 |
}) |
| 586 |
|
| 587 |
it('tests for formDataProxy array updating and testing for loc coverage', function() { |
| 588 |
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>') |
| 589 |
var vals = htmx.values(form, 'post') |
| 590 |
vals.foo.should.equal('bar') |
| 591 |
vals.do.should.deep.equal(['rey', 'rey']) |
| 592 |
should.equal(vals.do.toString(), undefined) |
| 593 |
vals.do.push('test') |
| 594 |
vals.do.should.deep.equal(['rey', 'rey', 'test']) |
| 595 |
vals.do = ['bob', 'jim'] |
| 596 |
vals.do.should.deep.equal(['bob', 'jim']) |
| 597 |
vals.do[0] = 'hi' |
| 598 |
vals.do.should.deep.equal(['hi', 'jim']) |
| 599 |
var arr = vals.do |
| 600 |
arr[0] = ['override'] |
| 601 |
arr[0].should.equal('override') |
| 602 |
vals.do.should.deep.equal(['override', 'jim']) |
| 603 |
vals[Symbol.toStringTag].should.equal('FormData') |
| 604 |
try { |
| 605 |
vals[Symbol.toStringTag] = 'notFormData' |
| 606 |
} catch (e) {} |
| 607 |
vals[Symbol.toStringTag].should.equal('FormData') |
| 608 |
}) |
| 609 |
|
| 610 |
it('logAll() and logNone() run without error', function() { |
| 611 |
make('<div id="d1"></div>') |
| 612 |
htmx.logAll() |
| 613 |
htmx.trigger(byId('d1'), 'test-event') |
| 614 |
htmx.logNone() |
| 615 |
}) |
| 616 |
|
| 617 |
it('querySelectorExt internal extension api works with just string', function() { |
| 618 |
make('<div id="d1">content</div>') |
| 619 |
var div = htmx._('querySelectorExt("#d1")') |
| 620 |
div.innerHTML.should.equal('content') |
| 621 |
}) |
| 622 |
|
| 623 |
it('ajax api with no context works', function() { |
| 624 |
|
| 625 |
var status |
| 626 |
var handler = htmx.on('htmx:beforeSwap', function(event) { |
| 627 |
status = event.detail.xhr.status |
| 628 |
}) |
| 629 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 630 |
xhr.respond(204, {}, 'foo!') |
| 631 |
}) |
| 632 |
var div = make('<div></div>') |
| 633 |
htmx.ajax('GET', '/test') |
| 634 |
this.server.respond() |
| 635 |
div.innerHTML.should.equal('') |
| 636 |
status.should.equal(204) |
| 637 |
htmx.off('htmx:beforeSwap', handler) |
| 638 |
}) |
| 639 |
|
| 640 |
it('ajax api with can pass in custom handler and handle if it throws error', function() { |
| 641 |
var onLoadError = false |
| 642 |
var handler = htmx.on('htmx:onLoadError', function(event) { |
| 643 |
onLoadError = true |
| 644 |
}) |
| 645 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 646 |
xhr.respond(204, {}, 'foo!') |
| 647 |
}) |
| 648 |
var div = make('<div></div>') |
| 649 |
try { |
| 650 |
htmx.ajax('GET', '/test', { handler: function() { throw new Error('throw') } }) |
| 651 |
this.server.respond() |
| 652 |
} catch (e) {} |
| 653 |
div.innerHTML.should.equal('') |
| 654 |
onLoadError.should.equal(true) |
| 655 |
|
| 656 |
|
| 657 |
onLoadError = false |
| 658 |
try { |
| 659 |
htmx.ajax('GET', '/test', { handler: function() { throw new Error('throw') } }) |
| 660 |
this.server.respond() |
| 661 |
} catch (e) {} |
| 662 |
div.innerHTML.should.equal('') |
| 663 |
onLoadError.should.equal(true) |
| 664 |
htmx.off('htmx:onLoadError', handler) |
| 665 |
}) |
| 666 |
|
| 667 |
it('process api can process non elements fine', function() { |
| 668 |
var div = make('<div>textNode</div>') |
| 669 |
htmx.process(div.firstChild) |
| 670 |
}) |
| 671 |
|
| 672 |
it('ajax api push Url should push an element into the cache when true', function() { |
| 673 |
this.server.respondWith('POST', '/test123', 'Clicked!') |
| 674 |
|
| 675 |
var div = make("<div id='d1'></div>") |
| 676 |
htmx.ajax('POST', '/test123', { |
| 677 |
target: '#d1', |
| 678 |
swap: 'innerHTML', |
| 679 |
push: 'true' |
| 680 |
}) |
| 681 |
this.server.respond() |
| 682 |
div.innerHTML.should.equal('Clicked!') |
| 683 |
var path = sessionStorage.getItem('htmx-current-path-for-history') |
| 684 |
path.should.equal('/test123') |
| 685 |
}) |
| 686 |
|
| 687 |
it('ajax api push Url should push an element into the cache when string', function() { |
| 688 |
this.server.respondWith('POST', '/test', 'Clicked!') |
| 689 |
|
| 690 |
var div = make("<div id='d1'></div>") |
| 691 |
htmx.ajax('POST', '/test', { |
| 692 |
target: '#d1', |
| 693 |
swap: 'innerHTML', |
| 694 |
push: '/abc123' |
| 695 |
}) |
| 696 |
this.server.respond() |
| 697 |
div.innerHTML.should.equal('Clicked!') |
| 698 |
var path = sessionStorage.getItem('htmx-current-path-for-history') |
| 699 |
path.should.equal('/abc123') |
| 700 |
}) |
| 701 |
|
| 702 |
it('ajax api replace Url should replace an element into the cache when true', function() { |
| 703 |
this.server.respondWith('POST', '/test123', 'Clicked!') |
| 704 |
|
| 705 |
var div = make("<div id='d1'></div>") |
| 706 |
htmx.ajax('POST', '/test123', { |
| 707 |
target: '#d1', |
| 708 |
swap: 'innerHTML', |
| 709 |
replace: 'true' |
| 710 |
}) |
| 711 |
this.server.respond() |
| 712 |
div.innerHTML.should.equal('Clicked!') |
| 713 |
var path = sessionStorage.getItem('htmx-current-path-for-history') |
| 714 |
path.should.equal('/test123') |
| 715 |
}) |
| 716 |
|
| 717 |
it('ajax api replace Url should replace an element into the cache when string', function() { |
| 718 |
this.server.respondWith('POST', '/test', 'Clicked!') |
| 719 |
|
| 720 |
var div = make("<div id='d1'></div>") |
| 721 |
htmx.ajax('POST', '/test', { |
| 722 |
target: '#d1', |
| 723 |
swap: 'innerHTML', |
| 724 |
replace: '/abc123' |
| 725 |
}) |
| 726 |
this.server.respond() |
| 727 |
div.innerHTML.should.equal('Clicked!') |
| 728 |
var path = sessionStorage.getItem('htmx-current-path-for-history') |
| 729 |
path.should.equal('/abc123') |
| 730 |
}) |
| 731 |
}) |