| 1 |
describe('hx-on:* attribute', function() { |
| 2 |
beforeEach(function() { |
| 3 |
this.server = makeServer() |
| 4 |
clearWorkArea() |
| 5 |
}) |
| 6 |
afterEach(function() { |
| 7 |
this.server.restore() |
| 8 |
clearWorkArea() |
| 9 |
}) |
| 10 |
|
| 11 |
it('can handle basic events w/ no other attributes', function() { |
| 12 |
var btn = make("<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 use dashes rather than colons', function() { |
| 19 |
var btn = make("<button hx-on-click='window.foo = true'>Foo</button>") |
| 20 |
btn.click() |
| 21 |
window.foo.should.equal(true) |
| 22 |
delete window.foo |
| 23 |
}) |
| 24 |
|
| 25 |
it('can modify a parameter via htmx:configRequest', function() { |
| 26 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 27 |
var params = parseParams(xhr.requestBody) |
| 28 |
xhr.respond(200, {}, params.foo) |
| 29 |
}) |
| 30 |
var btn = make("<button hx-on:htmx:config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>") |
| 31 |
btn.click() |
| 32 |
this.server.respond() |
| 33 |
btn.innerText.should.equal('bar') |
| 34 |
}) |
| 35 |
|
| 36 |
it('can modify a parameter via htmx:configRequest with dashes', function() { |
| 37 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 38 |
var params = parseParams(xhr.requestBody) |
| 39 |
xhr.respond(200, {}, params.foo) |
| 40 |
}) |
| 41 |
var btn = make("<button hx-on-htmx-config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>") |
| 42 |
btn.click() |
| 43 |
this.server.respond() |
| 44 |
btn.innerText.should.equal('bar') |
| 45 |
}) |
| 46 |
|
| 47 |
it('expands :: shorthand into htmx:', function() { |
| 48 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 49 |
var params = parseParams(xhr.requestBody) |
| 50 |
xhr.respond(200, {}, params.foo) |
| 51 |
}) |
| 52 |
var btn = make("<button hx-on::config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>") |
| 53 |
btn.click() |
| 54 |
this.server.respond() |
| 55 |
btn.innerText.should.equal('bar') |
| 56 |
}) |
| 57 |
|
| 58 |
it('expands -- shorthand into htmx:', function() { |
| 59 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 60 |
var params = parseParams(xhr.requestBody) |
| 61 |
xhr.respond(200, {}, params.foo) |
| 62 |
}) |
| 63 |
var btn = make("<button hx-on--config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>") |
| 64 |
btn.click() |
| 65 |
this.server.respond() |
| 66 |
btn.innerText.should.equal('bar') |
| 67 |
}) |
| 68 |
|
| 69 |
it('can cancel an event via preventDefault for htmx:config-request', function() { |
| 70 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 71 |
xhr.respond(200, {}, '<button>Bar</button>') |
| 72 |
}) |
| 73 |
var btn = make("<button hx-on:htmx:config-request='event.preventDefault()' hx-post='/test' hx-swap='outerHTML'>Foo</button>") |
| 74 |
btn.click() |
| 75 |
this.server.respond() |
| 76 |
btn.innerText.should.equal('Foo') |
| 77 |
}) |
| 78 |
|
| 79 |
it('can respond to data-hx-on', function() { |
| 80 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 81 |
var params = parseParams(xhr.requestBody) |
| 82 |
xhr.respond(200, {}, params.foo) |
| 83 |
}) |
| 84 |
var btn = make("<button data-hx-on:htmx:config-request='event.detail.parameters.foo = \"bar\"' hx-post='/test'>Foo</button>") |
| 85 |
btn.click() |
| 86 |
this.server.respond() |
| 87 |
btn.innerText.should.equal('bar') |
| 88 |
}) |
| 89 |
|
| 90 |
it('has the this symbol set to the element', function() { |
| 91 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 92 |
xhr.respond(200, {}, 'foo') |
| 93 |
}) |
| 94 |
var btn = make("<button hx-on:htmx:config-request='window.elt = this' hx-post='/test'>Foo</button>") |
| 95 |
btn.click() |
| 96 |
this.server.respond() |
| 97 |
btn.innerText.should.equal('foo') |
| 98 |
btn.should.equal(window.elt) |
| 99 |
delete window.elt |
| 100 |
}) |
| 101 |
|
| 102 |
it('can handle multi-line JSON', function() { |
| 103 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 104 |
xhr.respond(200, {}, 'foo') |
| 105 |
}) |
| 106 |
var btn = make("<button hx-on:htmx:config-request='window.elt = {foo: true,\n" + |
| 107 |
" bar: false}' hx-post='/test'>Foo</button>") |
| 108 |
btn.click() |
| 109 |
this.server.respond() |
| 110 |
btn.innerText.should.equal('foo') |
| 111 |
var obj = { foo: true, bar: false } |
| 112 |
obj.should.deep.equal(window.elt) |
| 113 |
delete window.elt |
| 114 |
}) |
| 115 |
|
| 116 |
it('can handle multiple event handlers in the presence of multi-line JSON', function() { |
| 117 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 118 |
xhr.respond(200, {}, 'foo') |
| 119 |
}) |
| 120 |
var btn = make("<button hx-on:htmx:config-request='window.elt = {foo: true,\n" + |
| 121 |
" bar: false}\n'" + |
| 122 |
" hx-on:htmx:after-request='window.foo = true'" + |
| 123 |
" hx-post='/test'>Foo</button>") |
| 124 |
btn.click() |
| 125 |
this.server.respond() |
| 126 |
btn.innerText.should.equal('foo') |
| 127 |
|
| 128 |
var obj = { foo: true, bar: false } |
| 129 |
obj.should.deep.equal(window.elt) |
| 130 |
delete window.elt |
| 131 |
|
| 132 |
window.foo.should.equal(true) |
| 133 |
delete window.foo |
| 134 |
}) |
| 135 |
|
| 136 |
it('should fire when triggered by load', function() { |
| 137 |
this.server.respondWith('POST', '/test', 'test') |
| 138 |
make("<div hx-trigger='load' hx-post='/test' hx-on:htmx:config-request='foo = true'></div>") |
| 139 |
window.foo.should.equal(true) |
| 140 |
delete window.foo |
| 141 |
}) |
| 142 |
|
| 143 |
it('should fire when triggered by revealed', function() { |
| 144 |
this.server.respondWith('POST', '/test', 'test') |
| 145 |
make("<div hx-trigger='revealed' hx-post='/test' hx-on:htmx:config-request='foo = true' style='position: fixed; top: 1px; left: 1px; border: 3px solid red'></div>") |
| 146 |
window.foo.should.equal(true) |
| 147 |
delete window.foo |
| 148 |
}) |
| 149 |
|
| 150 |
it('de-initializes hx-on-* content properly', function() { |
| 151 |
window.tempCount = 0 |
| 152 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 153 |
xhr.respond(200, {}, "<button id='foo' hx-on:click=\"window.tempCount++;\">increment</button>") |
| 154 |
}) |
| 155 |
var div = make("<div hx-post='/test'>Foo</div>") |
| 156 |
|
| 157 |
|
| 158 |
div.click() |
| 159 |
this.server.respond() |
| 160 |
|
| 161 |
|
| 162 |
byId('foo').click() |
| 163 |
window.tempCount.should.equal(1) |
| 164 |
|
| 165 |
|
| 166 |
div.click() |
| 167 |
this.server.respond() |
| 168 |
|
| 169 |
|
| 170 |
byId('foo').click() |
| 171 |
window.tempCount.should.equal(2) |
| 172 |
|
| 173 |
delete window.tempCount |
| 174 |
}) |
| 175 |
|
| 176 |
it('is not evaluated when allowEval is false', function() { |
| 177 |
var calledEvent = false |
| 178 |
var handler = htmx.on('htmx:evalDisallowedError', function() { |
| 179 |
calledEvent = true |
| 180 |
}) |
| 181 |
htmx.config.allowEval = false |
| 182 |
try { |
| 183 |
var btn = make("<button hx-on:click='window.foo = true'>Foo</button>") |
| 184 |
btn.click() |
| 185 |
should.not.exist(window.foo) |
| 186 |
} finally { |
| 187 |
htmx.config.allowEval = true |
| 188 |
htmx.off('htmx:evalDisallowedError', handler) |
| 189 |
delete window.foo |
| 190 |
} |
| 191 |
calledEvent.should.equal(true) |
| 192 |
}) |
| 193 |
|
| 194 |
it('can handle being swapped using innerHTML', function() { |
| 195 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 196 |
xhr.respond(200, {}, '<button id="bar" hx-on:click="window.bar = true">Bar</button>') |
| 197 |
}) |
| 198 |
|
| 199 |
make( |
| 200 |
'<div>' + |
| 201 |
'<button id="swap" hx-get="/test" hx-target="#baz" hx-swap="innerHTML">Swap</button>' + |
| 202 |
'<div id="baz"><button id="foo" hx-on:click="window.foo = true">Foo</button></div>' + |
| 203 |
'</div>' |
| 204 |
) |
| 205 |
|
| 206 |
var fooBtn = byId('foo') |
| 207 |
fooBtn.click() |
| 208 |
window.foo.should.equal(true) |
| 209 |
|
| 210 |
var swapBtn = byId('swap') |
| 211 |
swapBtn.click() |
| 212 |
this.server.respond() |
| 213 |
|
| 214 |
var barBtn = byId('bar') |
| 215 |
barBtn.click() |
| 216 |
window.bar.should.equal(true) |
| 217 |
|
| 218 |
delete window.foo |
| 219 |
delete window.bar |
| 220 |
}) |
| 221 |
|
| 222 |
it('cleans up all handlers when the DOM updates', function() { |
| 223 |
|
| 224 |
window.foo = 0 |
| 225 |
window.bar = 0 |
| 226 |
var div = make("<div hx-on:increment-foo='window.foo++' hx-on:increment-bar='window.bar++'>Foo</div>") |
| 227 |
make('<div>Another Div</div>') |
| 228 |
|
| 229 |
|
| 230 |
htmx.trigger(div, 'increment-foo') |
| 231 |
htmx.trigger(div, 'increment-bar') |
| 232 |
window.foo.should.equal(1) |
| 233 |
window.bar.should.equal(1) |
| 234 |
|
| 235 |
|
| 236 |
delete window.foo |
| 237 |
delete window.bar |
| 238 |
}) |
| 239 |
}) |