| 1 |
describe('hx-push-url attribute', function() { |
| 2 |
const chai = window.chai |
| 3 |
var HTMX_HISTORY_CACHE_NAME = 'htmx-history-cache' |
| 4 |
|
| 5 |
beforeEach(function() { |
| 6 |
this.server = makeServer() |
| 7 |
clearWorkArea() |
| 8 |
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) |
| 9 |
}) |
| 10 |
afterEach(function() { |
| 11 |
this.server.restore() |
| 12 |
clearWorkArea() |
| 13 |
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) |
| 14 |
}) |
| 15 |
|
| 16 |
it('navigation should push an element into the cache when true', function() { |
| 17 |
this.server.respondWith('GET', '/test', 'second') |
| 18 |
getWorkArea().innerHTML.should.be.equal('') |
| 19 |
var div = make('<div hx-push-url="true" hx-get="/test">first</div>') |
| 20 |
div.click() |
| 21 |
this.server.respond() |
| 22 |
div.click() |
| 23 |
this.server.respond() |
| 24 |
getWorkArea().textContent.should.equal('second') |
| 25 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 26 |
cache[cache.length - 1].url.should.equal('/test') |
| 27 |
}) |
| 28 |
|
| 29 |
it('navigation should not push an element into the cache when false', function() { |
| 30 |
this.server.respondWith('GET', '/test', 'second') |
| 31 |
getWorkArea().innerHTML.should.be.equal('') |
| 32 |
var div = make('<div hx-push-url="false" hx-get="/test">first</div>') |
| 33 |
div.click() |
| 34 |
this.server.respond() |
| 35 |
div.click() |
| 36 |
this.server.respond() |
| 37 |
getWorkArea().textContent.should.equal('second') |
| 38 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 39 |
should.equal(cache, null) |
| 40 |
}) |
| 41 |
|
| 42 |
it('navigation should push an element into the cache when string', function() { |
| 43 |
this.server.respondWith('GET', '/test', 'second') |
| 44 |
getWorkArea().innerHTML.should.be.equal('') |
| 45 |
var div = make('<div hx-push-url="abc123" hx-get="/test">first</div>') |
| 46 |
div.click() |
| 47 |
this.server.respond() |
| 48 |
div.click() |
| 49 |
this.server.respond() |
| 50 |
getWorkArea().textContent.should.equal('second') |
| 51 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 52 |
cache.length.should.equal(2) |
| 53 |
cache[1].url.should.equal('/abc123') |
| 54 |
}) |
| 55 |
|
| 56 |
it('restore should return old value', function() { |
| 57 |
this.server.respondWith('GET', '/test1', '<div id="d2" hx-push-url="true" hx-get="/test2" hx-swap="outerHTML settle:0">test1</div>') |
| 58 |
this.server.respondWith('GET', '/test2', '<div id="d3" hx-push-url="true" hx-get="/test3" hx-swap="outerHTML settle:0">test2</div>') |
| 59 |
|
| 60 |
make('<div id="d1" hx-push-url="true" hx-get="/test1" hx-swap="outerHTML settle:0">init</div>') |
| 61 |
|
| 62 |
byId('d1').click() |
| 63 |
this.server.respond() |
| 64 |
var workArea = getWorkArea() |
| 65 |
workArea.textContent.should.equal('test1') |
| 66 |
|
| 67 |
byId('d2').click() |
| 68 |
this.server.respond() |
| 69 |
workArea.textContent.should.equal('test2') |
| 70 |
|
| 71 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 72 |
|
| 73 |
cache.length.should.equal(2) |
| 74 |
htmx._('restoreHistory')('/test1') |
| 75 |
getWorkArea().textContent.should.equal('test1') |
| 76 |
}) |
| 77 |
|
| 78 |
it('history restore should not have htmx support classes in content', function() { |
| 79 |
this.server.respondWith('GET', '/test1', '<div id="d2" hx-push-url="true" hx-get="/test2" hx-swap="outerHTML settle:0">test1</div>') |
| 80 |
this.server.respondWith('GET', '/test2', '<div id="d3" hx-push-url="true" hx-get="/test3" hx-swap="outerHTML settle:0">test2</div>') |
| 81 |
|
| 82 |
make('<div id="d1" hx-push-url="true" hx-get="/test1" hx-swap="outerHTML settle:0">init</div>') |
| 83 |
|
| 84 |
byId('d1').click() |
| 85 |
this.server.respond() |
| 86 |
var workArea = getWorkArea() |
| 87 |
workArea.textContent.should.equal('test1') |
| 88 |
|
| 89 |
byId('d2').click() |
| 90 |
this.server.respond() |
| 91 |
workArea.textContent.should.equal('test2') |
| 92 |
|
| 93 |
htmx._('restoreHistory')('/test1') |
| 94 |
getWorkArea().getElementsByClassName('htmx-request').length.should.equal(0) |
| 95 |
}) |
| 96 |
|
| 97 |
it('cache should only store 10 entries', function() { |
| 98 |
var x = 0 |
| 99 |
this.server.respondWith('GET', /test.*/, function(xhr) { |
| 100 |
x++ |
| 101 |
xhr.respond(200, {}, '<div id="d1" hx-push-url="true" hx-get="/test' + x + '" hx-swap="outerHTML settle:0"></div>') |
| 102 |
}) |
| 103 |
getWorkArea().innerHTML.should.be.equal('') |
| 104 |
make('<div id="d1" hx-push-url="true" hx-get="/test" hx-swap="outerHTML settle:0"></div>') |
| 105 |
for (var i = 0; i < 20; i++) { |
| 106 |
byId('d1').click() |
| 107 |
this.server.respond() |
| 108 |
} |
| 109 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 110 |
cache.length.should.equal(10) |
| 111 |
}) |
| 112 |
|
| 113 |
it('cache miss should issue another GET', function() { |
| 114 |
this.server.respondWith('GET', '/test1', '<div id="d2" hx-push-url="true" hx-get="/test2" hx-swap="outerHTML settle:0">test1</div>') |
| 115 |
this.server.respondWith('GET', '/test2', '<div id="d3" hx-push-url="true" hx-get="/test3" hx-swap="outerHTML settle:0">test2</div>') |
| 116 |
|
| 117 |
make('<div id="d1" hx-push-url="true" hx-get="/test1" hx-swap="outerHTML settle:0">init</div>') |
| 118 |
|
| 119 |
byId('d1').click() |
| 120 |
this.server.respond() |
| 121 |
var workArea = getWorkArea() |
| 122 |
workArea.textContent.should.equal('test1') |
| 123 |
|
| 124 |
byId('d2').click() |
| 125 |
this.server.respond() |
| 126 |
workArea.textContent.should.equal('test2') |
| 127 |
|
| 128 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 129 |
|
| 130 |
cache.length.should.equal(2) |
| 131 |
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) |
| 132 |
htmx._('restoreHistory')('/test1') |
| 133 |
this.server.respond() |
| 134 |
getWorkArea().textContent.should.equal('test1') |
| 135 |
}) |
| 136 |
|
| 137 |
it('cache miss should refresh when refreshOnHistoryMiss true', function() { |
| 138 |
htmx.config.refreshOnHistoryMiss = true |
| 139 |
var refresh = false |
| 140 |
htmx.location = { reload: function() { refresh = true } } |
| 141 |
sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) |
| 142 |
htmx._('restoreHistory')('/test3') |
| 143 |
refresh.should.equal(true) |
| 144 |
htmx.location = window.location |
| 145 |
htmx.config.refreshOnHistoryMiss = false |
| 146 |
}) |
| 147 |
|
| 148 |
it('navigation should push an element into the cache w/ data-* prefix', function() { |
| 149 |
this.server.respondWith('GET', '/test', 'second') |
| 150 |
getWorkArea().innerHTML.should.be.equal('') |
| 151 |
var div = make('<div data-hx-push-url="true" data-hx-get="/test">first</div>') |
| 152 |
div.click() |
| 153 |
this.server.respond() |
| 154 |
getWorkArea().textContent.should.equal('second') |
| 155 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 156 |
cache.length.should.equal(1) |
| 157 |
}) |
| 158 |
|
| 159 |
it('deals with malformed JSON in history cache when getting', function() { |
| 160 |
sessionStorage.setItem(HTMX_HISTORY_CACHE_NAME, 'Invalid JSON') |
| 161 |
var history = htmx._('getCachedHistory')('url') |
| 162 |
should.equal(history, null) |
| 163 |
}) |
| 164 |
|
| 165 |
it('deals with malformed JSON in history cache when saving', function() { |
| 166 |
sessionStorage.setItem(HTMX_HISTORY_CACHE_NAME, 'Invalid JSON') |
| 167 |
htmx._('saveToHistoryCache')('url', make('<div>')) |
| 168 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 169 |
cache.length.should.equal(1) |
| 170 |
}) |
| 171 |
|
| 172 |
it('does not blow out cache when saving a URL twice', function() { |
| 173 |
htmx._('saveToHistoryCache')('url1', make('<div>')) |
| 174 |
htmx._('saveToHistoryCache')('url2', make('<div>')) |
| 175 |
htmx._('saveToHistoryCache')('url3', make('<div>')) |
| 176 |
htmx._('saveToHistoryCache')('url2', make('<div>')) |
| 177 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 178 |
cache.length.should.equal(3) |
| 179 |
}) |
| 180 |
|
| 181 |
it('setting history cache size to 0 clears cache', function() { |
| 182 |
htmx._('saveToHistoryCache')('url1', make('<div>')) |
| 183 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 184 |
cache.length.should.equal(1) |
| 185 |
htmx.config.historyCacheSize = 0 |
| 186 |
htmx._('saveToHistoryCache')('url2', make('<div>')) |
| 187 |
cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 188 |
should.equal(cache, null) |
| 189 |
htmx.config.historyCacheSize = 10 |
| 190 |
}) |
| 191 |
|
| 192 |
it('history cache is LRU', function() { |
| 193 |
htmx._('saveToHistoryCache')('url1', make('<div>')) |
| 194 |
htmx._('saveToHistoryCache')('url2', make('<div>')) |
| 195 |
htmx._('saveToHistoryCache')('url3', make('<div>')) |
| 196 |
htmx._('saveToHistoryCache')('url2', make('<div>')) |
| 197 |
htmx._('saveToHistoryCache')('url1', make('<div>')) |
| 198 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 199 |
cache.length.should.equal(3) |
| 200 |
cache[0].url.should.equal('/url3') |
| 201 |
cache[1].url.should.equal('/url2') |
| 202 |
cache[2].url.should.equal('/url1') |
| 203 |
}) |
| 204 |
|
| 205 |
it('htmx:afterSettle is called when replacing outerHTML', function() { |
| 206 |
var called = false |
| 207 |
var handler = htmx.on('htmx:afterSettle', function(evt) { |
| 208 |
called = true |
| 209 |
}) |
| 210 |
try { |
| 211 |
this.server.respondWith('POST', '/test', function(xhr) { |
| 212 |
xhr.respond(200, {}, '<button>Bar</button>') |
| 213 |
}) |
| 214 |
var div = make("<button hx-post='/test' hx-swap='outerHTML'>Foo</button>") |
| 215 |
div.click() |
| 216 |
this.server.respond() |
| 217 |
should.equal(called, true) |
| 218 |
} finally { |
| 219 |
htmx.off('htmx:afterSettle', handler) |
| 220 |
} |
| 221 |
}) |
| 222 |
|
| 223 |
it('should include parameters on a get', function() { |
| 224 |
var path = '' |
| 225 |
var handler = htmx.on('htmx:pushedIntoHistory', function(evt) { |
| 226 |
path = evt.detail.path |
| 227 |
}) |
| 228 |
try { |
| 229 |
this.server.respondWith('GET', /test.*/, function(xhr) { |
| 230 |
xhr.respond(200, {}, 'second') |
| 231 |
}) |
| 232 |
var form = make('<form hx-trigger="click" hx-push-url="true" hx-get="/test"><input type="hidden" name="foo" value="bar"/>first</form>') |
| 233 |
form.click() |
| 234 |
this.server.respond() |
| 235 |
form.textContent.should.equal('second') |
| 236 |
path.should.equal('/test?foo=bar') |
| 237 |
} finally { |
| 238 |
htmx.off('htmx:pushedIntoHistory', handler) |
| 239 |
} |
| 240 |
}) |
| 241 |
|
| 242 |
it('saveToHistoryCache should not throw', function() { |
| 243 |
this.timeout(4000) |
| 244 |
var bigContent = 'Dummy' |
| 245 |
for (var i = 0; i < 20; i++) { |
| 246 |
bigContent += bigContent |
| 247 |
} |
| 248 |
try { |
| 249 |
sessionStorage.removeItem('htmx-history-cache') |
| 250 |
htmx._('saveToHistoryCache')('/dummy', make('<div>' + bigContent + '</div>'), 'Foo', 0) |
| 251 |
should.equal(sessionStorage.getItem('htmx-history-cache'), null) |
| 252 |
} finally { |
| 253 |
|
| 254 |
sessionStorage.removeItem('htmx-history-cache') |
| 255 |
} |
| 256 |
}) |
| 257 |
|
| 258 |
if (/chrome/i.test(navigator.userAgent)) { |
| 259 |
it('when sessionStorage disabled history not saved fine', function() { |
| 260 |
var setItem = sessionStorage.setItem |
| 261 |
sessionStorage.setItem = undefined |
| 262 |
this.server.respondWith('GET', '/test', 'second') |
| 263 |
getWorkArea().innerHTML.should.be.equal('') |
| 264 |
var div = make('<div hx-push-url="true" hx-get="/test">first</div>') |
| 265 |
div.click() |
| 266 |
this.server.respond() |
| 267 |
div.click() |
| 268 |
this.server.respond() |
| 269 |
getWorkArea().textContent.should.equal('second') |
| 270 |
var hist = htmx._('getCachedHistory')('/test') |
| 271 |
should.equal(hist, null) |
| 272 |
sessionStorage.setItem = setItem |
| 273 |
}) |
| 274 |
} |
| 275 |
|
| 276 |
it.skip('normalizePath falls back to no normalization if path not valid URL', function() { |
| 277 |
|
| 278 |
htmx._('saveToHistoryCache')('http://', make('<div>')) |
| 279 |
htmx._('saveToHistoryCache')('http//', make('<div>')) |
| 280 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 281 |
cache.length.should.equal(2) |
| 282 |
cache[0].url.should.equal('http://') |
| 283 |
cache[1].url.should.equal('/http') |
| 284 |
}) |
| 285 |
|
| 286 |
it('history cache clears out disabled attribute', function() { |
| 287 |
htmx._('saveToHistoryCache')('/url1', make('<div><div data-disabled-by-htmx disabled></div></div>')) |
| 288 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 289 |
cache.length.should.equal(1) |
| 290 |
cache[0].url.should.equal('/url1') |
| 291 |
cache[0].content.should.equal('<div data-disabled-by-htmx=""></div>') |
| 292 |
}) |
| 293 |
|
| 294 |
it('ensure cache-busting parameter not pushed to history url', function() { |
| 295 |
this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 296 |
getParameters(xhr)['org.htmx.cache-buster'].should.equal('foo') |
| 297 |
xhr.respond(200, {}, 'Clicked!') |
| 298 |
}) |
| 299 |
|
| 300 |
try { |
| 301 |
htmx.config.getCacheBusterParam = true |
| 302 |
var btn = make('<button hx-push-url="true" hx-get="/test" id="foo">Click Me!</button>') |
| 303 |
btn.click() |
| 304 |
this.server.respond() |
| 305 |
btn.innerHTML.should.equal('Clicked!') |
| 306 |
} finally { |
| 307 |
htmx.config.getCacheBusterParam = false |
| 308 |
} |
| 309 |
sessionStorage.getItem('htmx-current-path-for-history').should.equal('/test') |
| 310 |
}) |
| 311 |
|
| 312 |
it('ensure history pushState called', function() { |
| 313 |
if (!byId('mocha')) { |
| 314 |
this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 315 |
xhr.respond(200, {}, 'Clicked!') |
| 316 |
}) |
| 317 |
|
| 318 |
try { |
| 319 |
htmx.config.historyEnabled = true |
| 320 |
var btn = make('<button hx-push-url="true" hx-get="/test" id="foo">Click Me!</button>') |
| 321 |
btn.click() |
| 322 |
this.server.respond() |
| 323 |
btn.innerHTML.should.equal('Clicked!') |
| 324 |
} finally { |
| 325 |
htmx.config.historyEnabled = false |
| 326 |
} |
| 327 |
} |
| 328 |
}) |
| 329 |
|
| 330 |
it('should handle HX-Push response header', function() { |
| 331 |
var path |
| 332 |
var handler = htmx.on('htmx:pushedIntoHistory', function(event) { |
| 333 |
path = event.detail.path |
| 334 |
}) |
| 335 |
this.server.respondWith('GET', '/test', [200, { 'HX-Push': '/pushpath' }, 'Result']) |
| 336 |
var div1 = make('<div id="d1" hx-get="/test"></div>') |
| 337 |
div1.click() |
| 338 |
this.server.respond() |
| 339 |
div1.innerHTML.should.equal('Result') |
| 340 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 341 |
cache.length.should.equal(1) |
| 342 |
path.should.equal('/pushpath') |
| 343 |
htmx.off('htmx:pushedIntoHistory', handler) |
| 344 |
}) |
| 345 |
|
| 346 |
it('should handle HX-Push-Url response header', function() { |
| 347 |
var path |
| 348 |
var handler = htmx.on('htmx:pushedIntoHistory', function(event) { |
| 349 |
path = event.detail.path |
| 350 |
}) |
| 351 |
this.server.respondWith('GET', '/test', [200, { 'HX-Push-Url': '/pushpath' }, 'Result']) |
| 352 |
var div1 = make('<div id="d1" hx-get="/test"></div>') |
| 353 |
div1.click() |
| 354 |
this.server.respond() |
| 355 |
div1.innerHTML.should.equal('Result') |
| 356 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 357 |
cache.length.should.equal(1) |
| 358 |
path.should.equal('/pushpath') |
| 359 |
htmx.off('htmx:pushedIntoHistory', handler) |
| 360 |
}) |
| 361 |
|
| 362 |
it('should ignore HX-Push-Url=false response header', function() { |
| 363 |
var path = '' |
| 364 |
var handler = htmx.on('htmx:pushedIntoHistory', function(event) { |
| 365 |
path = event.detail.path |
| 366 |
}) |
| 367 |
this.server.respondWith('GET', '/test', [200, { 'HX-Push-Url': 'false' }, 'Result']) |
| 368 |
var div1 = make('<div id="d1" hx-get="/test"></div>') |
| 369 |
div1.click() |
| 370 |
this.server.respond() |
| 371 |
div1.innerHTML.should.equal('Result') |
| 372 |
var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME)) |
| 373 |
should.equal(cache, null) |
| 374 |
path.should.equal('') |
| 375 |
htmx.off('htmx:pushedIntoHistory', handler) |
| 376 |
}) |
| 377 |
|
| 378 |
it('pushing url without anchor will retain the page anchor tag', function() { |
| 379 |
var handler = htmx.on('htmx:configRequest', function(evt) { |
| 380 |
evt.detail.path = evt.detail.path + '#test' |
| 381 |
}) |
| 382 |
var path = '' |
| 383 |
var handler2 = htmx.on('htmx:pushedIntoHistory', function(evt) { |
| 384 |
path = evt.detail.path |
| 385 |
}) |
| 386 |
try { |
| 387 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 388 |
var div = make("<div hx-get='/test' hx-push-url='/test'></div>") |
| 389 |
div.click() |
| 390 |
this.server.respond() |
| 391 |
div.innerHTML.should.equal('Clicked!') |
| 392 |
path.should.equal('/test#test') |
| 393 |
} finally { |
| 394 |
htmx.off('htmx:configRequest', handler) |
| 395 |
htmx.off('htmx:pushedIntoHistory', handler2) |
| 396 |
} |
| 397 |
}) |
| 398 |
}) |