htmx/test/attributes/hx-replace-url.js
MichaelWest22 24a0106f76
Update testing framework to web-test-runner and improve code coverage (#3273)
* Fix old npm dependencies

* implement web-test-runner tests for headless alongside Mocha browser tests

* Increase test and code coverage

* update to 100% coverage and impove eslint

* Update testing Doco

* revert all htmx changes and updates/disable tests needed

* fix browser mocha test

* Default testing to use playwrite only instead of puppeter

* playwright install fix

* Imporve test summary reporting

* flatten false looks closer to original
2025-04-17 17:55:43 -06:00

44 lines
1.5 KiB
JavaScript

describe('hx-replace-url attribute', function() {
var HTMX_HISTORY_CACHE_NAME = 'htmx-history-cache'
beforeEach(function() {
this.server = makeServer()
clearWorkArea()
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
})
afterEach(function() {
this.server.restore()
clearWorkArea()
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
})
it('navigation should replace an element into the cache when true', function() {
this.server.respondWith('GET', '/test', 'second')
getWorkArea().innerHTML.should.be.equal('')
var div = make('<div hx-replace-url="true" hx-get="/test">first</div>')
div.click()
this.server.respond()
div.click()
this.server.respond()
getWorkArea().textContent.should.equal('second')
var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME))
cache[cache.length - 1].url.should.equal('/test')
})
it('should handle HX-Replace-Url response header', function() {
var path
var handler = htmx.on('htmx:replacedInHistory', function(event) {
path = event.detail.path
})
this.server.respondWith('GET', '/test', [200, { 'HX-Replace-Url': '/pushpath' }, 'Result'])
var div1 = make('<div id="d1" hx-get="/test"></div>')
div1.click()
this.server.respond()
div1.innerHTML.should.equal('Result')
var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME))
cache.length.should.equal(1)
path.should.equal('/pushpath')
htmx.off('htmx:replacedInHistory', handler)
})
})