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