| 1 |
describe('hx-alpine-compat extension', function() { |
| 2 |
|
| 3 |
let extBackup; |
| 4 |
|
| 5 |
before(async () => { |
| 6 |
extBackup = backupExtensions(); |
| 7 |
clearExtensions(); |
| 8 |
|
| 9 |
|
| 10 |
if (!window.Alpine) { |
| 11 |
let alpine = document.createElement('script'); |
| 12 |
alpine.defer = true; |
| 13 |
alpine.src = '../test/lib/alpine.js'; |
| 14 |
await new Promise(resolve => { |
| 15 |
|
| 16 |
document.addEventListener('alpine:init', resolve, { once: true }); |
| 17 |
document.head.appendChild(alpine); |
| 18 |
}); |
| 19 |
} |
| 20 |
|
| 21 |
let script = document.createElement('script'); |
| 22 |
script.src = '../src/ext/hx-alpine-compat.js'; |
| 23 |
await new Promise(resolve => { |
| 24 |
script.onload = resolve; |
| 25 |
document.head.appendChild(script); |
| 26 |
}); |
| 27 |
}) |
| 28 |
|
| 29 |
after(() => { |
| 30 |
restoreExtensions(extBackup); |
| 31 |
}) |
| 32 |
|
| 33 |
beforeEach(() => { |
| 34 |
setupTest(this.currentTest) |
| 35 |
}) |
| 36 |
|
| 37 |
afterEach(() => { |
| 38 |
cleanupTest(this.currentTest) |
| 39 |
}) |
| 40 |
|
| 41 |
it('preserves Alpine x-show style attribute during Swap and Settle', async function () { |
| 42 |
mockResponse('GET', '/test', '<div id="content" x-show="show" class="inactive">Content</div>'); |
| 43 |
const div = createProcessedHTML('<div x-data="{ show: false }"><div id="target" hx-get="/test" hx-swap="innerHTML"><div id="content" x-show="show" class="inactive">Content</div></div></div>'); |
| 44 |
|
| 45 |
await htmx.timeout(50); |
| 46 |
|
| 47 |
const target = div.querySelector('#target'); |
| 48 |
target.click(); |
| 49 |
await forRequest(); |
| 50 |
await htmx.timeout(20); |
| 51 |
|
| 52 |
|
| 53 |
const content = div.querySelector('#content'); |
| 54 |
assert.equal(content.style.display, 'none'); |
| 55 |
}) |
| 56 |
|
| 57 |
it('preserves Alpine class binding during Swap and Settle', async function () { |
| 58 |
mockResponse('GET', '/test', '<div id="tab1" :class="{ \'active\': activeTab === \'tab1\' }">Tab 1</div>'); |
| 59 |
const div = createProcessedHTML('<div x-data="{ activeTab: \'tab1\' }"><div id="target" hx-get="/test" hx-swap="innerHTML"><div id="tab1" :class="{ \'active\': activeTab === \'tab1\' }">Tab 1</div></div></div>'); |
| 60 |
|
| 61 |
await htmx.timeout(50); |
| 62 |
|
| 63 |
const target = div.querySelector('#target'); |
| 64 |
target.click(); |
| 65 |
await forRequest(); |
| 66 |
await htmx.timeout(20); |
| 67 |
|
| 68 |
|
| 69 |
const tab = div.querySelector('#tab1'); |
| 70 |
assert.isTrue(tab.classList.contains('active')); |
| 71 |
}) |
| 72 |
|
| 73 |
it('preserves Alpine x-show with outerHTML swap', async function () { |
| 74 |
mockResponse('GET', '/test', '<div id="content" x-show="show">New Content</div>'); |
| 75 |
const div = createProcessedHTML('<div x-data="{ show: false }"><div id="content" hx-get="/test" hx-swap="outerHTML" x-show="show">Old Content</div></div>'); |
| 76 |
|
| 77 |
await htmx.timeout(50); |
| 78 |
|
| 79 |
const content = div.querySelector('#content'); |
| 80 |
content.click(); |
| 81 |
await forRequest(); |
| 82 |
await htmx.timeout(20); |
| 83 |
|
| 84 |
const newContent = div.querySelector('#content'); |
| 85 |
assert.equal(newContent.style.display, 'none'); |
| 86 |
assert.equal(newContent.textContent, 'New Content'); |
| 87 |
}) |
| 88 |
|
| 89 |
it('preserves Alpine state with innerMorph swap', async function () { |
| 90 |
mockResponse('GET', '/test', '<div id="item" x-show="visible">Morphed</div>'); |
| 91 |
const div = createProcessedHTML('<div x-data="{ visible: false }"><div id="target" hx-get="/test" hx-swap="innerMorph"><div id="item" x-show="visible">Original</div></div></div>'); |
| 92 |
|
| 93 |
await htmx.timeout(50); |
| 94 |
|
| 95 |
const target = div.querySelector('#target'); |
| 96 |
target.click(); |
| 97 |
await forRequest(); |
| 98 |
await htmx.timeout(20); |
| 99 |
|
| 100 |
const item = div.querySelector('#item'); |
| 101 |
assert.equal(item.style.display, 'none'); |
| 102 |
assert.equal(item.textContent, 'Morphed'); |
| 103 |
}) |
| 104 |
|
| 105 |
it('preserves Alpine state with outerMorph swap', async function () { |
| 106 |
mockResponse('GET', '/test', '<div id="target" hx-get="/test" hx-swap="outerMorph"><div x-show="visible">Morphed</div></div>'); |
| 107 |
const div = createProcessedHTML('<div x-data="{ visible: false }"><div id="target" hx-get="/test" hx-swap="outerMorph"><div x-show="visible">Original</div></div></div>'); |
| 108 |
|
| 109 |
await htmx.timeout(50); |
| 110 |
|
| 111 |
const target = div.querySelector('#target'); |
| 112 |
target.click(); |
| 113 |
await forRequest(); |
| 114 |
await htmx.timeout(20); |
| 115 |
|
| 116 |
const newTarget = div.querySelector('#target'); |
| 117 |
const item = newTarget.querySelector('[x-show]'); |
| 118 |
assert.equal(item.style.display, 'none'); |
| 119 |
assert.equal(item.textContent, 'Morphed'); |
| 120 |
}) |
| 121 |
|
| 122 |
it('preserves Alpine state with beforebegin swap', async function () { |
| 123 |
mockResponse('GET', '/test', '<div x-show="visible">Before</div>'); |
| 124 |
const div = createProcessedHTML('<div x-data="{ visible: false }"><div id="target" hx-get="/test" hx-swap="beforebegin">Target</div></div>'); |
| 125 |
|
| 126 |
await htmx.timeout(50); |
| 127 |
|
| 128 |
const target = div.querySelector('#target'); |
| 129 |
target.click(); |
| 130 |
await forRequest(); |
| 131 |
await htmx.timeout(20); |
| 132 |
|
| 133 |
const inserted = target.previousElementSibling; |
| 134 |
assert.equal(inserted.style.display, 'none'); |
| 135 |
assert.equal(inserted.textContent, 'Before'); |
| 136 |
}) |
| 137 |
|
| 138 |
it('preserves Alpine state with afterbegin swap', async function () { |
| 139 |
mockResponse('GET', '/test', '<div x-show="visible">First Child</div>'); |
| 140 |
const div = createProcessedHTML('<div x-data="{ visible: false }"><div id="target" hx-get="/test" hx-swap="afterbegin"><span>Existing</span></div></div>'); |
| 141 |
|
| 142 |
await htmx.timeout(50); |
| 143 |
|
| 144 |
const target = div.querySelector('#target'); |
| 145 |
target.click(); |
| 146 |
await forRequest(); |
| 147 |
await htmx.timeout(20); |
| 148 |
|
| 149 |
const inserted = target.firstElementChild; |
| 150 |
assert.equal(inserted.style.display, 'none'); |
| 151 |
assert.equal(inserted.textContent, 'First Child'); |
| 152 |
}) |
| 153 |
|
| 154 |
it('preserves Alpine state with beforeend swap', async function () { |
| 155 |
mockResponse('GET', '/test', '<div x-show="visible">Last Child</div>'); |
| 156 |
const div = createProcessedHTML('<div x-data="{ visible: false }"><div id="target" hx-get="/test" hx-swap="beforeend"><span>Existing</span></div></div>'); |
| 157 |
|
| 158 |
await htmx.timeout(50); |
| 159 |
|
| 160 |
const target = div.querySelector('#target'); |
| 161 |
target.click(); |
| 162 |
await forRequest(); |
| 163 |
await htmx.timeout(20); |
| 164 |
|
| 165 |
const inserted = target.lastElementChild; |
| 166 |
assert.equal(inserted.style.display, 'none'); |
| 167 |
assert.equal(inserted.textContent, 'Last Child'); |
| 168 |
}) |
| 169 |
|
| 170 |
it('preserves Alpine state with afterend swap', async function () { |
| 171 |
mockResponse('GET', '/test', '<div x-show="visible">After</div>'); |
| 172 |
const div = createProcessedHTML('<div x-data="{ visible: false }"><div id="target" hx-get="/test" hx-swap="afterend">Target</div></div>'); |
| 173 |
|
| 174 |
await htmx.timeout(50); |
| 175 |
|
| 176 |
const target = div.querySelector('#target'); |
| 177 |
target.click(); |
| 178 |
await forRequest(); |
| 179 |
await htmx.timeout(20); |
| 180 |
|
| 181 |
const inserted = target.nextElementSibling; |
| 182 |
assert.equal(inserted.style.display, 'none'); |
| 183 |
assert.equal(inserted.textContent, 'After'); |
| 184 |
}) |
| 185 |
|
| 186 |
it('preserves x-ref during innerMorph swap', async function () { |
| 187 |
mockResponse('GET', '/test', '<span x-ref="myRef">Morphed</span>'); |
| 188 |
const div = createProcessedHTML('<div x-data="{ test() { return this.$refs.myRef?.textContent; } }"><div id="target" hx-get="/test" hx-swap="innerMorph"><span x-ref="myRef">Original</span></div></div>'); |
| 189 |
|
| 190 |
await htmx.timeout(50); |
| 191 |
|
| 192 |
const data = Alpine.$data(div); |
| 193 |
|
| 194 |
|
| 195 |
assert.equal(data.test(), 'Original'); |
| 196 |
assert.equal(div._x_refs.myRef.textContent, 'Original'); |
| 197 |
|
| 198 |
const target = div.querySelector('#target'); |
| 199 |
target.click(); |
| 200 |
await forRequest(); |
| 201 |
await htmx.timeout(20); |
| 202 |
|
| 203 |
|
| 204 |
assert.equal(data.test(), 'Morphed'); |
| 205 |
assert.equal(div._x_refs.myRef.textContent, 'Morphed'); |
| 206 |
assert.equal(div._x_refs.myRef, div.querySelector('[x-ref="myRef"]')); |
| 207 |
}) |
| 208 |
|
| 209 |
it('preserves x-ref during outerMorph swap', async function () { |
| 210 |
mockResponse('GET', '/test', '<div id="target" hx-get="/test" hx-swap="outerMorph"><span x-ref="myRef">Morphed</span></div>'); |
| 211 |
const div = createProcessedHTML('<div x-data="{ test() { return this.$refs.myRef?.textContent; } }"><div id="target" hx-get="/test" hx-swap="outerMorph"><span x-ref="myRef">Original</span></div></div>'); |
| 212 |
|
| 213 |
await htmx.timeout(50); |
| 214 |
|
| 215 |
const data = Alpine.$data(div); |
| 216 |
|
| 217 |
|
| 218 |
assert.equal(data.test(), 'Original'); |
| 219 |
assert.equal(div._x_refs.myRef.textContent, 'Original'); |
| 220 |
|
| 221 |
const target = div.querySelector('#target'); |
| 222 |
target.click(); |
| 223 |
await forRequest(); |
| 224 |
await htmx.timeout(20); |
| 225 |
|
| 226 |
|
| 227 |
assert.equal(data.test(), 'Morphed'); |
| 228 |
assert.equal(div._x_refs.myRef.textContent, 'Morphed'); |
| 229 |
assert.equal(div._x_refs.myRef, div.querySelector('[x-ref="myRef"]')); |
| 230 |
}) |
| 231 |
|
| 232 |
it('preserves x-ref when element tag changes from div to span', async function () { |
| 233 |
mockResponse('GET', '/test', '<span x-ref="myRef">Changed to Span</span>'); |
| 234 |
const div = createProcessedHTML('<div x-data="{ test() { return this.$refs.myRef?.textContent; } }"><div id="target" hx-get="/test" hx-swap="innerMorph"><div x-ref="myRef">Original Div</div></div></div>'); |
| 235 |
|
| 236 |
await htmx.timeout(50); |
| 237 |
const data = Alpine.$data(div); |
| 238 |
assert.equal(data.test(), 'Original Div'); |
| 239 |
assert.equal(div._x_refs.myRef.tagName, 'DIV'); |
| 240 |
|
| 241 |
const target = div.querySelector('#target'); |
| 242 |
target.click(); |
| 243 |
await forRequest(); |
| 244 |
await htmx.timeout(20); |
| 245 |
|
| 246 |
assert.equal(data.test(), 'Changed to Span'); |
| 247 |
assert.equal(div.querySelector('[x-ref="myRef"]').tagName, 'SPAN'); |
| 248 |
assert.equal(div._x_refs.myRef.tagName, 'SPAN'); |
| 249 |
assert.equal(div._x_refs.myRef, div.querySelector('[x-ref="myRef"]')); |
| 250 |
}) |
| 251 |
|
| 252 |
it('preserves x-ref when element is completely replaced', async function () { |
| 253 |
mockResponse('GET', '/test', '<button x-ref="myRef">Button</button>'); |
| 254 |
const div = createProcessedHTML('<div x-data="{ test() { return this.$refs.myRef?.textContent; } }"><div id="target" hx-get="/test" hx-swap="innerMorph"><span x-ref="myRef">Span</span></div></div>'); |
| 255 |
|
| 256 |
await htmx.timeout(50); |
| 257 |
const data = Alpine.$data(div); |
| 258 |
assert.equal(data.test(), 'Span'); |
| 259 |
assert.equal(div._x_refs.myRef.tagName, 'SPAN'); |
| 260 |
|
| 261 |
const target = div.querySelector('#target'); |
| 262 |
target.click(); |
| 263 |
await forRequest(); |
| 264 |
await htmx.timeout(20); |
| 265 |
|
| 266 |
assert.equal(data.test(), 'Button'); |
| 267 |
assert.equal(div.querySelector('[x-ref="myRef"]').tagName, 'BUTTON'); |
| 268 |
assert.equal(div._x_refs.myRef.tagName, 'BUTTON'); |
| 269 |
assert.equal(div._x_refs.myRef, div.querySelector('[x-ref="myRef"]')); |
| 270 |
}) |
| 271 |
|
| 272 |
it('preserves multiple x-refs during morph', async function () { |
| 273 |
mockResponse('GET', '/test', '<span x-ref="first">New First</span><span x-ref="second">New Second</span>'); |
| 274 |
const div = createProcessedHTML('<div x-data="{ test() { return this.$refs.first?.textContent + \'|\' + this.$refs.second?.textContent; } }"><div id="target" hx-get="/test" hx-swap="innerMorph"><span x-ref="first">Old First</span><span x-ref="second">Old Second</span></div></div>'); |
| 275 |
|
| 276 |
await htmx.timeout(50); |
| 277 |
const data = Alpine.$data(div); |
| 278 |
assert.equal(data.test(), 'Old First|Old Second'); |
| 279 |
assert.equal(div._x_refs.first.textContent, 'Old First'); |
| 280 |
assert.equal(div._x_refs.second.textContent, 'Old Second'); |
| 281 |
|
| 282 |
const target = div.querySelector('#target'); |
| 283 |
target.click(); |
| 284 |
await forRequest(); |
| 285 |
await htmx.timeout(20); |
| 286 |
|
| 287 |
assert.equal(data.test(), 'New First|New Second'); |
| 288 |
assert.equal(div._x_refs.first.textContent, 'New First'); |
| 289 |
assert.equal(div._x_refs.second.textContent, 'New Second'); |
| 290 |
assert.equal(div._x_refs.first, div.querySelector('[x-ref="first"]')); |
| 291 |
assert.equal(div._x_refs.second, div.querySelector('[x-ref="second"]')); |
| 292 |
}) |
| 293 |
|
| 294 |
it('preserves x-ref when ref name changes', async function () { |
| 295 |
mockResponse('GET', '/test', '<span x-ref="newRef">New Ref Name</span>'); |
| 296 |
const div = createProcessedHTML('<div x-data="{ test() { return this.$refs.oldRef?.textContent || this.$refs.newRef?.textContent; } }"><div id="target" hx-get="/test" hx-swap="innerMorph"><span x-ref="oldRef">Old Ref Name</span></div></div>'); |
| 297 |
|
| 298 |
await htmx.timeout(50); |
| 299 |
const data = Alpine.$data(div); |
| 300 |
assert.equal(data.test(), 'Old Ref Name'); |
| 301 |
assert.equal(div._x_refs.oldRef.textContent, 'Old Ref Name'); |
| 302 |
assert.isUndefined(div._x_refs.newRef); |
| 303 |
|
| 304 |
const target = div.querySelector('#target'); |
| 305 |
target.click(); |
| 306 |
await forRequest(); |
| 307 |
await htmx.timeout(20); |
| 308 |
|
| 309 |
assert.equal(data.test(), 'New Ref Name'); |
| 310 |
assert.isUndefined(div._x_refs.oldRef); |
| 311 |
assert.equal(div._x_refs.newRef.textContent, 'New Ref Name'); |
| 312 |
assert.equal(div._x_refs.newRef, div.querySelector('[x-ref="newRef"]')); |
| 313 |
}) |
| 314 |
|
| 315 |
it('preserves x-ref with nested structure changes', async function () { |
| 316 |
mockResponse('GET', '/test', '<div><p><span x-ref="nested">Deeply Nested</span></p></div>'); |
| 317 |
const div = createProcessedHTML('<div x-data="{ test() { return this.$refs.nested?.textContent; } }"><div id="target" hx-get="/test" hx-swap="innerMorph"><span x-ref="nested">Flat</span></div></div>'); |
| 318 |
|
| 319 |
await htmx.timeout(50); |
| 320 |
const data = Alpine.$data(div); |
| 321 |
assert.equal(data.test(), 'Flat'); |
| 322 |
assert.equal(div._x_refs.nested.textContent, 'Flat'); |
| 323 |
assert.equal(div._x_refs.nested.tagName, 'SPAN'); |
| 324 |
|
| 325 |
const target = div.querySelector('#target'); |
| 326 |
target.click(); |
| 327 |
await forRequest(); |
| 328 |
await htmx.timeout(20); |
| 329 |
|
| 330 |
assert.equal(data.test(), 'Deeply Nested'); |
| 331 |
assert.equal(div._x_refs.nested.textContent, 'Deeply Nested'); |
| 332 |
assert.equal(div._x_refs.nested, div.querySelector('[x-ref="nested"]')); |
| 333 |
}) |
| 334 |
|
| 335 |
it('preserves x-ref when x-data parent is inside morph window', async function () { |
| 336 |
mockResponse('GET', '/test', '<div x-data="{ test() { return this.$refs.myRef?.textContent; } }"><span x-ref="myRef">Morphed</span></div>'); |
| 337 |
const div = createProcessedHTML('<div><div id="target" hx-get="/test" hx-swap="innerMorph"><div x-data="{ test() { return this.$refs.myRef?.textContent; } }"><span x-ref="myRef">Original</span></div></div></div>'); |
| 338 |
|
| 339 |
await htmx.timeout(50); |
| 340 |
|
| 341 |
const component = div.querySelector('[x-data]'); |
| 342 |
const data = Alpine.$data(component); |
| 343 |
|
| 344 |
|
| 345 |
assert.equal(data.test(), 'Original'); |
| 346 |
assert.equal(component._x_refs.myRef.textContent, 'Original'); |
| 347 |
|
| 348 |
const target = div.querySelector('#target'); |
| 349 |
target.click(); |
| 350 |
await forRequest(); |
| 351 |
await htmx.timeout(20); |
| 352 |
|
| 353 |
|
| 354 |
const newComponent = div.querySelector('[x-data]'); |
| 355 |
const newData = Alpine.$data(newComponent); |
| 356 |
|
| 357 |
|
| 358 |
assert.equal(newData.test(), 'Morphed'); |
| 359 |
assert.equal(newComponent._x_refs.myRef.textContent, 'Morphed'); |
| 360 |
assert.equal(newComponent._x_refs.myRef, newComponent.querySelector('[x-ref="myRef"]')); |
| 361 |
}) |
| 362 |
|
| 363 |
it('preserves x-teleport during innerMorph swap', async function () { |
| 364 |
mockResponse('GET', '/test', '<template x-teleport="#teleport-target"><div id="teleported">Morphed Content</div></template>'); |
| 365 |
const div = createProcessedHTML('<div><div id="teleport-target"></div><div id="target" hx-get="/test" hx-swap="innerMorph" x-data><template x-teleport="#teleport-target"><div id="teleported">Original Content</div></template></div></div>'); |
| 366 |
|
| 367 |
await htmx.timeout(50); |
| 368 |
|
| 369 |
let teleportTarget = div.querySelector('#teleport-target'); |
| 370 |
let teleported = teleportTarget.querySelector('#teleported'); |
| 371 |
assert.equal(teleported?.textContent, 'Original Content'); |
| 372 |
|
| 373 |
const target = div.querySelector('#target'); |
| 374 |
target.click(); |
| 375 |
await forRequest(); |
| 376 |
await htmx.timeout(20); |
| 377 |
|
| 378 |
teleportTarget = div.querySelector('#teleport-target'); |
| 379 |
teleported = teleportTarget.querySelector('#teleported'); |
| 380 |
assert.equal(teleported?.textContent, 'Morphed Content'); |
| 381 |
}) |
| 382 |
|
| 383 |
it('preserves x-teleport when teleport target is outside morph window', async function () { |
| 384 |
mockResponse('GET', '/test', '<template x-teleport="body"><div id="body-teleported">Morphed to Body</div></template>'); |
| 385 |
const div = createProcessedHTML('<div id="target" hx-get="/test" hx-swap="innerMorph" x-data><template x-teleport="body"><div id="body-teleported">Original to Body</div></template></div>'); |
| 386 |
|
| 387 |
await htmx.timeout(50); |
| 388 |
|
| 389 |
let teleported = document.body.querySelector('#body-teleported'); |
| 390 |
assert.equal(teleported?.textContent, 'Original to Body'); |
| 391 |
|
| 392 |
div.click(); |
| 393 |
await forRequest(); |
| 394 |
await htmx.timeout(20); |
| 395 |
|
| 396 |
teleported = document.body.querySelector('#body-teleported'); |
| 397 |
assert.equal(teleported?.textContent, 'Morphed to Body'); |
| 398 |
|
| 399 |
teleported?.remove(); |
| 400 |
}) |
| 401 |
|
| 402 |
it('preserves x-teleport with multiple teleports', async function () { |
| 403 |
mockResponse('GET', '/test', '<template x-teleport="#target1"><span>New 1</span></template><template x-teleport="#target2"><span>New 2</span></template>'); |
| 404 |
const div = createProcessedHTML('<div><div id="target1"></div><div id="target2"></div><div id="source" hx-get="/test" hx-swap="innerMorph" x-data><template x-teleport="#target1"><span>Old 1</span></template><template x-teleport="#target2"><span>Old 2</span></template></div></div>'); |
| 405 |
|
| 406 |
await htmx.timeout(50); |
| 407 |
|
| 408 |
assert.equal(div.querySelector('#target1')?.textContent, 'Old 1'); |
| 409 |
assert.equal(div.querySelector('#target2')?.textContent, 'Old 2'); |
| 410 |
|
| 411 |
const source = div.querySelector('#source'); |
| 412 |
source.click(); |
| 413 |
await forRequest(); |
| 414 |
await htmx.timeout(20); |
| 415 |
|
| 416 |
assert.equal(div.querySelector('#target1')?.textContent, 'New 1'); |
| 417 |
assert.equal(div.querySelector('#target2')?.textContent, 'New 2'); |
| 418 |
}) |
| 419 |
|
| 420 |
it('removes teleported content when template is removed during morph', async function () { |
| 421 |
mockResponse('GET', '/test', '<div>No teleport anymore</div>'); |
| 422 |
const div = createProcessedHTML('<div><div id="teleport-target"></div><div id="target" hx-get="/test" hx-swap="innerMorph" x-data><template x-teleport="#teleport-target"><div id="teleported">Original</div></template></div></div>'); |
| 423 |
|
| 424 |
await htmx.timeout(50); |
| 425 |
|
| 426 |
|
| 427 |
let teleportTarget = div.querySelector('#teleport-target'); |
| 428 |
assert.equal(teleportTarget.children.length, 1); |
| 429 |
assert.equal(teleportTarget.textContent, 'Original'); |
| 430 |
|
| 431 |
const target = div.querySelector('#target'); |
| 432 |
target.click(); |
| 433 |
await forRequest(); |
| 434 |
await htmx.timeout(20); |
| 435 |
|
| 436 |
|
| 437 |
teleportTarget = div.querySelector('#teleport-target'); |
| 438 |
assert.equal(teleportTarget.children.length, 0); |
| 439 |
assert.equal(teleportTarget.textContent, ''); |
| 440 |
}) |
| 441 |
|
| 442 |
it('adds teleported content when template is added during morph', async function () { |
| 443 |
mockResponse('GET', '/test', '<template x-teleport="#teleport-target"><div id="teleported">New Teleport</div></template>'); |
| 444 |
const div = createProcessedHTML('<div><div id="teleport-target"></div><div id="target" hx-get="/test" hx-swap="innerMorph" x-data><div>No teleport yet</div></div></div>'); |
| 445 |
|
| 446 |
await htmx.timeout(50); |
| 447 |
|
| 448 |
let teleportTarget = div.querySelector('#teleport-target'); |
| 449 |
assert.equal(teleportTarget.children.length, 0); |
| 450 |
|
| 451 |
const target = div.querySelector('#target'); |
| 452 |
target.click(); |
| 453 |
await forRequest(); |
| 454 |
await htmx.timeout(20); |
| 455 |
|
| 456 |
teleportTarget = div.querySelector('#teleport-target'); |
| 457 |
assert.equal(teleportTarget.children.length, 1); |
| 458 |
assert.equal(teleportTarget.textContent, 'New Teleport'); |
| 459 |
}) |
| 460 |
|
| 461 |
it('changes teleport target when selector changes during morph', async function () { |
| 462 |
mockResponse('GET', '/test', '<template x-teleport="#target2"><div id="teleported">Moved</div></template>'); |
| 463 |
const div = createProcessedHTML('<div><div id="target1"></div><div id="target2"></div><div id="source" hx-get="/test" hx-swap="innerMorph" x-data><template x-teleport="#target1"><div id="teleported">Original</div></template></div></div>'); |
| 464 |
|
| 465 |
await htmx.timeout(50); |
| 466 |
|
| 467 |
|
| 468 |
assert.equal(div.querySelector('#target1')?.textContent, 'Original'); |
| 469 |
assert.equal(div.querySelector('#target2')?.textContent, ''); |
| 470 |
|
| 471 |
const source = div.querySelector('#source'); |
| 472 |
source.click(); |
| 473 |
await forRequest(); |
| 474 |
await htmx.timeout(20); |
| 475 |
|
| 476 |
|
| 477 |
assert.equal(div.querySelector('#target1')?.textContent, ''); |
| 478 |
assert.equal(div.querySelector('#target2')?.textContent, 'Moved'); |
| 479 |
}) |
| 480 |
|
| 481 |
it('preserves Alpine x-for state during innerMorph swap', async function () { |
| 482 |
mockResponse('GET', '/todos', '<template x-for="item in items" :key="item"><div x-text="item + \' (morphed)\'"></div></template>'); |
| 483 |
|
| 484 |
const div = createProcessedHTML( |
| 485 |
'<div x-data="{ items: [\'a\', \'b\', \'c\'] }">' + |
| 486 |
' <div id="list" hx-get="/todos" hx-swap="innerMorph">' + |
| 487 |
' <template x-for="item in items" :key="item">' + |
| 488 |
' <div x-text="item"></div>' + |
| 489 |
' </template>' + |
| 490 |
' </div>' + |
| 491 |
'</div>' |
| 492 |
); |
| 493 |
|
| 494 |
await htmx.timeout(50); |
| 495 |
|
| 496 |
const data = Alpine.$data(div); |
| 497 |
const list = div.querySelector('#list'); |
| 498 |
|
| 499 |
|
| 500 |
assert.equal(list.querySelectorAll('div').length, 3); |
| 501 |
|
| 502 |
list.click(); |
| 503 |
await forRequest(); |
| 504 |
await htmx.timeout(20); |
| 505 |
|
| 506 |
|
| 507 |
assert.deepEqual(data.items, ['a', 'b', 'c']); |
| 508 |
assert.equal(list.querySelectorAll('div').length, 3); |
| 509 |
assert.include(list.textContent, '(morphed)'); |
| 510 |
}) |
| 511 |
|
| 512 |
it('preserves x-id generated IDs during innerMorph and morphs input value', async function () { |
| 513 |
mockResponse('GET', '/test', '<label :for="$id(\'email\')" :id="$id(\'label\')">Email (morphed):</label><input type="email" :id="$id(\'email\')">'); |
| 514 |
|
| 515 |
const div = createProcessedHTML( |
| 516 |
'<div x-data x-id="[\'email\', \'label\']">' + |
| 517 |
' <div id="target" hx-get="/test" hx-swap="innerMorph">' + |
| 518 |
' <label :for="$id(\'email\')" :id="$id(\'label\')">Email:</label>' + |
| 519 |
' <input type="email" :id="$id(\'email\')">' + |
| 520 |
' </div>' + |
| 521 |
'</div>' |
| 522 |
); |
| 523 |
|
| 524 |
await htmx.timeout(50); |
| 525 |
|
| 526 |
const input = div.querySelector('input'); |
| 527 |
const label = div.querySelector('label'); |
| 528 |
const originalInputId = input.id; |
| 529 |
const originalLabelId = label.id; |
| 530 |
|
| 531 |
|
| 532 |
input.value = '[email protected]'; |
| 533 |
|
| 534 |
|
| 535 |
assert.isTrue(originalInputId.length > 0); |
| 536 |
assert.isTrue(originalLabelId.length > 0); |
| 537 |
assert.equal(label.getAttribute('for'), originalInputId); |
| 538 |
|
| 539 |
const target = div.querySelector('#target'); |
| 540 |
target.click(); |
| 541 |
await forRequest(); |
| 542 |
await htmx.timeout(20); |
| 543 |
|
| 544 |
const newInput = div.querySelector('input'); |
| 545 |
const newLabel = div.querySelector('label'); |
| 546 |
|
| 547 |
|
| 548 |
assert.equal(newInput.id, originalInputId); |
| 549 |
assert.equal(newLabel.id, originalLabelId); |
| 550 |
assert.equal(newLabel.getAttribute('for'), originalInputId); |
| 551 |
|
| 552 |
|
| 553 |
assert.equal(newInput.value, '[email protected]'); |
| 554 |
assert.include(newLabel.textContent, '(morphed)'); |
| 555 |
}) |
| 556 |
|
| 557 |
it('preserves x-id generated IDs during outerMorph and morphs input value', async function () { |
| 558 |
mockResponse('GET', '/test', '<div id="target" hx-get="/test" hx-swap="outerMorph"><label :for="$id(\'email\')" :id="$id(\'label\')">Email (morphed):</label><input type="email" :id="$id(\'email\')"></div>'); |
| 559 |
|
| 560 |
const div = createProcessedHTML( |
| 561 |
'<div x-data x-id="[\'email\', \'label\']">' + |
| 562 |
' <div id="target" hx-get="/test" hx-swap="outerMorph">' + |
| 563 |
' <label :for="$id(\'email\')" :id="$id(\'label\')">Email:</label>' + |
| 564 |
' <input type="email" :id="$id(\'email\')">' + |
| 565 |
' </div>' + |
| 566 |
'</div>' |
| 567 |
); |
| 568 |
|
| 569 |
await htmx.timeout(50); |
| 570 |
|
| 571 |
const input = div.querySelector('input'); |
| 572 |
const label = div.querySelector('label'); |
| 573 |
const originalInputId = input.id; |
| 574 |
const originalLabelId = label.id; |
| 575 |
|
| 576 |
|
| 577 |
input.value = '[email protected]'; |
| 578 |
|
| 579 |
|
| 580 |
assert.isTrue(originalInputId.length > 0); |
| 581 |
assert.isTrue(originalLabelId.length > 0); |
| 582 |
assert.equal(label.getAttribute('for'), originalInputId); |
| 583 |
|
| 584 |
const target = div.querySelector('#target'); |
| 585 |
target.click(); |
| 586 |
await forRequest(); |
| 587 |
await htmx.timeout(20); |
| 588 |
|
| 589 |
const newInput = div.querySelector('input'); |
| 590 |
const newLabel = div.querySelector('label'); |
| 591 |
|
| 592 |
|
| 593 |
assert.equal(newInput.id, originalInputId); |
| 594 |
assert.equal(newLabel.id, originalLabelId); |
| 595 |
assert.equal(newLabel.getAttribute('for'), originalInputId); |
| 596 |
|
| 597 |
|
| 598 |
assert.equal(newInput.value, '[email protected]'); |
| 599 |
assert.include(newLabel.textContent, '(morphed)'); |
| 600 |
}) |
| 601 |
}) |