| 1 |
describe('Core htmx AJAX Tests', function() { |
| 2 |
beforeEach(function() { |
| 3 |
this.server = makeServer() |
| 4 |
clearWorkArea() |
| 5 |
}) |
| 6 |
afterEach(function() { |
| 7 |
this.server.restore() |
| 8 |
clearWorkArea() |
| 9 |
}) |
| 10 |
|
| 11 |
|
| 12 |
it('issues a GET request on click and swaps content', function() { |
| 13 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 14 |
|
| 15 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 16 |
btn.click() |
| 17 |
this.server.respond() |
| 18 |
btn.innerHTML.should.equal('Clicked!') |
| 19 |
}) |
| 20 |
|
| 21 |
it('processes inner content properly', function() { |
| 22 |
this.server.respondWith('GET', '/test', '<a hx-get="/test2">Click Me</a>') |
| 23 |
this.server.respondWith('GET', '/test2', 'Clicked!') |
| 24 |
|
| 25 |
var div = make('<div hx-get="/test"></div>') |
| 26 |
div.click() |
| 27 |
this.server.respond() |
| 28 |
div.innerHTML.should.equal('<a hx-get="/test2">Click Me</a>') |
| 29 |
var a = div.querySelector('a') |
| 30 |
a.click() |
| 31 |
this.server.respond() |
| 32 |
a.innerHTML.should.equal('Clicked!') |
| 33 |
}) |
| 34 |
|
| 35 |
it('handles swap outerHTML properly', function() { |
| 36 |
this.server.respondWith('GET', '/test', '<a id="a1" hx-get="/test2">Click Me</a>') |
| 37 |
this.server.respondWith('GET', '/test2', 'Clicked!') |
| 38 |
|
| 39 |
var div = make('<div id="d1" hx-get="/test" hx-swap="outerHTML"></div>') |
| 40 |
div.click() |
| 41 |
should.equal(byId('d1'), div) |
| 42 |
this.server.respond() |
| 43 |
should.equal(byId('d1'), null) |
| 44 |
byId('a1').click() |
| 45 |
this.server.respond() |
| 46 |
byId('a1').innerHTML.should.equal('Clicked!') |
| 47 |
}) |
| 48 |
|
| 49 |
it('handles beforebegin properly', function() { |
| 50 |
var i = 0 |
| 51 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 52 |
i++ |
| 53 |
xhr.respond(200, {}, '<a id="a' + i + '" hx-get="/test2" hx-swap="innerHTML">' + i + '</a>') |
| 54 |
}) |
| 55 |
this.server.respondWith('GET', '/test2', '*') |
| 56 |
|
| 57 |
var div = make('<div hx-get="/test" hx-swap="beforebegin">*</div>') |
| 58 |
var parent = div.parentElement |
| 59 |
div.click() |
| 60 |
this.server.respond() |
| 61 |
div.innerText.should.equal('*') |
| 62 |
removeWhiteSpace(parent.innerText).should.equal('1*') |
| 63 |
|
| 64 |
byId('a1').click() |
| 65 |
this.server.respond() |
| 66 |
removeWhiteSpace(parent.innerText).should.equal('**') |
| 67 |
|
| 68 |
div.click() |
| 69 |
this.server.respond() |
| 70 |
div.innerText.should.equal('*') |
| 71 |
removeWhiteSpace(parent.innerText).should.equal('*2*') |
| 72 |
|
| 73 |
byId('a2').click() |
| 74 |
this.server.respond() |
| 75 |
removeWhiteSpace(parent.innerText).should.equal('***') |
| 76 |
}) |
| 77 |
|
| 78 |
it('handles afterbegin properly', function() { |
| 79 |
var i = 0 |
| 80 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 81 |
i++ |
| 82 |
xhr.respond(200, {}, '' + i) |
| 83 |
}) |
| 84 |
|
| 85 |
var div = make('<div hx-get="/test" hx-swap="afterbegin">*</div>') |
| 86 |
|
| 87 |
div.click() |
| 88 |
this.server.respond() |
| 89 |
div.innerText.should.equal('1*') |
| 90 |
|
| 91 |
div.click() |
| 92 |
this.server.respond() |
| 93 |
div.innerText.should.equal('21*') |
| 94 |
|
| 95 |
div.click() |
| 96 |
this.server.respond() |
| 97 |
div.innerText.should.equal('321*') |
| 98 |
}) |
| 99 |
|
| 100 |
it('handles afterbegin properly with no initial content', function() { |
| 101 |
var i = 0 |
| 102 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 103 |
i++ |
| 104 |
xhr.respond(200, {}, '' + i) |
| 105 |
}) |
| 106 |
|
| 107 |
var div = make('<div hx-get="/test" hx-swap="afterbegin"></div>') |
| 108 |
|
| 109 |
div.click() |
| 110 |
this.server.respond() |
| 111 |
div.innerText.should.equal('1') |
| 112 |
|
| 113 |
div.click() |
| 114 |
this.server.respond() |
| 115 |
div.innerText.should.equal('21') |
| 116 |
|
| 117 |
div.click() |
| 118 |
this.server.respond() |
| 119 |
div.innerText.should.equal('321') |
| 120 |
}) |
| 121 |
|
| 122 |
it('handles afterend properly', function() { |
| 123 |
var i = 0 |
| 124 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 125 |
i++ |
| 126 |
xhr.respond(200, {}, '<a id="a' + i + '" hx-get="/test2" hx-swap="innerHTML">' + i + '</a>') |
| 127 |
}) |
| 128 |
this.server.respondWith('GET', '/test2', '*') |
| 129 |
|
| 130 |
var div = make('<div hx-get="/test" hx-swap="afterend">*</div>') |
| 131 |
var parent = div.parentElement |
| 132 |
div.click() |
| 133 |
this.server.respond() |
| 134 |
div.innerText.should.equal('*') |
| 135 |
removeWhiteSpace(parent.innerText).should.equal('*1') |
| 136 |
|
| 137 |
byId('a1').click() |
| 138 |
this.server.respond() |
| 139 |
removeWhiteSpace(parent.innerText).should.equal('**') |
| 140 |
|
| 141 |
div.click() |
| 142 |
this.server.respond() |
| 143 |
div.innerText.should.equal('*') |
| 144 |
removeWhiteSpace(parent.innerText).should.equal('*2*') |
| 145 |
|
| 146 |
byId('a2').click() |
| 147 |
this.server.respond() |
| 148 |
removeWhiteSpace(parent.innerText).should.equal('***') |
| 149 |
}) |
| 150 |
|
| 151 |
it('handles beforeend properly', function() { |
| 152 |
var i = 0 |
| 153 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 154 |
i++ |
| 155 |
xhr.respond(200, {}, '' + i) |
| 156 |
}) |
| 157 |
|
| 158 |
var div = make('<div hx-get="/test" hx-swap="beforeend">*</div>') |
| 159 |
|
| 160 |
div.click() |
| 161 |
this.server.respond() |
| 162 |
div.innerText.should.equal('*1') |
| 163 |
|
| 164 |
div.click() |
| 165 |
this.server.respond() |
| 166 |
div.innerText.should.equal('*12') |
| 167 |
|
| 168 |
div.click() |
| 169 |
this.server.respond() |
| 170 |
div.innerText.should.equal('*123') |
| 171 |
}) |
| 172 |
|
| 173 |
it('handles beforeend properly with no initial content', function() { |
| 174 |
var i = 0 |
| 175 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 176 |
i++ |
| 177 |
xhr.respond(200, {}, '' + i) |
| 178 |
}) |
| 179 |
|
| 180 |
var div = make('<div hx-get="/test" hx-swap="beforeend"></div>') |
| 181 |
|
| 182 |
div.click() |
| 183 |
this.server.respond() |
| 184 |
div.innerText.should.equal('1') |
| 185 |
|
| 186 |
div.click() |
| 187 |
this.server.respond() |
| 188 |
div.innerText.should.equal('12') |
| 189 |
|
| 190 |
div.click() |
| 191 |
this.server.respond() |
| 192 |
div.innerText.should.equal('123') |
| 193 |
}) |
| 194 |
|
| 195 |
it('handles hx-target properly', function() { |
| 196 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 197 |
|
| 198 |
var btn = make('<button hx-get="/test" hx-target="#s1">Click Me!</button>') |
| 199 |
var target = make('<span id="s1">Initial</span>') |
| 200 |
btn.click() |
| 201 |
target.innerHTML.should.equal('Initial') |
| 202 |
this.server.respond() |
| 203 |
target.innerHTML.should.equal('Clicked!') |
| 204 |
}) |
| 205 |
|
| 206 |
it('handles 204 NO CONTENT responses properly', function() { |
| 207 |
this.server.respondWith('GET', '/test', [204, {}, 'No Content!']) |
| 208 |
|
| 209 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 210 |
btn.click() |
| 211 |
btn.innerHTML.should.equal('Click Me!') |
| 212 |
this.server.respond() |
| 213 |
btn.innerHTML.should.equal('Click Me!') |
| 214 |
}) |
| 215 |
|
| 216 |
it('handles 304 NOT MODIFIED responses properly', function() { |
| 217 |
this.server.respondWith('GET', '/test-1', [200, {}, 'Content for Tab 1']) |
| 218 |
this.server.respondWith('GET', '/test-2', [200, {}, 'Content for Tab 2']) |
| 219 |
|
| 220 |
var target = make('<div id="target"></div>') |
| 221 |
var btn1 = make('<button hx-get="/test-1" hx-target="#target">Tab 1</button>') |
| 222 |
var btn2 = make('<button hx-get="/test-2" hx-target="#target">Tab 2</button>') |
| 223 |
|
| 224 |
btn1.click() |
| 225 |
target.innerHTML.should.equal('') |
| 226 |
this.server.respond() |
| 227 |
target.innerHTML.should.equal('Content for Tab 1') |
| 228 |
|
| 229 |
btn2.click() |
| 230 |
this.server.respond() |
| 231 |
target.innerHTML.should.equal('Content for Tab 2') |
| 232 |
|
| 233 |
this.server.respondWith('GET', '/test-1', [304, {}, 'Content for Tab 1']) |
| 234 |
this.server.respondWith('GET', '/test-2', [304, {}, 'Content for Tab 2']) |
| 235 |
|
| 236 |
btn1.click() |
| 237 |
this.server.respond() |
| 238 |
target.innerHTML.should.equal('Content for Tab 1') |
| 239 |
|
| 240 |
btn2.click() |
| 241 |
this.server.respond() |
| 242 |
target.innerHTML.should.equal('Content for Tab 2') |
| 243 |
}) |
| 244 |
|
| 245 |
it('handles hx-trigger with non-default value', function() { |
| 246 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 247 |
|
| 248 |
var form = make('<form hx-get="/test" hx-trigger="click">Click Me!</form>') |
| 249 |
form.click() |
| 250 |
form.innerHTML.should.equal('Click Me!') |
| 251 |
this.server.respond() |
| 252 |
form.innerHTML.should.equal('Clicked!') |
| 253 |
}) |
| 254 |
|
| 255 |
it('handles hx-trigger with load event', function() { |
| 256 |
this.server.respondWith('GET', '/test', 'Loaded!') |
| 257 |
var div = make('<div hx-get="/test" hx-trigger="load">Load Me!</div>') |
| 258 |
div.innerHTML.should.equal('Load Me!') |
| 259 |
this.server.respond() |
| 260 |
div.innerHTML.should.equal('Loaded!') |
| 261 |
}) |
| 262 |
|
| 263 |
it('sets the content type of the request properly', function(done) { |
| 264 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 265 |
xhr.respond(200, {}, 'done') |
| 266 |
xhr.overriddenMimeType.should.equal('text/html') |
| 267 |
done() |
| 268 |
}) |
| 269 |
var div = make('<div hx-get="/test">Click Me!</div>') |
| 270 |
div.click() |
| 271 |
this.server.respond() |
| 272 |
}) |
| 273 |
|
| 274 |
it('issues two requests when clicked twice before response', function() { |
| 275 |
var i = 1 |
| 276 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 277 |
xhr.respond(200, {}, 'click ' + i) |
| 278 |
i++ |
| 279 |
}) |
| 280 |
var div = make('<div hx-get="/test"></div>') |
| 281 |
div.click() |
| 282 |
div.click() |
| 283 |
this.server.respond() |
| 284 |
div.innerHTML.should.equal('click 1') |
| 285 |
this.server.respond() |
| 286 |
div.innerHTML.should.equal('click 2') |
| 287 |
}) |
| 288 |
|
| 289 |
it('issues two requests when clicked three times before response', function() { |
| 290 |
var i = 1 |
| 291 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 292 |
xhr.respond(200, {}, 'click ' + i) |
| 293 |
i++ |
| 294 |
}) |
| 295 |
var div = make('<div hx-get="/test"></div>') |
| 296 |
div.click() |
| 297 |
div.click() |
| 298 |
div.click() |
| 299 |
this.server.respondAll() |
| 300 |
div.innerHTML.should.equal('click 2') |
| 301 |
}) |
| 302 |
|
| 303 |
it('properly handles hx-select for basic situation', function() { |
| 304 |
var i = 1 |
| 305 |
this.server.respondWith('GET', '/test', "<div id='d1'>foo</div><div id='d2'>bar</div>") |
| 306 |
var div = make('<div hx-get="/test" hx-select="#d1"></div>') |
| 307 |
div.click() |
| 308 |
this.server.respond() |
| 309 |
div.innerHTML.should.equal('<div id="d1">foo</div>') |
| 310 |
}) |
| 311 |
|
| 312 |
it('properly handles hx-select for full html document situation', function() { |
| 313 |
this.server.respondWith('GET', '/test', "<html><body><div id='d1'>foo</div><div id='d2'>bar</div></body></html>") |
| 314 |
var div = make('<div hx-get="/test" hx-select="#d1"></div>') |
| 315 |
div.click() |
| 316 |
this.server.respond() |
| 317 |
div.innerHTML.should.equal('<div id="d1">foo</div>') |
| 318 |
}) |
| 319 |
|
| 320 |
it('properly settles attributes on interior elements', function(done) { |
| 321 |
this.server.respondWith('GET', '/test', "<div hx-get='/test'><div width='bar' id='d1'></div></div>") |
| 322 |
var div = make("<div hx-get='/test' hx-swap='outerHTML settle:10ms'><div id='d1'></div></div>") |
| 323 |
div.click() |
| 324 |
this.server.respond() |
| 325 |
should.equal(byId('d1').getAttribute('width'), null) |
| 326 |
setTimeout(function() { |
| 327 |
should.equal(byId('d1').getAttribute('width'), 'bar') |
| 328 |
done() |
| 329 |
}, 20) |
| 330 |
}) |
| 331 |
|
| 332 |
it('properly settles attributes elements with single quotes in id', function(done) { |
| 333 |
this.server.respondWith('GET', '/test', "<div hx-get='/test'><div width='bar' id=\"d1'\"></div></div>") |
| 334 |
var div = make("<div hx-get='/test' hx-swap='outerHTML settle:10ms'><div id=\"d1'\"></div></div>") |
| 335 |
div.click() |
| 336 |
this.server.respond() |
| 337 |
should.equal(byId("d1'").getAttribute('width'), null) |
| 338 |
setTimeout(function() { |
| 339 |
should.equal(byId("d1'").getAttribute('width'), 'bar') |
| 340 |
done() |
| 341 |
}, 20) |
| 342 |
}) |
| 343 |
|
| 344 |
it('properly settles attributes elements with double quotes in id', function(done) { |
| 345 |
this.server.respondWith('GET', '/test', "<div hx-get='/test'><div width='bar' id='d1\"'></div></div>") |
| 346 |
var div = make("<div hx-get='/test' hx-swap='outerHTML settle:10ms'><div id='d1\"'></div></div>") |
| 347 |
div.click() |
| 348 |
this.server.respond() |
| 349 |
should.equal(byId('d1"').getAttribute('width'), null) |
| 350 |
setTimeout(function() { |
| 351 |
should.equal(byId('d1"').getAttribute('width'), 'bar') |
| 352 |
done() |
| 353 |
}, 20) |
| 354 |
}) |
| 355 |
|
| 356 |
it('properly handles multiple select input', function() { |
| 357 |
var values |
| 358 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 359 |
values = getParameters(xhr) |
| 360 |
xhr.respond(204, {}, '') |
| 361 |
}) |
| 362 |
|
| 363 |
var form = make('<form hx-post="/test" hx-trigger="click">' + |
| 364 |
'<select id="multiSelect" name="multiSelect" multiple="multiple">' + |
| 365 |
'<option id="m1" value="m1">m1</option>' + |
| 366 |
'<option id="m2" value="m2">m2</option>' + |
| 367 |
'<option id="m3" value="m3">m3</option>' + |
| 368 |
'<option id="m4" value="m4">m4</option>' + |
| 369 |
'</select>' + |
| 370 |
'</form>') |
| 371 |
|
| 372 |
form.click() |
| 373 |
this.server.respond() |
| 374 |
values.should.deep.equal({}) |
| 375 |
|
| 376 |
byId('m1').selected = true |
| 377 |
form.click() |
| 378 |
this.server.respond() |
| 379 |
values.should.deep.equal({ multiSelect: 'm1' }) |
| 380 |
|
| 381 |
byId('m1').selected = true |
| 382 |
byId('m3').selected = true |
| 383 |
form.click() |
| 384 |
this.server.respond() |
| 385 |
values.should.deep.equal({ multiSelect: ['m1', 'm3'] }) |
| 386 |
}) |
| 387 |
|
| 388 |
it('properly handles multiple select input when "multiple" attribute is empty string', function() { |
| 389 |
var values |
| 390 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 391 |
values = getParameters(xhr) |
| 392 |
xhr.respond(204, {}, '') |
| 393 |
}) |
| 394 |
|
| 395 |
var form = make('<form hx-post="/test" hx-trigger="click">' + |
| 396 |
'<select name="multiSelect" id="id_question_list" multiple="" tabindex="-1" aria-hidden="true">' + |
| 397 |
'<option id="m1" value="m1">m1</option>' + |
| 398 |
'<option id="m2" value="m2">m2</option>' + |
| 399 |
'<option id="m3" value="m3">m3</option>' + |
| 400 |
'<option id="m4" value="m4">m4</option>' + |
| 401 |
'</select>' + |
| 402 |
'</form>') |
| 403 |
|
| 404 |
form.click() |
| 405 |
this.server.respond() |
| 406 |
values.should.deep.equal({}) |
| 407 |
|
| 408 |
byId('m1').selected = true |
| 409 |
form.click() |
| 410 |
this.server.respond() |
| 411 |
values.should.deep.equal({ multiSelect: 'm1' }) |
| 412 |
|
| 413 |
byId('m1').selected = true |
| 414 |
byId('m3').selected = true |
| 415 |
form.click() |
| 416 |
this.server.respond() |
| 417 |
values.should.deep.equal({ multiSelect: ['m1', 'm3'] }) |
| 418 |
}) |
| 419 |
|
| 420 |
it('properly handles two multiple select inputs w/ same name', function() { |
| 421 |
var values |
| 422 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 423 |
values = getParameters(xhr) |
| 424 |
xhr.respond(204, {}, '') |
| 425 |
}) |
| 426 |
|
| 427 |
var form = make('<form hx-post="/test" hx-trigger="click">' + |
| 428 |
'<select id="multiSelect" name="multiSelect" multiple="multiple">' + |
| 429 |
'<option id="m1" value="m1">m1</option>' + |
| 430 |
'<option id="m2" value="m2">m2</option>' + |
| 431 |
'<option id="m3" value="m3">m3</option>' + |
| 432 |
'<option id="m4" value="m4">m4</option>' + |
| 433 |
'</select>' + |
| 434 |
'<select id="multiSelect" name="multiSelect" multiple="multiple">' + |
| 435 |
'<option id="m5" value="m5">m1</option>' + |
| 436 |
'<option id="m6" value="m6">m2</option>' + |
| 437 |
'<option id="m7" value="m7">m3</option>' + |
| 438 |
'<option id="m8" value="m8">m4</option>' + |
| 439 |
'</select>' + |
| 440 |
'</form>') |
| 441 |
|
| 442 |
form.click() |
| 443 |
this.server.respond() |
| 444 |
values.should.deep.equal({}) |
| 445 |
|
| 446 |
byId('m1').selected = true |
| 447 |
form.click() |
| 448 |
this.server.respond() |
| 449 |
values.should.deep.equal({ multiSelect: 'm1' }) |
| 450 |
|
| 451 |
byId('m1').selected = true |
| 452 |
byId('m3').selected = true |
| 453 |
byId('m7').selected = true |
| 454 |
byId('m8').selected = true |
| 455 |
form.click() |
| 456 |
this.server.respond() |
| 457 |
values.should.deep.equal({ multiSelect: ['m1', 'm3', 'm7', 'm8'] }) |
| 458 |
}) |
| 459 |
|
| 460 |
it('properly handles multiple email input', function() { |
| 461 |
var values |
| 462 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 463 |
values = getParameters(xhr) |
| 464 |
xhr.respond(204, {}, '') |
| 465 |
}) |
| 466 |
|
| 467 |
var form = make('<form hx-post="/test" hx-trigger="click">' + |
| 468 |
'<input id="multiEmail" name="multiEmail" multiple>' + |
| 469 |
'</form>') |
| 470 |
|
| 471 |
form.click() |
| 472 |
this.server.respond() |
| 473 |
values.should.deep.equal({ multiEmail: '' }) |
| 474 |
|
| 475 |
byId('multiEmail').value = '[email protected]' |
| 476 |
form.click() |
| 477 |
this.server.respond() |
| 478 |
values.should.deep.equal({ multiEmail: '[email protected]' }) |
| 479 |
|
| 480 |
byId('multiEmail').value = '[email protected],[email protected]' |
| 481 |
form.click() |
| 482 |
this.server.respond() |
| 483 |
values.should.deep.equal({ multiEmail: '[email protected],[email protected]' }) |
| 484 |
}) |
| 485 |
|
| 486 |
it('properly handles checkbox inputs', function() { |
| 487 |
var values |
| 488 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 489 |
values = getParameters(xhr) |
| 490 |
xhr.respond(204, {}, '') |
| 491 |
}) |
| 492 |
|
| 493 |
var form = make('<form hx-post="/test" hx-trigger="click">' + |
| 494 |
'<input id="cb1" name="c1" value="cb1" type="checkbox">' + |
| 495 |
'<input id="cb2" name="c1" value="cb2" type="checkbox">' + |
| 496 |
'<input id="cb3" name="c1" value="cb3" type="checkbox">' + |
| 497 |
'<input id="cb4" name="c2" value="cb4" type="checkbox">' + |
| 498 |
'<input id="cb5" name="c2" value="cb5" type="checkbox">' + |
| 499 |
'<input id="cb6" name="c3" value="cb6" type="checkbox">' + |
| 500 |
'</form>') |
| 501 |
|
| 502 |
form.click() |
| 503 |
this.server.respond() |
| 504 |
values.should.deep.equal({}) |
| 505 |
|
| 506 |
byId('cb1').checked = true |
| 507 |
form.click() |
| 508 |
this.server.respond() |
| 509 |
values.should.deep.equal({ c1: 'cb1' }) |
| 510 |
|
| 511 |
byId('cb1').checked = true |
| 512 |
byId('cb2').checked = true |
| 513 |
form.click() |
| 514 |
this.server.respond() |
| 515 |
values.should.deep.equal({ c1: ['cb1', 'cb2'] }) |
| 516 |
|
| 517 |
byId('cb1').checked = true |
| 518 |
byId('cb2').checked = true |
| 519 |
byId('cb3').checked = true |
| 520 |
form.click() |
| 521 |
this.server.respond() |
| 522 |
values.should.deep.equal({ c1: ['cb1', 'cb2', 'cb3'] }) |
| 523 |
|
| 524 |
byId('cb1').checked = true |
| 525 |
byId('cb2').checked = true |
| 526 |
byId('cb3').checked = true |
| 527 |
byId('cb4').checked = true |
| 528 |
form.click() |
| 529 |
this.server.respond() |
| 530 |
values.should.deep.equal({ c1: ['cb1', 'cb2', 'cb3'], c2: 'cb4' }) |
| 531 |
|
| 532 |
byId('cb1').checked = true |
| 533 |
byId('cb2').checked = true |
| 534 |
byId('cb3').checked = true |
| 535 |
byId('cb4').checked = true |
| 536 |
byId('cb5').checked = true |
| 537 |
form.click() |
| 538 |
this.server.respond() |
| 539 |
values.should.deep.equal({ c1: ['cb1', 'cb2', 'cb3'], c2: ['cb4', 'cb5'] }) |
| 540 |
|
| 541 |
byId('cb1').checked = true |
| 542 |
byId('cb2').checked = true |
| 543 |
byId('cb3').checked = true |
| 544 |
byId('cb4').checked = true |
| 545 |
byId('cb5').checked = true |
| 546 |
byId('cb6').checked = true |
| 547 |
form.click() |
| 548 |
this.server.respond() |
| 549 |
values.should.deep.equal({ c1: ['cb1', 'cb2', 'cb3'], c2: ['cb4', 'cb5'], c3: 'cb6' }) |
| 550 |
|
| 551 |
byId('cb1').checked = true |
| 552 |
byId('cb2').checked = false |
| 553 |
byId('cb3').checked = true |
| 554 |
byId('cb4').checked = false |
| 555 |
byId('cb5').checked = true |
| 556 |
byId('cb6').checked = true |
| 557 |
form.click() |
| 558 |
this.server.respond() |
| 559 |
values.should.deep.equal({ c1: ['cb1', 'cb3'], c2: 'cb5', c3: 'cb6' }) |
| 560 |
}) |
| 561 |
|
| 562 |
it('properly handles radio inputs', function() { |
| 563 |
var values |
| 564 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 565 |
values = getParameters(xhr) |
| 566 |
xhr.respond(204, {}, '') |
| 567 |
}) |
| 568 |
|
| 569 |
var form = make('<form hx-post="/test" hx-trigger="click">' + |
| 570 |
'<div role="radiogroup">' + |
| 571 |
'<input id="rb1" name="r1" value="rb1" type="radio">' + |
| 572 |
'<input id="rb2" name="r1" value="rb2" type="radio">' + |
| 573 |
'<input id="rb3" name="r1" value="rb3" type="radio">' + |
| 574 |
'<input id="rb4" name="r2" value="rb4" type="radio">' + |
| 575 |
'<input id="rb5" name="r2" value="rb5" type="radio">' + |
| 576 |
'<input id="rb6" name="r3" value="rb6" type="radio">' + |
| 577 |
'</div>' + |
| 578 |
'</form>') |
| 579 |
|
| 580 |
form.click() |
| 581 |
this.server.respond() |
| 582 |
values.should.deep.equal({}) |
| 583 |
|
| 584 |
byId('rb1').checked = true |
| 585 |
form.click() |
| 586 |
this.server.respond() |
| 587 |
values.should.deep.equal({ r1: 'rb1' }) |
| 588 |
}) |
| 589 |
|
| 590 |
it('text nodes dont screw up settling via variable capture', function() { |
| 591 |
this.server.respondWith('GET', '/test', "<div id='d1' hx-trigger='click consume' hx-get='/test2'></div>fooo") |
| 592 |
this.server.respondWith('GET', '/test2', 'clicked') |
| 593 |
var div = make("<div hx-get='/test'/>") |
| 594 |
div.click() |
| 595 |
this.server.respond() |
| 596 |
byId('d1').click() |
| 597 |
this.server.respond() |
| 598 |
byId('d1').innerHTML.should.equal('clicked') |
| 599 |
}) |
| 600 |
|
| 601 |
it('script nodes evaluate', function() { |
| 602 |
var globalWasCalled = false |
| 603 |
window.callGlobal = function() { |
| 604 |
globalWasCalled = true |
| 605 |
} |
| 606 |
try { |
| 607 |
this.server.respondWith('GET', '/test', "<div></div><script type='text/javascript'>callGlobal()</script>") |
| 608 |
var div = make("<div hx-get='/test'></div>") |
| 609 |
div.click() |
| 610 |
this.server.respond() |
| 611 |
globalWasCalled.should.equal(true) |
| 612 |
} finally { |
| 613 |
delete window.callGlobal |
| 614 |
} |
| 615 |
}) |
| 616 |
|
| 617 |
it('stand alone script nodes evaluate', function() { |
| 618 |
var globalWasCalled = false |
| 619 |
window.callGlobal = function() { |
| 620 |
globalWasCalled = true |
| 621 |
} |
| 622 |
try { |
| 623 |
this.server.respondWith('GET', '/test', "<script type='text/javascript'>callGlobal()</script>") |
| 624 |
var div = make("<div hx-get='/test'></div>") |
| 625 |
div.click() |
| 626 |
this.server.respond() |
| 627 |
globalWasCalled.should.equal(true) |
| 628 |
} finally { |
| 629 |
delete window.callGlobal |
| 630 |
} |
| 631 |
}) |
| 632 |
|
| 633 |
it('script nodes can define global functions', function() { |
| 634 |
try { |
| 635 |
window.foo = {} |
| 636 |
this.server.respondWith('GET', '/test', "<script type='text/javascript'>foo.bar = function() { return 42 }</script>") |
| 637 |
var div = make("<div hx-get='/test'></div>") |
| 638 |
div.click() |
| 639 |
this.server.respond() |
| 640 |
foo.bar().should.equal(42) |
| 641 |
} finally { |
| 642 |
delete window.foo |
| 643 |
} |
| 644 |
}) |
| 645 |
|
| 646 |
it('child script nodes evaluate when children', function() { |
| 647 |
var globalWasCalled = false |
| 648 |
window.callGlobal = function() { |
| 649 |
globalWasCalled = true |
| 650 |
} |
| 651 |
try { |
| 652 |
this.server.respondWith('GET', '/test', "<div><script type='text/javascript'>callGlobal()</script></div>") |
| 653 |
var div = make("<div hx-get='/test'></div>") |
| 654 |
div.click() |
| 655 |
this.server.respond() |
| 656 |
globalWasCalled.should.equal(true) |
| 657 |
} finally { |
| 658 |
delete window.callGlobal |
| 659 |
} |
| 660 |
}) |
| 661 |
|
| 662 |
it('child script nodes evaluate when first child', function() { |
| 663 |
var globalWasCalled = false |
| 664 |
window.callGlobal = function() { |
| 665 |
globalWasCalled = true |
| 666 |
} |
| 667 |
try { |
| 668 |
this.server.respondWith('GET', '/test', "<script type='text/javascript'>callGlobal()</script><div></div>") |
| 669 |
var div = make("<div hx-get='/test'></div>") |
| 670 |
div.click() |
| 671 |
this.server.respond() |
| 672 |
globalWasCalled.should.equal(true) |
| 673 |
} finally { |
| 674 |
delete window.callGlobal |
| 675 |
} |
| 676 |
}) |
| 677 |
|
| 678 |
it('child script nodes evaluate when not explicitly marked javascript', function() { |
| 679 |
var globalWasCalled = false |
| 680 |
window.callGlobal = function() { |
| 681 |
globalWasCalled = true |
| 682 |
} |
| 683 |
try { |
| 684 |
this.server.respondWith('GET', '/test', '<div><script>callGlobal()</script></div>') |
| 685 |
var div = make("<div hx-get='/test'></div>") |
| 686 |
div.click() |
| 687 |
this.server.respond() |
| 688 |
globalWasCalled.should.equal(true) |
| 689 |
} finally { |
| 690 |
delete window.callGlobal |
| 691 |
} |
| 692 |
}) |
| 693 |
|
| 694 |
it('script nodes do not evaluate when explicitly marked as something other than javascript', function() { |
| 695 |
var globalWasCalled = false |
| 696 |
window.callGlobal = function() { |
| 697 |
globalWasCalled = true |
| 698 |
} |
| 699 |
try { |
| 700 |
this.server.respondWith('GET', '/test', "<div><script type='text/samplescript'>callGlobal()</script></div>") |
| 701 |
var div = make("<div hx-get='/test'></div>") |
| 702 |
div.click() |
| 703 |
this.server.respond() |
| 704 |
globalWasCalled.should.equal(false) |
| 705 |
} finally { |
| 706 |
delete window.callGlobal |
| 707 |
} |
| 708 |
}) |
| 709 |
|
| 710 |
it('script nodes evaluate after swap', function() { |
| 711 |
window.callGlobal = function() { |
| 712 |
console.log('Here...') |
| 713 |
window.tempVal = byId('d1').innerText |
| 714 |
} |
| 715 |
try { |
| 716 |
this.server.respondWith('GET', '/test', "<div><script>callGlobal()</script><div id='d1'>After settle...</div> </div>") |
| 717 |
var div = make("<div hx-get='/test'></div>") |
| 718 |
div.click() |
| 719 |
this.server.respond() |
| 720 |
window.tempVal.should.equal('After settle...') |
| 721 |
} finally { |
| 722 |
delete window.callGlobal |
| 723 |
delete window.tempVal |
| 724 |
} |
| 725 |
}) |
| 726 |
|
| 727 |
it('script node exceptions do not break rendering', function() { |
| 728 |
this.skip('Rendering does not break, but the exception bubbles up and mocha reports it') |
| 729 |
this.server.respondWith('GET', '/test', "clicked<script type='text/javascript'>throw 'foo';</script>") |
| 730 |
var div = make("<div hx-get='/test'></div>") |
| 731 |
div.click() |
| 732 |
this.server.respond() |
| 733 |
div.innerText.should.equal('clicked') |
| 734 |
console.log(div.innerText) |
| 735 |
console.log('here') |
| 736 |
}) |
| 737 |
|
| 738 |
it('allows empty verb values', function() { |
| 739 |
var path = null |
| 740 |
var div = make("<div hx-get=''/>") |
| 741 |
htmx.on(div, 'htmx:configRequest', function(evt) { |
| 742 |
path = evt.detail.path |
| 743 |
return false |
| 744 |
}) |
| 745 |
div.click() |
| 746 |
this.server.respond() |
| 747 |
path.should.not.be.null |
| 748 |
}) |
| 749 |
|
| 750 |
it('allows blank verb values', function() { |
| 751 |
var path = null |
| 752 |
var div = make('<div hx-get/>') |
| 753 |
htmx.on(div, 'htmx:configRequest', function(evt) { |
| 754 |
path = evt.detail.path |
| 755 |
return false |
| 756 |
}) |
| 757 |
div.click() |
| 758 |
this.server.respond() |
| 759 |
path.should.not.be.null |
| 760 |
}) |
| 761 |
|
| 762 |
it('input values are not settle swapped (causes flicker)', function() { |
| 763 |
this.server.respondWith('GET', '/test', "<input id='i1' value='bar'/>") |
| 764 |
var input = make("<input id='i1' hx-get='/test' value='foo' hx-swap='outerHTML settle:50' hx-trigger='click'/>") |
| 765 |
input.click() |
| 766 |
this.server.respond() |
| 767 |
input = byId('i1') |
| 768 |
input.value.should.equal('bar') |
| 769 |
}) |
| 770 |
|
| 771 |
it('autofocus attribute works properly', function() { |
| 772 |
this.server.respondWith('GET', '/test', "<input id='i2' value='bar' autofocus/>") |
| 773 |
var input = make("<input id='i1' hx-get='/test' value='foo' hx-swap='afterend' hx-trigger='click'/>") |
| 774 |
input.focus() |
| 775 |
input.click() |
| 776 |
document.activeElement.should.equal(input) |
| 777 |
this.server.respond() |
| 778 |
var input2 = byId('i2') |
| 779 |
document.activeElement.should.equal(input2) |
| 780 |
}) |
| 781 |
|
| 782 |
it('autofocus attribute works properly w/ child', function() { |
| 783 |
this.server.respondWith('GET', '/test', "<div><input id='i2' value='bar' autofocus/></div>") |
| 784 |
var input = make("<input id='i1' hx-get='/test' value='foo' hx-swap='afterend' hx-trigger='click'/>") |
| 785 |
input.focus() |
| 786 |
input.click() |
| 787 |
document.activeElement.should.equal(input) |
| 788 |
this.server.respond() |
| 789 |
var input2 = byId('i2') |
| 790 |
document.activeElement.should.equal(input2) |
| 791 |
}) |
| 792 |
|
| 793 |
it('autofocus attribute works properly w/ true value', function() { |
| 794 |
this.server.respondWith('GET', '/test', "<div><input id='i2' value='bar' autofocus='true'/></div>") |
| 795 |
var input = make("<input id='i1' hx-get='/test' value='foo' hx-swap='afterend' hx-trigger='click'/>") |
| 796 |
input.focus() |
| 797 |
input.click() |
| 798 |
document.activeElement.should.equal(input) |
| 799 |
this.server.respond() |
| 800 |
var input2 = byId('i2') |
| 801 |
document.activeElement.should.equal(input2) |
| 802 |
}) |
| 803 |
|
| 804 |
it('multipart/form-data encoding works', function() { |
| 805 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 806 |
should.equal(xhr.requestHeaders['Content-Type'], undefined) |
| 807 |
if (xhr.requestBody.get) { |
| 808 |
xhr.requestBody.get('i1').should.equal('foo') |
| 809 |
} |
| 810 |
xhr.respond(200, {}, 'body: ' + xhr.requestBody) |
| 811 |
}) |
| 812 |
var form = make("<form hx-post='/test' hx-encoding='multipart/form-data' hx-trigger='click'>" + |
| 813 |
"<input name='i1' id='i1' value='foo'/>" + |
| 814 |
'</form>') |
| 815 |
form.focus() |
| 816 |
form.click() |
| 817 |
this.server.respond() |
| 818 |
}) |
| 819 |
|
| 820 |
it('removed elements do not issue requests', function() { |
| 821 |
var count = 0 |
| 822 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 823 |
count++ |
| 824 |
xhr.respond(200, {}, '') |
| 825 |
}) |
| 826 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 827 |
htmx.remove(btn) |
| 828 |
btn.click() |
| 829 |
this.server.respond() |
| 830 |
count.should.equal(0) |
| 831 |
}) |
| 832 |
|
| 833 |
it('title tags update title', function() { |
| 834 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 835 |
xhr.respond(200, {}, "<title class=''>htmx rocks!</title>Clicked!") |
| 836 |
}) |
| 837 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 838 |
btn.click() |
| 839 |
this.server.respond() |
| 840 |
btn.innerText.should.equal('Clicked!') |
| 841 |
window.document.title.should.equal('htmx rocks!') |
| 842 |
}) |
| 843 |
|
| 844 |
it('svg title tags do not update title', function() { |
| 845 |
var originalTitle = window.document.title |
| 846 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 847 |
xhr.respond(200, {}, "<svg class=''><title>" + originalTitle + 'UPDATE' + '</title></svg>Clicked!') |
| 848 |
}) |
| 849 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 850 |
btn.click() |
| 851 |
this.server.respond() |
| 852 |
btn.innerText.should.equal('Clicked!') |
| 853 |
window.document.title.should.equal(originalTitle) |
| 854 |
}) |
| 855 |
|
| 856 |
it('first title tag outside svg title tags updates title', function() { |
| 857 |
var originalTitle = window.document.title |
| 858 |
var newTitle = originalTitle + '!!!' |
| 859 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 860 |
xhr.respond(200, {}, "<title class=''>" + newTitle + "</title><svg class=''><title>foo</title></svg>Clicked!<title class=''>x</title>") |
| 861 |
}) |
| 862 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 863 |
btn.click() |
| 864 |
this.server.respond() |
| 865 |
btn.innerText.should.equal('Clicked!') |
| 866 |
window.document.title.should.equal(newTitle) |
| 867 |
}) |
| 868 |
|
| 869 |
it('title update does not URL escape', function() { |
| 870 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 871 |
xhr.respond(200, {}, '<title></> htmx rocks!</title>Clicked!') |
| 872 |
}) |
| 873 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 874 |
btn.click() |
| 875 |
this.server.respond() |
| 876 |
btn.innerText.should.equal('Clicked!') |
| 877 |
window.document.title.should.equal('</> htmx rocks!') |
| 878 |
}) |
| 879 |
|
| 880 |
it('by default 400 content is not swapped', function() { |
| 881 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 882 |
xhr.respond(400, {}, 'Clicked!') |
| 883 |
}) |
| 884 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 885 |
btn.click() |
| 886 |
this.server.respond() |
| 887 |
btn.innerText.should.equal('Click Me!') |
| 888 |
}) |
| 889 |
|
| 890 |
it('400 content can be swapped if configured to do so', function() { |
| 891 |
var handler = htmx.on('htmx:beforeSwap', function(event) { |
| 892 |
if (event.detail.xhr.status === 400) { |
| 893 |
event.detail.shouldSwap = true |
| 894 |
} |
| 895 |
}) |
| 896 |
|
| 897 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 898 |
xhr.respond(400, {}, 'Clicked!') |
| 899 |
}) |
| 900 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 901 |
btn.click() |
| 902 |
this.server.respond() |
| 903 |
btn.innerText.should.equal('Clicked!') |
| 904 |
htmx.off('htmx:beforeSwap', handler) |
| 905 |
}) |
| 906 |
|
| 907 |
it('400 content can be retargeted if configured to do so', function() { |
| 908 |
var handler = htmx.on('htmx:beforeSwap', function(event) { |
| 909 |
if (event.detail.xhr.status === 400) { |
| 910 |
event.detail.shouldSwap = true |
| 911 |
event.detail.target = byId('d1') |
| 912 |
} |
| 913 |
}) |
| 914 |
|
| 915 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 916 |
xhr.respond(400, {}, 'Clicked!') |
| 917 |
}) |
| 918 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 919 |
var div = make('<div id="d1"></div>') |
| 920 |
btn.click() |
| 921 |
this.server.respond() |
| 922 |
div.innerText.should.equal('Clicked!') |
| 923 |
htmx.off('htmx:beforeSwap', handler) |
| 924 |
}) |
| 925 |
|
| 926 |
it('errors are triggered only on 400+', function() { |
| 927 |
var errors = 0 |
| 928 |
var handler = htmx.on('htmx:responseError', function() { |
| 929 |
errors++ |
| 930 |
}) |
| 931 |
this.server.respondWith('GET', '/test1', function(xhr) { |
| 932 |
xhr.respond(204, {}, 'Clicked!') |
| 933 |
}) |
| 934 |
this.server.respondWith('GET', '/test2', function(xhr) { |
| 935 |
xhr.respond(400, {}, 'Clicked!') |
| 936 |
}) |
| 937 |
var btn1 = make('<button hx-get="/test1">Click Me!</button>') |
| 938 |
var btn2 = make('<button hx-get="/test2">Click Me!</button>') |
| 939 |
btn1.click() |
| 940 |
btn2.click() |
| 941 |
this.server.respond() |
| 942 |
this.server.respond() |
| 943 |
errors.should.equal(1) |
| 944 |
htmx.off('htmx:responseError', handler) |
| 945 |
}) |
| 946 |
|
| 947 |
it('content can be modified if configured to do so', function() { |
| 948 |
var handler = htmx.on('htmx:beforeSwap', function(event) { |
| 949 |
if (event.detail.xhr.status === 400) { |
| 950 |
event.detail.shouldSwap = true |
| 951 |
event.detail.serverResponse = event.detail.serverResponse + '!!' |
| 952 |
} |
| 953 |
}) |
| 954 |
|
| 955 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 956 |
xhr.respond(400, {}, 'Clicked!') |
| 957 |
}) |
| 958 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 959 |
btn.click() |
| 960 |
this.server.respond() |
| 961 |
btn.innerText.should.equal('Clicked!!!') |
| 962 |
htmx.off('htmx:beforeSwap', handler) |
| 963 |
}) |
| 964 |
|
| 965 |
it('scripts w/ src attribute are properly loaded', function(done) { |
| 966 |
try { |
| 967 |
if (byId('mocha')) { |
| 968 |
this.server.respondWith('GET', '/test', "<script id='setGlobalScript' src='setGlobal.js'></script>") |
| 969 |
} else { |
| 970 |
this.server.respondWith('GET', '/test', "<script id='setGlobalScript' src='/test/setGlobal.js'></script>") |
| 971 |
} |
| 972 |
var div = make("<div hx-get='/test'></div>") |
| 973 |
div.click() |
| 974 |
this.server.respond() |
| 975 |
byId('setGlobalScript').addEventListener('load', function() { |
| 976 |
window.globalWasCalled.should.equal(true) |
| 977 |
delete window.globalWasCalled |
| 978 |
done() |
| 979 |
}) |
| 980 |
} finally { |
| 981 |
delete window.globalWasCalled |
| 982 |
} |
| 983 |
}) |
| 984 |
|
| 985 |
it('should load tags with colon in their names', function() { |
| 986 |
this.server.respondWith('GET', '/test', '<with:colon id="foobar">Foobar</with:colon>') |
| 987 |
|
| 988 |
var btn = make('<button hx-get="/test">Give me colons!</button>') |
| 989 |
btn.click() |
| 990 |
this.server.respond() |
| 991 |
|
| 992 |
btn.innerHTML.should.equal('<with:colon id="foobar">Foobar</with:colon>') |
| 993 |
}) |
| 994 |
|
| 995 |
it('properly handles clicked submit button with a value inside a htmx form', function() { |
| 996 |
var values |
| 997 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 998 |
values = getParameters(xhr) |
| 999 |
xhr.respond(204, {}, '') |
| 1000 |
}) |
| 1001 |
|
| 1002 |
make('<form hx-post="/test">' + |
| 1003 |
'<input type="text" name="t1" value="textValue">' + |
| 1004 |
'<button id="submit" type="submit" name="b1" value="buttonValue">button</button>' + |
| 1005 |
'</form>') |
| 1006 |
|
| 1007 |
byId('submit').click() |
| 1008 |
this.server.respond() |
| 1009 |
values.should.deep.equal({ t1: 'textValue', b1: 'buttonValue' }) |
| 1010 |
}) |
| 1011 |
|
| 1012 |
it('properly handles clicked submit input with a value inside a htmx form', function() { |
| 1013 |
var values |
| 1014 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1015 |
values = getParameters(xhr) |
| 1016 |
xhr.respond(204, {}, '') |
| 1017 |
}) |
| 1018 |
|
| 1019 |
make('<form hx-post="/test">' + |
| 1020 |
'<input type="text" name="t1" value="textValue">' + |
| 1021 |
'<input id="submit" type="submit" name="b1" value="buttonValue">' + |
| 1022 |
'</form>') |
| 1023 |
|
| 1024 |
byId('submit').click() |
| 1025 |
this.server.respond() |
| 1026 |
values.should.deep.equal({ t1: 'textValue', b1: 'buttonValue' }) |
| 1027 |
}) |
| 1028 |
|
| 1029 |
it('properly handles clicked submit button with a value inside a non-htmx form', function() { |
| 1030 |
var values |
| 1031 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1032 |
values = getParameters(xhr) |
| 1033 |
xhr.respond(204, {}, '') |
| 1034 |
}) |
| 1035 |
|
| 1036 |
make('<form>' + |
| 1037 |
'<input type="text" name="t1" value="textValue">' + |
| 1038 |
'<button id="submit" type="submit" name="b1" value="buttonValue" hx-post="/test">button</button>' + |
| 1039 |
'</form>') |
| 1040 |
|
| 1041 |
byId('submit').click() |
| 1042 |
this.server.respond() |
| 1043 |
values.should.deep.equal({ t1: 'textValue', b1: 'buttonValue' }) |
| 1044 |
}) |
| 1045 |
|
| 1046 |
it('properly handles clicked submit input with a value inside a non-htmx form', function() { |
| 1047 |
var values |
| 1048 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1049 |
values = getParameters(xhr) |
| 1050 |
xhr.respond(204, {}, '') |
| 1051 |
}) |
| 1052 |
|
| 1053 |
make('<form>' + |
| 1054 |
'<input type="text" name="t1" value="textValue">' + |
| 1055 |
'<input id="submit" type="submit" name="b1" value="buttonValue" hx-post="/test">' + |
| 1056 |
'</form>') |
| 1057 |
|
| 1058 |
byId('submit').click() |
| 1059 |
this.server.respond() |
| 1060 |
values.should.deep.equal({ t1: 'textValue', b1: 'buttonValue' }) |
| 1061 |
}) |
| 1062 |
|
| 1063 |
it('properly handles clicked submit button with a value outside a htmx form', function() { |
| 1064 |
var values |
| 1065 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1066 |
values = getParameters(xhr) |
| 1067 |
xhr.respond(204, {}, '') |
| 1068 |
}) |
| 1069 |
|
| 1070 |
make('<form id="externalForm" hx-post="/test">' + |
| 1071 |
'<input type="text" name="t1" value="textValue">' + |
| 1072 |
'</form>' + |
| 1073 |
'<button id="submit" form="externalForm" type="submit" name="b1" value="buttonValue">button</button>') |
| 1074 |
|
| 1075 |
byId('submit').click() |
| 1076 |
this.server.respond() |
| 1077 |
values.should.deep.equal({ t1: 'textValue', b1: 'buttonValue' }) |
| 1078 |
}) |
| 1079 |
|
| 1080 |
it('properly handles clicked submit input with a value outside a htmx form', function() { |
| 1081 |
var values |
| 1082 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1083 |
values = getParameters(xhr) |
| 1084 |
xhr.respond(204, {}, '') |
| 1085 |
}) |
| 1086 |
|
| 1087 |
make('<form id="externalForm" hx-post="/test">' + |
| 1088 |
'<input type="text" name="t1" value="textValue">' + |
| 1089 |
'</form>' + |
| 1090 |
'<input id="submit" form="externalForm" type="submit" name="b1" value="buttonValue">') |
| 1091 |
|
| 1092 |
byId('submit').click() |
| 1093 |
this.server.respond() |
| 1094 |
values.should.deep.equal({ t1: 'textValue', b1: 'buttonValue' }) |
| 1095 |
}) |
| 1096 |
|
| 1097 |
it('properly handles clicked submit button with a value stacking with regular input', function() { |
| 1098 |
var values |
| 1099 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1100 |
values = getParameters(xhr) |
| 1101 |
xhr.respond(204, {}, '') |
| 1102 |
}) |
| 1103 |
|
| 1104 |
make('<form hx-post="/test">' + |
| 1105 |
'<input type="hidden" name="action" value="A">' + |
| 1106 |
'<button id="btnA" type="submit">A</button>' + |
| 1107 |
'<button id="btnB" type="submit" name="action" value="B">B</button>' + |
| 1108 |
'<button id="btnC" type="submit" name="action" value="C">C</button>' + |
| 1109 |
'</form>') |
| 1110 |
|
| 1111 |
byId('btnA').click() |
| 1112 |
this.server.respond() |
| 1113 |
values.should.deep.equal({ action: 'A' }) |
| 1114 |
|
| 1115 |
byId('btnB').click() |
| 1116 |
this.server.respond() |
| 1117 |
values.should.deep.equal({ action: ['A', 'B'] }) |
| 1118 |
|
| 1119 |
byId('btnC').click() |
| 1120 |
this.server.respond() |
| 1121 |
values.should.deep.equal({ action: ['A', 'C'] }) |
| 1122 |
}) |
| 1123 |
|
| 1124 |
it('properly handles clicked submit input with a value stacking with regular input', function() { |
| 1125 |
var values |
| 1126 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1127 |
values = getParameters(xhr) |
| 1128 |
xhr.respond(204, {}, '') |
| 1129 |
}) |
| 1130 |
|
| 1131 |
make('<form hx-post="/test">' + |
| 1132 |
'<input type="hidden" name="action" value="A">' + |
| 1133 |
'<input id="btnA" type="submit">A</input>' + |
| 1134 |
'<input id="btnB" type="submit" name="action" value="B">B</input>' + |
| 1135 |
'<input id="btnC" type="submit" name="action" value="C">C</input>' + |
| 1136 |
'</form>') |
| 1137 |
|
| 1138 |
byId('btnA').click() |
| 1139 |
this.server.respond() |
| 1140 |
values.should.deep.equal({ action: 'A' }) |
| 1141 |
|
| 1142 |
byId('btnB').click() |
| 1143 |
this.server.respond() |
| 1144 |
values.should.deep.equal({ action: ['A', 'B'] }) |
| 1145 |
|
| 1146 |
byId('btnC').click() |
| 1147 |
this.server.respond() |
| 1148 |
values.should.deep.equal({ action: ['A', 'C'] }) |
| 1149 |
}) |
| 1150 |
|
| 1151 |
it('properly handles clicked submit button with a value inside a form, referencing another form', function() { |
| 1152 |
var values |
| 1153 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1154 |
values = getParameters(xhr) |
| 1155 |
xhr.respond(204, {}, '') |
| 1156 |
}) |
| 1157 |
|
| 1158 |
make('<form id="externalForm" hx-post="/test">' + |
| 1159 |
'<input type="text" name="t1" value="textValue">' + |
| 1160 |
'<input type="hidden" name="b1" value="inputValue">' + |
| 1161 |
'</form>' + |
| 1162 |
'<form hx-post="/test2">' + |
| 1163 |
'<button id="submit" form="externalForm" type="submit" name="b1" value="buttonValue">button</button>' + |
| 1164 |
'</form>') |
| 1165 |
|
| 1166 |
byId('submit').click() |
| 1167 |
this.server.respond() |
| 1168 |
values.should.deep.equal({ t1: 'textValue', b1: ['inputValue', 'buttonValue'] }) |
| 1169 |
}) |
| 1170 |
|
| 1171 |
it('properly handles clicked submit input with a value inside a form, referencing another form', function() { |
| 1172 |
var values |
| 1173 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1174 |
values = getParameters(xhr) |
| 1175 |
xhr.respond(204, {}, '') |
| 1176 |
}) |
| 1177 |
|
| 1178 |
make('<form id="externalForm" hx-post="/test">' + |
| 1179 |
'<input type="text" name="t1" value="textValue">' + |
| 1180 |
'<input type="hidden" name="b1" value="inputValue">' + |
| 1181 |
'</form>' + |
| 1182 |
'<form hx-post="/test2">' + |
| 1183 |
'<input id="submit" form="externalForm" type="submit" name="b1" value="buttonValue">' + |
| 1184 |
'</form>') |
| 1185 |
|
| 1186 |
byId('submit').click() |
| 1187 |
this.server.respond() |
| 1188 |
values.should.deep.equal({ t1: 'textValue', b1: ['inputValue', 'buttonValue'] }) |
| 1189 |
}) |
| 1190 |
|
| 1191 |
it('sends referenced form values when a button referencing another form is clicked', function() { |
| 1192 |
var values |
| 1193 |
this.server.respondWith('POST', '/test3', function(xhr) { |
| 1194 |
values = getParameters(xhr) |
| 1195 |
xhr.respond(205, {}, '') |
| 1196 |
}) |
| 1197 |
|
| 1198 |
make('<form id="externalForm" hx-post="/test">' + |
| 1199 |
'<input type="text" name="t1" value="textValue">' + |
| 1200 |
'<input type="hidden" name="b1" value="inputValue">' + |
| 1201 |
'</form>' + |
| 1202 |
'<form hx-post="/test2">' + |
| 1203 |
'<input type="text" name="t1" value="checkValue">' + |
| 1204 |
'<button id="submit" form="externalForm" hx-post="/test3" type="submit" name="b1" value="buttonValue">button</button>' + |
| 1205 |
'</form>') |
| 1206 |
|
| 1207 |
byId('submit').click() |
| 1208 |
this.server.respond() |
| 1209 |
values.should.deep.equal({ t1: 'textValue', b1: ['inputValue', 'buttonValue'] }) |
| 1210 |
}) |
| 1211 |
|
| 1212 |
it('sends referenced form values when a submit input referencing another form is clicked', function() { |
| 1213 |
var values |
| 1214 |
this.server.respondWith('POST', '/test3', function(xhr) { |
| 1215 |
values = getParameters(xhr) |
| 1216 |
xhr.respond(204, {}, '') |
| 1217 |
}) |
| 1218 |
|
| 1219 |
make('<form id="externalForm" hx-post="/test">' + |
| 1220 |
'<input type="text" name="t1" value="textValue">' + |
| 1221 |
'<input type="hidden" name="b1" value="inputValue">' + |
| 1222 |
'</form>' + |
| 1223 |
'<form hx-post="/test2">' + |
| 1224 |
'<input type="text" name="t1" value="checkValue">' + |
| 1225 |
'<input id="submit" form="externalForm" hx-post="/test3" type="submit" name="b1" value="buttonValue">' + |
| 1226 |
'</form>') |
| 1227 |
|
| 1228 |
byId('submit').click() |
| 1229 |
this.server.respond() |
| 1230 |
values.should.deep.equal({ t1: 'textValue', b1: ['inputValue', 'buttonValue'] }) |
| 1231 |
}) |
| 1232 |
|
| 1233 |
it('properly handles inputs external to form', function() { |
| 1234 |
var values |
| 1235 |
this.server.respondWith('Post', '/test', function(xhr) { |
| 1236 |
values = getParameters(xhr) |
| 1237 |
xhr.respond(204, {}, '') |
| 1238 |
}) |
| 1239 |
|
| 1240 |
make('<form id="externalForm" hx-post="/test">' + |
| 1241 |
' <input type="hidden" name="b1" value="inputValue">' + |
| 1242 |
'</form>' + |
| 1243 |
'<input type="text" name="t1" value="textValue" form="externalForm">' + |
| 1244 |
'<select name="s1" form="externalForm">' + |
| 1245 |
' <option value="someValue"></option>' + |
| 1246 |
' <option value="selectValue" selected></option>' + |
| 1247 |
'</select>' + |
| 1248 |
'<button id="submit" form="externalForm" type="submit" name="b1" value="buttonValue">button</button>') |
| 1249 |
|
| 1250 |
byId('submit').click() |
| 1251 |
this.server.respond() |
| 1252 |
values.should.deep.equal({ t1: 'textValue', b1: ['inputValue', 'buttonValue'], s1: 'selectValue' }) |
| 1253 |
}) |
| 1254 |
|
| 1255 |
it('properly handles buttons with formmethod=dialog', function() { |
| 1256 |
var request = false |
| 1257 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 1258 |
request = true |
| 1259 |
xhr.respond(200, {}, '<button>Bar</button>') |
| 1260 |
}) |
| 1261 |
|
| 1262 |
make('<dialog><form hx-post="/test"><button id="submit" formmethod="dialog" name="foo" value="bar">Submit</button></form></dialog>') |
| 1263 |
|
| 1264 |
byId('submit').click() |
| 1265 |
this.server.respond() |
| 1266 |
request.should.equal(false) |
| 1267 |
}) |
| 1268 |
|
| 1269 |
it('can associate submit buttons from outside a form with the current version of the form after swap', function() { |
| 1270 |
const template = '<form ' + |
| 1271 |
'id="hello" ' + |
| 1272 |
'hx-target="#hello" ' + |
| 1273 |
'hx-select="#hello" ' + |
| 1274 |
'hx-swap="outerHTML" ' + |
| 1275 |
'hx-post="/test">\n' + |
| 1276 |
'<input id="input" type="text" name="name" />\n' + |
| 1277 |
'<button name="value" type="submit">Submit</button>\n' + |
| 1278 |
'</form>\n' + |
| 1279 |
'<button id="outside" name="outside" form="hello" type="submit">Outside</button>' |
| 1280 |
|
| 1281 |
var values |
| 1282 |
this.server.respondWith('/test', function(xhr) { |
| 1283 |
values = getParameters(xhr) |
| 1284 |
xhr.respond(200, {}, template) |
| 1285 |
}) |
| 1286 |
make(template) |
| 1287 |
const button = byId('outside') |
| 1288 |
button.focus() |
| 1289 |
button.click() |
| 1290 |
this.server.respond() |
| 1291 |
values.should.deep.equal({ name: '', outside: '' }) |
| 1292 |
button.focus() |
| 1293 |
button.click() |
| 1294 |
this.server.respond() |
| 1295 |
values.should.deep.equal({ name: '', outside: '' }) |
| 1296 |
}) |
| 1297 |
|
| 1298 |
it('properly handles form reset behaviour with a htmx enabled reset button inside a form', function() { |
| 1299 |
var values |
| 1300 |
this.server.respondWith('POST', '/reset', function(xhr) { |
| 1301 |
values = getParameters(xhr) |
| 1302 |
xhr.respond(204, {}, '') |
| 1303 |
}) |
| 1304 |
|
| 1305 |
make('<form id="externalForm" hx-post="/test">' + |
| 1306 |
'<input id="t1" type="text" name="t1" value="defaultValue">' + |
| 1307 |
'<button hx-post="/reset" id="reset" type="reset" name="b1" value="buttonValue">reset</button>' + |
| 1308 |
'</form>') |
| 1309 |
byId('t1').value = 'otherValue' |
| 1310 |
byId('reset').click() |
| 1311 |
this.server.respond() |
| 1312 |
values.should.deep.equal({ b1: 'buttonValue', t1: 'otherValue' }) |
| 1313 |
byId('t1').value.should.equal('defaultValue') |
| 1314 |
}) |
| 1315 |
|
| 1316 |
it('properly handles form reset behaviour with a htmx enabled reset button outside a form', function() { |
| 1317 |
var values |
| 1318 |
this.server.respondWith('POST', '/reset', function(xhr) { |
| 1319 |
values = getParameters(xhr) |
| 1320 |
xhr.respond(204, {}, '') |
| 1321 |
}) |
| 1322 |
|
| 1323 |
make('<form id="externalForm" hx-post="/test">' + |
| 1324 |
'<input id="t1" type="text" name="t1" value="defaultValue">' + |
| 1325 |
'</form>' + |
| 1326 |
'<button hx-post="/reset" id="reset" form="externalForm" type="reset" name="b1" value="buttonValue">reset</button>') |
| 1327 |
byId('t1').value = 'otherValue' |
| 1328 |
byId('reset').click() |
| 1329 |
this.server.respond() |
| 1330 |
values.should.deep.equal({ b1: 'buttonValue', t1: 'otherValue' }) |
| 1331 |
byId('t1').value.should.equal('defaultValue') |
| 1332 |
}) |
| 1333 |
|
| 1334 |
it('script tags get swapped in with nonce applied from inlineScriptNonce', function() { |
| 1335 |
var globalWasCalled = false |
| 1336 |
window.callGlobal = function() { |
| 1337 |
globalWasCalled = true |
| 1338 |
} |
| 1339 |
htmx.config.inlineScriptNonce = 'testnonce' |
| 1340 |
try { |
| 1341 |
this.server.respondWith('GET', '/test', "<script id='noncescript'>callGlobal()</script>") |
| 1342 |
var div = make("<div hx-get='/test'></div>") |
| 1343 |
div.click() |
| 1344 |
this.server.respond() |
| 1345 |
globalWasCalled.should.equal(true) |
| 1346 |
byId('noncescript').nonce.should.equal('testnonce') |
| 1347 |
} finally { |
| 1348 |
delete window.callGlobal |
| 1349 |
htmx.config.inlineScriptNonce = '' |
| 1350 |
} |
| 1351 |
}) |
| 1352 |
|
| 1353 |
it('normalizeScriptTags logs error when insertBefore fails', function() { |
| 1354 |
htmx.div = make('<div><script></script></div>') |
| 1355 |
htmx.div.insertBefore = undefined |
| 1356 |
htmx._('normalizeScriptTags(htmx.div)') |
| 1357 |
delete htmx.div |
| 1358 |
}) |
| 1359 |
}) |