| 1 |
describe('hx-on attribute', function() { |
| 2 |
|
| 3 |
beforeEach(() => { |
| 4 |
setupTest(this.currentTest) |
| 5 |
}) |
| 6 |
|
| 7 |
afterEach(() => { |
| 8 |
cleanupTest(this.currentTest) |
| 9 |
}) |
| 10 |
|
| 11 |
it('can handle basic events w/ no other attributes', function() { |
| 12 |
var btn = createProcessedHTML("<button hx-on:click='window.foo = true'>Foo</button>") |
| 13 |
btn.click() |
| 14 |
window.foo.should.equal(true) |
| 15 |
delete window.foo |
| 16 |
}) |
| 17 |
|
| 18 |
it('can handle custom events w/ no other attributes', function() { |
| 19 |
var btn = createProcessedHTML("<button hx-on:foo='window.foo = true'>Foo</button>") |
| 20 |
htmx.trigger(btn, "foo") |
| 21 |
window.foo.should.equal(true) |
| 22 |
delete window.foo |
| 23 |
}) |
| 24 |
|
| 25 |
it('event symbol works', function() { |
| 26 |
var btn = createProcessedHTML("<button hx-on:foo='window.foo = event'>Foo</button>") |
| 27 |
let evt = new CustomEvent("foo"); |
| 28 |
btn.dispatchEvent(evt) |
| 29 |
window.foo.should.equal(evt) |
| 30 |
delete window.foo |
| 31 |
}) |
| 32 |
|
| 33 |
it('this symbol works', function() { |
| 34 |
var btn = createProcessedHTML("<button hx-on:foo='window.foo = this'>Foo</button>") |
| 35 |
let evt = new CustomEvent("foo"); |
| 36 |
btn.dispatchEvent(evt) |
| 37 |
window.foo.should.equal(btn) |
| 38 |
delete window.foo |
| 39 |
}) |
| 40 |
|
| 41 |
it('htmx API works', async function () { |
| 42 |
var btn = createProcessedHTML( |
| 43 |
`<button hx-on:foo='await timeout(1); window.foo = 10'> |
| 44 |
Foo |
| 45 |
</button>`) |
| 46 |
let evt = new CustomEvent("foo"); |
| 47 |
btn.dispatchEvent(evt) |
| 48 |
assert.equal(window.foo, undefined); |
| 49 |
await htmx.timeout(10); |
| 50 |
assert.equal(window.foo, 10); |
| 51 |
delete window.foo |
| 52 |
}) |
| 53 |
|
| 54 |
it('htmx find works relative to element', async function () { |
| 55 |
var btn = createProcessedHTML( |
| 56 |
`<button hx-on:foo="window.foo = find('next div');"> |
| 57 |
Foo |
| 58 |
</button> |
| 59 |
<div id="foo"></div> |
| 60 |
`) |
| 61 |
let evt = new CustomEvent("foo"); |
| 62 |
btn.dispatchEvent(evt) |
| 63 |
let div = find("#foo"); |
| 64 |
assert.isNotNull(div) |
| 65 |
assert.equal(window.foo, div); |
| 66 |
delete window.foo |
| 67 |
}) |
| 68 |
|
| 69 |
it('htmx find works relative to element passed in', async function () { |
| 70 |
var btn = createProcessedHTML( |
| 71 |
`<button hx-on:foo="window.foo = find('#foo', 'next div');"> |
| 72 |
Foo |
| 73 |
</button> |
| 74 |
<div id="foo"></div> |
| 75 |
<div id="bar"></div> |
| 76 |
`) |
| 77 |
let evt = new CustomEvent("foo"); |
| 78 |
btn.dispatchEvent(evt) |
| 79 |
let div = find("#bar"); |
| 80 |
assert.isNotNull(div) |
| 81 |
assert.equal(window.foo, div); |
| 82 |
delete window.foo |
| 83 |
}) |
| 84 |
|
| 85 |
it('hx-on:: shorthand expands to htmx: event', function() { |
| 86 |
var btn = createProcessedHTML("<button hx-on::my-event='window.foo = true'>Foo</button>") |
| 87 |
htmx.trigger(btn, "htmx:my-event") |
| 88 |
window.foo.should.equal(true) |
| 89 |
delete window.foo |
| 90 |
}) |
| 91 |
|
| 92 |
it('hx-on:: shorthand does not fire on non-htmx event', function() { |
| 93 |
var btn = createProcessedHTML("<button hx-on::my-event='window.foo = true'>Foo</button>") |
| 94 |
htmx.trigger(btn, "my-event") |
| 95 |
assert.equal(window.foo, undefined) |
| 96 |
}) |
| 97 |
|
| 98 |
it('event.detail keys are exposed as bare names in handler scope', function() { |
| 99 |
let btn = createProcessedHTML('<button hx-on:zap="window.foo = path.toUpperCase()">x</button>'); |
| 100 |
btn.dispatchEvent(new CustomEvent('zap', { detail: { path: 'hello' } })); |
| 101 |
window.foo.should.equal('HELLO'); |
| 102 |
delete window.foo; |
| 103 |
}); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
it('hx-on:load fires immediately when element is processed', function() { |
| 108 |
window.foo = false; |
| 109 |
createProcessedHTML('<div hx-on:load="window.foo = true">x</div>'); |
| 110 |
window.foo.should.equal(true); |
| 111 |
delete window.foo; |
| 112 |
}); |
| 113 |
|
| 114 |
it('hx-on:load provides this as the element', function() { |
| 115 |
let div = createProcessedHTML('<div hx-on:load="window.foo = this">x</div>'); |
| 116 |
window.foo.should.equal(div); |
| 117 |
delete window.foo; |
| 118 |
}); |
| 119 |
|
| 120 |
it('hx-on:load fires on children inside a swap response', function() { |
| 121 |
window.parentLoaded = false; |
| 122 |
window.childLoaded = false; |
| 123 |
playground().innerHTML = '<div id="target">old</div>'; |
| 124 |
htmx.process(playground()); |
| 125 |
let target = playground().querySelector('#target'); |
| 126 |
|
| 127 |
target.innerHTML = '<div hx-on:load="window.parentLoaded = true"><span hx-on:load="window.childLoaded = true">new</span></div>'; |
| 128 |
htmx.process(target); |
| 129 |
window.parentLoaded.should.equal(true); |
| 130 |
window.childLoaded.should.equal(true); |
| 131 |
delete window.parentLoaded; |
| 132 |
delete window.childLoaded; |
| 133 |
}); |
| 134 |
|
| 135 |
it('hx-on:load passes a CustomEvent as event', function() { |
| 136 |
createProcessedHTML('<div hx-on:load="window.foo = event.type">x</div>'); |
| 137 |
window.foo.should.equal('load'); |
| 138 |
delete window.foo; |
| 139 |
}); |
| 140 |
|
| 141 |
}) |
| 142 |
|
| 143 |
describe('hx-on="eventSpec -> code" syntax', function() { |
| 144 |
|
| 145 |
beforeEach(() => { setupTest(this.currentTest); }); |
| 146 |
afterEach(() => { cleanupTest(); }); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
it('basic: fires handler on event', function() { |
| 151 |
let btn = createProcessedHTML('<button hx-on="click -> window.foo = true">x</button>'); |
| 152 |
btn.click(); |
| 153 |
window.foo.should.equal(true); |
| 154 |
delete window.foo; |
| 155 |
}); |
| 156 |
|
| 157 |
it('this refers to the element', function() { |
| 158 |
let btn = createProcessedHTML('<button hx-on="click -> window.foo = this">x</button>'); |
| 159 |
btn.click(); |
| 160 |
window.foo.should.equal(btn); |
| 161 |
delete window.foo; |
| 162 |
}); |
| 163 |
|
| 164 |
it('event is available in handler', function() { |
| 165 |
let btn = createProcessedHTML('<button hx-on="click -> window.foo = event.type">x</button>'); |
| 166 |
btn.click(); |
| 167 |
window.foo.should.equal('click'); |
| 168 |
delete window.foo; |
| 169 |
}); |
| 170 |
|
| 171 |
it('event.detail keys are exposed as bare names', function() { |
| 172 |
let btn = createProcessedHTML('<button hx-on="zap -> window.foo = path">x</button>'); |
| 173 |
btn.dispatchEvent(new CustomEvent('zap', { detail: { path: 'hello' } })); |
| 174 |
window.foo.should.equal('hello'); |
| 175 |
delete window.foo; |
| 176 |
}); |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
it('once: removes listener after first fire', function() { |
| 181 |
window.fooCount = 0; |
| 182 |
let btn = createProcessedHTML('<button hx-on="click once -> window.fooCount++">x</button>'); |
| 183 |
btn.click(); |
| 184 |
btn.click(); |
| 185 |
btn.click(); |
| 186 |
window.fooCount.should.equal(1); |
| 187 |
delete window.fooCount; |
| 188 |
}); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
it('prevent: calls preventDefault before handler', function() { |
| 193 |
let form = createProcessedHTML('<form hx-on="submit prevent -> window.foo = event.defaultPrevented"><button type="submit">go</button></form>'); |
| 194 |
let evt = new SubmitEvent('submit', { cancelable: true, bubbles: true }); |
| 195 |
form.dispatchEvent(evt); |
| 196 |
evt.defaultPrevented.should.equal(true); |
| 197 |
window.foo.should.equal(true); |
| 198 |
delete window.foo; |
| 199 |
}); |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
it('stop: calls stopPropagation before handler', function() { |
| 204 |
playground().innerHTML = '<div id="outer"><button hx-on="click stop -> window.foo = \'inner\'">x</button></div>'; |
| 205 |
htmx.process(playground()); |
| 206 |
let outerFired = false; |
| 207 |
playground().querySelector('#outer').addEventListener('click', () => outerFired = true); |
| 208 |
playground().querySelector('button').click(); |
| 209 |
window.foo.should.equal('inner'); |
| 210 |
outerFired.should.equal(false); |
| 211 |
delete window.foo; |
| 212 |
}); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
it('halt: preventDefault and stopPropagation', function() { |
| 217 |
playground().innerHTML = '<div id="outer"><button hx-on="click halt -> window.foo = 1">x</button></div>'; |
| 218 |
htmx.process(playground()); |
| 219 |
let outerFired = false; |
| 220 |
playground().querySelector('#outer').addEventListener('click', () => outerFired = true); |
| 221 |
let btn = playground().querySelector('button'); |
| 222 |
let evt = new MouseEvent('click', { cancelable: true, bubbles: true }); |
| 223 |
btn.dispatchEvent(evt); |
| 224 |
evt.defaultPrevented.should.equal(true); |
| 225 |
outerFired.should.equal(false); |
| 226 |
delete window.foo; |
| 227 |
}); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
it('prevent stop: equivalent to halt', function() { |
| 232 |
playground().innerHTML = '<div id="outer"><button hx-on="click prevent stop -> window.foo = 1">x</button></div>'; |
| 233 |
htmx.process(playground()); |
| 234 |
let outerFired = false; |
| 235 |
playground().querySelector('#outer').addEventListener('click', () => outerFired = true); |
| 236 |
let btn = playground().querySelector('button'); |
| 237 |
let evt = new MouseEvent('click', { cancelable: true, bubbles: true }); |
| 238 |
btn.dispatchEvent(evt); |
| 239 |
evt.defaultPrevented.should.equal(true); |
| 240 |
outerFired.should.equal(false); |
| 241 |
delete window.foo; |
| 242 |
}); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
it('delay: debounces handler', async function() { |
| 247 |
window.fooCount = 0; |
| 248 |
playground().innerHTML = '<button hx-on="click delay:30ms -> window.fooCount++">x</button>'; |
| 249 |
htmx.process(playground()); |
| 250 |
let btn = playground().querySelector('button'); |
| 251 |
btn.click(); |
| 252 |
btn.click(); |
| 253 |
btn.click(); |
| 254 |
window.fooCount.should.equal(0); |
| 255 |
await htmx.timeout(60); |
| 256 |
window.fooCount.should.equal(1); |
| 257 |
delete window.fooCount; |
| 258 |
}); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
it('throttle: throttles handler with trailing edge', async function() { |
| 263 |
window.fooCount = 0; |
| 264 |
playground().innerHTML = '<button hx-on="click throttle:30ms -> window.fooCount++">x</button>'; |
| 265 |
htmx.process(playground()); |
| 266 |
let btn = playground().querySelector('button'); |
| 267 |
btn.click(); |
| 268 |
btn.click(); |
| 269 |
btn.click(); |
| 270 |
window.fooCount.should.equal(1); |
| 271 |
await htmx.timeout(60); |
| 272 |
window.fooCount.should.equal(2); |
| 273 |
delete window.fooCount; |
| 274 |
}); |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
it('changed: only fires when value changes', function() { |
| 279 |
window.fooCount = 0; |
| 280 |
playground().innerHTML = '<input value="a" hx-on="input changed -> window.fooCount++">'; |
| 281 |
htmx.process(playground()); |
| 282 |
let inp = playground().querySelector('input'); |
| 283 |
re |
| 284 |
inp.dispatchEvent(new Event('input', { bubbles: true })); |
| 285 |
window.fooCount.should.equal(1); |
| 286 |
re |
| 287 |
inp.dispatchEvent(new Event('input', { bubbles: true })); |
| 288 |
window.fooCount.should.equal(1); |
| 289 |
re |
| 290 |
inp.value = 'b'; |
| 291 |
inp.dispatchEvent(new Event('input', { bubbles: true })); |
| 292 |
window.fooCount.should.equal(2); |
| 293 |
delete window.fooCount; |
| 294 |
}); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
it('from:document: listens on document', function() { |
| 299 |
window.foo = false; |
| 300 |
let btn = createProcessedHTML('<button hx-on="custom-evt from:document -> window.foo = true">x</button>'); |
| 301 |
document.dispatchEvent(new CustomEvent('custom-evt')); |
| 302 |
window.foo.should.equal(true); |
| 303 |
|
| 304 |
for (let l of (btn._htmx?.listeners || [])) l.fromElt.removeEventListener(l.eventName, l.handler); |
| 305 |
delete window.foo; |
| 306 |
}); |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
it('from:self: only fires when event.target is the element', function() { |
| 311 |
window.fooCount = 0; |
| 312 |
playground().innerHTML = '<div hx-on="click from:self -> window.fooCount++"><span>child</span></div>'; |
| 313 |
htmx.process(playground()); |
| 314 |
let div = playground().querySelector('div'); |
| 315 |
playground().querySelector('span').click(); ip |
| 316 |
window.fooCount.should.equal(0); |
| 317 |
div.click(); re |
| 318 |
window.fooCount.should.equal(1); |
| 319 |
delete window.fooCount; |
| 320 |
}); |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
it('from:outside: fires only when event is outside the element', function() { |
| 325 |
window.outsideCount = 0; |
| 326 |
playground().innerHTML = '<button hx-on="click from:outside -> window.outsideCount++">x</button><div id="other"></div>'; |
| 327 |
htmx.process(playground()); |
| 328 |
let btn = playground().querySelector('button'); |
| 329 |
btn.click(); ip |
| 330 |
window.outsideCount.should.equal(0); |
| 331 |
playground().querySelector('#other').click(); re |
| 332 |
window.outsideCount.should.equal(1); |
| 333 |
|
| 334 |
for (let l of (btn._htmx?.listeners || [])) l.fromElt.removeEventListener(l.eventName, l.handler); |
| 335 |
delete window.outsideCount; |
| 336 |
}); |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
it('capture: fires during capture phase', function() { |
| 341 |
let order = []; |
| 342 |
playground().innerHTML = '<div id="outer"><button hx-on="click capture -> window.captureOrder.push(\'capture\')">x</button></div>'; |
| 343 |
window.captureOrder = order; |
| 344 |
htmx.process(playground()); |
| 345 |
|
| 346 |
playground().querySelector('button').addEventListener('click', () => order.push('bubble')); |
| 347 |
playground().querySelector('button').click(); |
| 348 |
|
| 349 |
order[0].should.equal('capture'); |
| 350 |
order[1].should.equal('bubble'); |
| 351 |
delete window.captureOrder; |
| 352 |
}); |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
it('passive: addEventListener is called with passive option', function() { |
| 357 |
|
| 358 |
|
| 359 |
window.foo = false; |
| 360 |
let btn = createProcessedHTML('<button hx-on="click passive -> window.foo = true">x</button>'); |
| 361 |
btn.click(); |
| 362 |
window.foo.should.equal(true); |
| 363 |
delete window.foo; |
| 364 |
}); |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
it('[filter]: only fires when filter expression is truthy', function() { |
| 369 |
window.fooCount = 0; |
| 370 |
let btn = createProcessedHTML('<button hx-on="keydown[key==\'Enter\'] -> window.fooCount++">x</button>'); |
| 371 |
btn.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); |
| 372 |
window.fooCount.should.equal(0); |
| 373 |
btn.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); |
| 374 |
window.fooCount.should.equal(1); |
| 375 |
delete window.fooCount; |
| 376 |
}); |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
it('changed + delay: debounced changed-only handler', async function() { |
| 381 |
window.fooCount = 0; |
| 382 |
playground().innerHTML = '<input value="a" hx-on="input changed delay:30ms -> window.fooCount++">'; |
| 383 |
htmx.process(playground()); |
| 384 |
let inp = playground().querySelector('input'); |
| 385 |
ce |
| 386 |
inp.value = 'b'; |
| 387 |
inp.dispatchEvent(new Event('input', { bubbles: true })); |
| 388 |
inp.value = 'c'; |
| 389 |
inp.dispatchEvent(new Event('input', { bubbles: true })); |
| 390 |
inp.value = 'd'; |
| 391 |
inp.dispatchEvent(new Event('input', { bubbles: true })); |
| 392 |
window.fooCount.should.equal(0); |
| 393 |
await htmx.timeout(60); |
| 394 |
window.fooCount.should.equal(1); |
| 395 |
delete window.fooCount; |
| 396 |
}); |
| 397 |
|
| 398 |
it('once + from:self: only counts toward once when self matches', function() { |
| 399 |
window.fooCount = 0; |
| 400 |
playground().innerHTML = '<div hx-on="click once from:self -> window.fooCount++"><span>child</span></div>'; |
| 401 |
htmx.process(playground()); |
| 402 |
let div = playground().querySelector('div'); |
| 403 |
let span = playground().querySelector('span'); |
| 404 |
span.click(); ed |
| 405 |
span.click(); |
| 406 |
window.fooCount.should.equal(0); |
| 407 |
div.click(); ce |
| 408 |
window.fooCount.should.equal(1); |
| 409 |
div.click(); |
| 410 |
window.fooCount.should.equal(1); |
| 411 |
delete window.fooCount; |
| 412 |
}); |
| 413 |
|
| 414 |
it('once + from:outside: only counts toward once when outside matches', function() { |
| 415 |
window.outsideCount = 0; |
| 416 |
playground().innerHTML = '<button hx-on="click once from:outside -> window.outsideCount++">x</button><div id="other"></div>'; |
| 417 |
htmx.process(playground()); |
| 418 |
let btn = playground().querySelector('button'); |
| 419 |
let other = playground().querySelector('#other'); |
| 420 |
btn.click(); ed |
| 421 |
btn.click(); |
| 422 |
window.outsideCount.should.equal(0); |
| 423 |
other.click(); ce |
| 424 |
window.outsideCount.should.equal(1); |
| 425 |
other.click(); |
| 426 |
window.outsideCount.should.equal(1); |
| 427 |
for (let l of (btn._htmx?.listeners || [])) l.fromElt.removeEventListener(l.eventName, l.handler); |
| 428 |
delete window.outsideCount; |
| 429 |
}); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
it('multiple events: separated by semicolons', function() { |
| 434 |
window.focusFired = false; |
| 435 |
window.blurFired = false; |
| 436 |
playground().innerHTML = '<input hx-on="focus -> window.focusFired = true; blur -> window.blurFired = true">'; |
| 437 |
htmx.process(playground()); |
| 438 |
let inp = playground().querySelector('input'); |
| 439 |
inp.dispatchEvent(new Event('focus')); |
| 440 |
window.focusFired.should.equal(true); |
| 441 |
inp.dispatchEvent(new Event('blur')); |
| 442 |
window.blurFired.should.equal(true); |
| 443 |
delete window.focusFired; |
| 444 |
delete window.blurFired; |
| 445 |
}); |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
it('multi-statement JS works with braces', function() { |
| 450 |
window.foo = 0; |
| 451 |
let btn = createProcessedHTML('<button hx-on="click -> { window.foo++; window.foo++ }">x</button>'); |
| 452 |
btn.click(); |
| 453 |
window.foo.should.equal(2); |
| 454 |
delete window.foo; |
| 455 |
}); |
| 456 |
|
| 457 |
it('semicolons inside nested braces are preserved', function() { |
| 458 |
window.foo = 0; |
| 459 |
let btn = createProcessedHTML('<button hx-on="click -> { if (true) { window.foo++; window.foo++ } }">x</button>'); |
| 460 |
btn.click(); |
| 461 |
window.foo.should.equal(2); |
| 462 |
delete window.foo; |
| 463 |
}); |
| 464 |
|
| 465 |
it('semicolons inside parens are preserved', function() { |
| 466 |
window.foo = 0; |
| 467 |
let btn = createProcessedHTML('<button hx-on="click -> (window.foo++, window.foo++)">x</button>'); |
| 468 |
btn.click(); |
| 469 |
window.foo.should.equal(2); |
| 470 |
delete window.foo; |
| 471 |
}); |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
it('comma: multiple events fire the same handler', function() { |
| 476 |
window.fooCount = 0; |
| 477 |
let btn = createProcessedHTML('<button hx-on="click, customevt -> window.fooCount++">x</button>'); |
| 478 |
btn.click(); |
| 479 |
btn.dispatchEvent(new CustomEvent('customevt')); |
| 480 |
window.fooCount.should.equal(2); |
| 481 |
delete window.fooCount; |
| 482 |
}); |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
it('arrow functions in JS code do not break parsing', function() { |
| 487 |
let btn = createProcessedHTML('<button hx-on="click -> window.foo = [1,2,3].filter(x => x > 1)">x</button>'); |
| 488 |
btn.click(); |
| 489 |
window.foo.should.deep.equal([2, 3]); |
| 490 |
delete window.foo; |
| 491 |
}); |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
it('multi-statement and multi-event combined', function() { |
| 496 |
window.clickCount = 0; |
| 497 |
window.blurFired = false; |
| 498 |
playground().innerHTML = '<button hx-on="click -> { window.clickCount++; window.clickCount++ }; blur -> window.blurFired = true">x</button>'; |
| 499 |
htmx.process(playground()); |
| 500 |
let btn = playground().querySelector('button'); |
| 501 |
btn.click(); |
| 502 |
window.clickCount.should.equal(2); |
| 503 |
btn.dispatchEvent(new Event('blur')); |
| 504 |
window.blurFired.should.equal(true); |
| 505 |
delete window.clickCount; |
| 506 |
delete window.blurFired; |
| 507 |
}); |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
it('from:.selector: listens on matching elements', function() { |
| 512 |
window.foo = false; |
| 513 |
playground().innerHTML = '<div id="source"></div><button hx-on="customevt from:#source -> window.foo = true">x</button>'; |
| 514 |
htmx.process(playground()); |
| 515 |
playground().querySelector('#source').dispatchEvent(new CustomEvent('customevt')); |
| 516 |
window.foo.should.equal(true); |
| 517 |
delete window.foo; |
| 518 |
}); |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
it('hx-on: and hx-on= coexist on the same element', function() { |
| 523 |
window.colonFired = false; |
| 524 |
window.arrowFired = false; |
| 525 |
let btn = createProcessedHTML('<button hx-on:click="window.colonFired = true" hx-on="customevt -> window.arrowFired = true">x</button>'); |
| 526 |
btn.click(); |
| 527 |
window.colonFired.should.equal(true); |
| 528 |
btn.dispatchEvent(new CustomEvent('customevt')); |
| 529 |
window.arrowFired.should.equal(true); |
| 530 |
delete window.colonFired; |
| 531 |
delete window.arrowFired; |
| 532 |
}); |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
it('load: fires immediately when element is processed', function() { |
| 537 |
window.foo = false; |
| 538 |
createProcessedHTML('<div hx-on="load -> window.foo = true">x</div>'); |
| 539 |
window.foo.should.equal(true); |
| 540 |
delete window.foo; |
| 541 |
}); |
| 542 |
|
| 543 |
it('load: works with modifiers', function() { |
| 544 |
window.foo = false; |
| 545 |
createProcessedHTML('<div hx-on="load once -> window.foo = true">x</div>'); |
| 546 |
window.foo.should.equal(true); |
| 547 |
delete window.foo; |
| 548 |
}); |
| 549 |
|
| 550 |
it('load: provides this and event', function() { |
| 551 |
let div = createProcessedHTML('<div hx-on="load -> { window.fooThis = this; window.fooType = event.type }">x</div>'); |
| 552 |
window.fooThis.should.equal(div); |
| 553 |
window.fooType.should.equal('load'); |
| 554 |
delete window.fooThis; |
| 555 |
delete window.fooType; |
| 556 |
}); |
| 557 |
|
| 558 |
it('every: fires repeatedly on interval', async function() { |
| 559 |
window.fooCount = 0; |
| 560 |
playground().innerHTML = '<div hx-on="every 50ms -> window.fooCount++">x</div>'; |
| 561 |
htmx.process(playground()); |
| 562 |
window.fooCount.should.equal(0); |
| 563 |
await htmx.timeout(130); |
| 564 |
window.fooCount.should.be.at.least(2); |
| 565 |
|
| 566 |
playground().innerHTML = ''; |
| 567 |
delete window.fooCount; |
| 568 |
}); |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
it('load: focuses first input when element is processed', function() { |
| 573 |
let div = createProcessedHTML('<div hx-on:load="this.querySelector(\'input\')?.focus()"><input id="myinput"></div>'); |
| 574 |
document.activeElement.id.should.equal('myinput'); |
| 575 |
}); |
| 576 |
|
| 577 |
it('revealed: adds class when element intersects', function() { |
| 578 |
let originalIO = window.IntersectionObserver; |
| 579 |
let observerCallback; |
| 580 |
window.IntersectionObserver = function(cb, opts) { |
| 581 |
observerCallback = cb; |
| 582 |
return { observe: function() {}, disconnect: function() {} }; |
| 583 |
}; |
| 584 |
let div = createProcessedHTML('<div hx-on="revealed -> this.classList.add(\'visible\')">x</div>'); |
| 585 |
div.classList.contains('visible').should.equal(false); |
| 586 |
observerCallback([{ isIntersecting: true }]); |
| 587 |
div.classList.contains('visible').should.equal(true); |
| 588 |
window.IntersectionObserver = originalIO; |
| 589 |
}); |
| 590 |
|
| 591 |
it('intersect once: adds class when element becomes visible', function() { |
| 592 |
let originalIO = window.IntersectionObserver; |
| 593 |
let observerCallback, disconnected = false; |
| 594 |
window.IntersectionObserver = function(cb, opts) { |
| 595 |
observerCallback = cb; |
| 596 |
return { observe: function() {}, disconnect: function() { disconnected = true; } }; |
| 597 |
}; |
| 598 |
let div = createProcessedHTML('<div hx-on="intersect once -> this.classList.add(\'in-view\')">x</div>'); |
| 599 |
div.classList.contains('in-view').should.equal(false); |
| 600 |
observerCallback([{ isIntersecting: true }]); |
| 601 |
div.classList.contains('in-view').should.equal(true); |
| 602 |
window.IntersectionObserver = originalIO; |
| 603 |
}); |
| 604 |
|
| 605 |
it('every: updates text content on interval', async function() { |
| 606 |
let div = createProcessedHTML('<div hx-on="every 50ms -> this.textContent = \'updated\'">original</div>'); |
| 607 |
div.textContent.should.equal('original'); |
| 608 |
await htmx.timeout(80); |
| 609 |
div.textContent.should.equal('updated'); |
| 610 |
playground().innerHTML = ''; |
| 611 |
}); |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
it('hx-on:click still works (backward compat)', function() { |
| 616 |
let btn = createProcessedHTML('<button hx-on:click="window.foo = true">x</button>'); |
| 617 |
btn.click(); |
| 618 |
window.foo.should.equal(true); |
| 619 |
delete window.foo; |
| 620 |
}); |
| 621 |
|
| 622 |
it('calling process() multiple times does not duplicate hx-on listeners', function() { |
| 623 |
window.fooCount = 0; |
| 624 |
let btn = createProcessedHTML('<button hx-on:click="window.fooCount++">x</button>'); |
| 625 |
htmx.process(btn); |
| 626 |
htmx.process(btn); |
| 627 |
htmx.process(btn); |
| 628 |
btn.click(); |
| 629 |
window.fooCount.should.equal(1); |
| 630 |
delete window.fooCount; |
| 631 |
}); |
| 632 |
|
| 633 |
it('calling process() multiple times does not duplicate hx-on= listeners', function() { |
| 634 |
window.fooCount = 0; |
| 635 |
let btn = createProcessedHTML('<button hx-on="click -> window.fooCount++">x</button>'); |
| 636 |
htmx.process(btn); |
| 637 |
htmx.process(btn); |
| 638 |
htmx.process(btn); |
| 639 |
btn.click(); |
| 640 |
window.fooCount.should.equal(1); |
| 641 |
delete window.fooCount; |
| 642 |
}); |
| 643 |
}) |