| 1 |
describe('hx-history-cache + hx-alpine-compat integration', function () { |
| 2 |
|
| 3 |
let extBackup; |
| 4 |
let historyElt; |
| 5 |
|
| 6 |
before(async () => { |
| 7 |
extBackup = backupExtensions(); |
| 8 |
clearExtensions(); |
| 9 |
htmx.config.extensions = 'history-cache,alpine-compat'; |
| 10 |
htmx.__approvedExt = 'history-cache,alpine-compat'; |
| 11 |
|
| 12 |
let historyCacheScript = document.createElement('script'); |
| 13 |
historyCacheScript.src = '../src/ext/hx-history-cache.js'; |
| 14 |
await new Promise(resolve => { |
| 15 |
historyCacheScript.onload = resolve; |
| 16 |
document.head.appendChild(historyCacheScript); |
| 17 |
}); |
| 18 |
|
| 19 |
if (!window.Alpine) { |
| 20 |
await new Promise(resolve => { |
| 21 |
let alpineScript = document.createElement('script'); |
| 22 |
alpineScript.defer = true; |
| 23 |
alpineScript.src = '../test/lib/alpine.js'; |
| 24 |
document.addEventListener('alpine:init', resolve, { once: true }); |
| 25 |
document.head.appendChild(alpineScript); |
| 26 |
}); |
| 27 |
} |
| 28 |
|
| 29 |
let alpineCompatScript = document.createElement('script'); |
| 30 |
alpineCompatScript.src = '../src/ext/hx-alpine-compat.js'; |
| 31 |
await new Promise(resolve => { |
| 32 |
alpineCompatScript.onload = resolve; |
| 33 |
document.head.appendChild(alpineCompatScript); |
| 34 |
}); |
| 35 |
}); |
| 36 |
|
| 37 |
after(() => { |
| 38 |
restoreExtensions(extBackup); |
| 39 |
}); |
| 40 |
|
| 41 |
beforeEach(() => { |
| 42 |
setupTest(); |
| 43 |
sessionStorage.clear(); |
| 44 |
htmx.config.historyCache = { size: 10, refreshOnMiss: false, disable: false, swapStyle: 'innerHTML' }; |
| 45 |
|
| 46 |
historyElt = document.createElement('div'); |
| 47 |
historyElt.setAttribute('hx-history-elt', ''); |
| 48 |
historyElt.innerHTML = '<p>initial content</p>'; |
| 49 |
playground().appendChild(historyElt); |
| 50 |
}); |
| 51 |
|
| 52 |
afterEach(() => { |
| 53 |
cleanupTest(); |
| 54 |
sessionStorage.clear(); |
| 55 |
}); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
async function saveAndRestoreAlpine(setupFn) { |
| 62 |
let cachedPath = location.pathname + location.search; |
| 63 |
createProcessedHTML(` |
| 64 |
<div hx-history-elt></div> |
| 65 |
<button hx-get="/page2" hx-push-url="/page2">go</button> |
| 66 |
`); |
| 67 |
historyElt = playground().querySelector('[hx-history-elt]'); |
| 68 |
setupFn(historyElt); |
| 69 |
htmx.process(historyElt); |
| 70 |
await htmx.timeout(50); |
| 71 |
|
| 72 |
mockResponse('GET', '/page2', '<p>page 2</p>'); |
| 73 |
playground().querySelector('button').click(); |
| 74 |
await forRequest(); |
| 75 |
|
| 76 |
let index = JSON.parse(sessionStorage.getItem('htmx-history-index') || '[]'); |
| 77 |
let htmxId = index[index.length - 1]; |
| 78 |
history.replaceState({ htmx: true, htmxId }, '', cachedPath); |
| 79 |
await new Promise(resolve => { |
| 80 |
document.addEventListener('htmx:history:cache:after:restore', resolve, { once: true }); |
| 81 |
htmx.__restoreHistory(cachedPath); |
| 82 |
}); |
| 83 |
await htmx.timeout(50); |
| 84 |
return historyElt; |
| 85 |
} |
| 86 |
|
| 87 |
it('restores mutated Alpine counter after user interaction', async function () { |
| 88 |
let cachedPath = location.pathname + location.search; |
| 89 |
createProcessedHTML(` |
| 90 |
<div hx-history-elt></div> |
| 91 |
<button hx-get="/page2" hx-push-url="/page2">go</button> |
| 92 |
`); |
| 93 |
historyElt = playground().querySelector('[hx-history-elt]'); |
| 94 |
historyElt.innerHTML = '<div x-data="{ count: 0 }"><span x-text="count"></span></div>'; |
| 95 |
htmx.process(historyElt); |
| 96 |
await htmx.timeout(50); |
| 97 |
|
| 98 |
let component = historyElt.querySelector('[x-data]'); |
| 99 |
Alpine.$data(component).count = 42; |
| 100 |
await htmx.timeout(10); |
| 101 |
|
| 102 |
mockResponse('GET', '/page2', '<p>page 2</p>'); |
| 103 |
playground().querySelector('button').click(); |
| 104 |
await forRequest(); |
| 105 |
|
| 106 |
let index = JSON.parse(sessionStorage.getItem('htmx-history-index') || '[]'); |
| 107 |
let htmxId = index[index.length - 1]; |
| 108 |
history.replaceState({ htmx: true, htmxId }, '', cachedPath); |
| 109 |
await new Promise(resolve => { |
| 110 |
document.addEventListener('htmx:history:cache:after:restore', resolve, { once: true }); |
| 111 |
htmx.__restoreHistory(cachedPath); |
| 112 |
}); |
| 113 |
await htmx.timeout(50); |
| 114 |
|
| 115 |
assert.equal(historyElt.querySelector('span').textContent, '42'); |
| 116 |
}); |
| 117 |
|
| 118 |
it('restores mutated Alpine string after user interaction', async function () { |
| 119 |
let cachedPath = location.pathname + location.search; |
| 120 |
createProcessedHTML(` |
| 121 |
<div hx-history-elt></div> |
| 122 |
<button hx-get="/page2" hx-push-url="/page2">go</button> |
| 123 |
`); |
| 124 |
historyElt = playground().querySelector('[hx-history-elt]'); |
| 125 |
historyElt.innerHTML = '<div x-data="{ name: \'initial\' }"><span x-text="name"></span></div>'; |
| 126 |
htmx.process(historyElt); |
| 127 |
await htmx.timeout(50); |
| 128 |
|
| 129 |
Alpine.$data(historyElt.querySelector('[x-data]')).name = 'changed'; |
| 130 |
await htmx.timeout(10); |
| 131 |
|
| 132 |
mockResponse('GET', '/page2', '<p>page 2</p>'); |
| 133 |
playground().querySelector('button').click(); |
| 134 |
await forRequest(); |
| 135 |
|
| 136 |
let index = JSON.parse(sessionStorage.getItem('htmx-history-index') || '[]'); |
| 137 |
let htmxId = index[index.length - 1]; |
| 138 |
history.replaceState({ htmx: true, htmxId }, '', cachedPath); |
| 139 |
await new Promise(resolve => { |
| 140 |
document.addEventListener('htmx:history:cache:after:restore', resolve, { once: true }); |
| 141 |
htmx.__restoreHistory(cachedPath); |
| 142 |
}); |
| 143 |
await htmx.timeout(50); |
| 144 |
|
| 145 |
assert.equal(historyElt.querySelector('span').textContent, 'changed'); |
| 146 |
}); |
| 147 |
|
| 148 |
it('restores nested x-data components independently', async function () { |
| 149 |
let elt = await saveAndRestoreAlpine(root => { |
| 150 |
root.innerHTML = |
| 151 |
'<div x-data="{ outer: \'A\' }">' + |
| 152 |
' <span id="outer-val" x-text="outer"></span>' + |
| 153 |
' <div x-data="{ inner: 42 }">' + |
| 154 |
' <span id="inner-val" x-text="inner"></span>' + |
| 155 |
' </div>' + |
| 156 |
'</div>'; |
| 157 |
}); |
| 158 |
assert.equal(elt.querySelector('#outer-val').textContent, 'A'); |
| 159 |
assert.equal(elt.querySelector('#inner-val').textContent, '42'); |
| 160 |
}); |
| 161 |
|
| 162 |
it('does not duplicate teleported content after history restore', async function () { |
| 163 |
let cachedPath = location.pathname + location.search; |
| 164 |
createProcessedHTML(` |
| 165 |
<div id="tp-target"></div> |
| 166 |
<div hx-history-elt></div> |
| 167 |
<button hx-get="/page2" hx-push-url="/page2">go</button> |
| 168 |
`); |
| 169 |
historyElt = playground().querySelector('[hx-history-elt]'); |
| 170 |
historyElt.innerHTML = |
| 171 |
'<div x-data="{ open: true }">' + |
| 172 |
' <template x-teleport="#tp-target"><div class="tp-content">teleported</div></template>' + |
| 173 |
'</div>'; |
| 174 |
htmx.process(historyElt); |
| 175 |
await htmx.timeout(50); |
| 176 |
|
| 177 |
mockResponse('GET', '/page2', '<p>page 2</p>'); |
| 178 |
playground().querySelector('button').click(); |
| 179 |
await forRequest(); |
| 180 |
|
| 181 |
let index = JSON.parse(sessionStorage.getItem('htmx-history-index') || '[]'); |
| 182 |
let htmxId = index[index.length - 1]; |
| 183 |
history.replaceState({ htmx: true, htmxId }, '', cachedPath); |
| 184 |
await new Promise(resolve => { |
| 185 |
document.addEventListener('htmx:history:cache:after:restore', resolve, { once: true }); |
| 186 |
htmx.__restoreHistory(cachedPath); |
| 187 |
}); |
| 188 |
await htmx.timeout(50); |
| 189 |
|
| 190 |
let teleportTarget = playground().querySelector('#tp-target'); |
| 191 |
assert.equal(teleportTarget.querySelectorAll('.tp-content').length, 1); |
| 192 |
}); |
| 193 |
|
| 194 |
it('does not throw when restoring state with getter and method properties', async function () { |
| 195 |
let elt = await saveAndRestoreAlpine(root => { |
| 196 |
root.innerHTML = |
| 197 |
'<div x-data="{' + |
| 198 |
' selectedCategory: \'all\',' + |
| 199 |
' searchTerm: \'htmx\',' + |
| 200 |
' cartItems: 3,' + |
| 201 |
' products: [],' + |
| 202 |
' get filteredProducts() { return this.products; },' + |
| 203 |
' addToCart(p) { this.cartItems++; }' + |
| 204 |
'}">' + |
| 205 |
' <span id="cart" x-text="cartItems"></span>' + |
| 206 |
' <span id="cat" x-text="selectedCategory"></span>' + |
| 207 |
'</div>'; |
| 208 |
}); |
| 209 |
|
| 210 |
assert.equal(elt.querySelector('#cart').textContent, '3'); |
| 211 |
assert.equal(elt.querySelector('#cat').textContent, 'all'); |
| 212 |
}); |
| 213 |
|
| 214 |
it('teleport target is empty after destroyTree runs before snapshot', async function () { |
| 215 |
let cachedPath = location.pathname + location.search; |
| 216 |
createProcessedHTML(` |
| 217 |
<div id="tp-target3"></div> |
| 218 |
<div hx-history-elt></div> |
| 219 |
<button hx-get="/page2" hx-push-url="/page2">go</button> |
| 220 |
`); |
| 221 |
historyElt = playground().querySelector('[hx-history-elt]'); |
| 222 |
historyElt.innerHTML = |
| 223 |
'<div x-data>' + |
| 224 |
' <template x-teleport="#tp-target3"><div>content</div></template>' + |
| 225 |
'</div>'; |
| 226 |
htmx.process(historyElt); |
| 227 |
await htmx.timeout(50); |
| 228 |
|
| 229 |
let childCountAtSnapshot = null; |
| 230 |
document.addEventListener('htmx:history:cache:before:save', () => { |
| 231 |
childCountAtSnapshot = playground().querySelector('#tp-target3').children.length; |
| 232 |
}, { once: true }); |
| 233 |
|
| 234 |
let savedState; |
| 235 |
document.addEventListener('htmx:history:cache:after:save', () => { |
| 236 |
savedState = { ...history.state }; |
| 237 |
}, { once: true }); |
| 238 |
|
| 239 |
mockResponse('GET', '/page2', '<p>page 2</p>'); |
| 240 |
playground().querySelector('button').click(); |
| 241 |
await forRequest(); |
| 242 |
|
| 243 |
assert.equal(childCountAtSnapshot, 0); |
| 244 |
}); |
| 245 |
|
| 246 |
}); |