htmx/test/attributes/hx-boost.js
youssame 3e265ea263
Fix TypeError on null path variable (#2967)
* Fix the error

* add tests
2024-10-20 16:37:58 -06:00

173 lines
6.4 KiB
JavaScript

describe('hx-boost attribute', function() {
beforeEach(function() {
this.server = makeServer()
clearWorkArea()
})
afterEach(function() {
this.server.restore()
clearWorkArea()
})
it('handles basic anchor properly', function() {
this.server.respondWith('GET', '/test', 'Boosted')
var div = make('<div hx-target="this" hx-boost="true"><a id="a1" href="/test">Foo</a></div>')
var a = byId('a1')
a.click()
this.server.respond()
div.innerHTML.should.equal('Boosted')
})
it('handles basic form post properly', function() {
this.server.respondWith('POST', '/test', 'Boosted')
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test" method="post"><button id="b1">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted')
})
it('handles basic form post with button formaction properly', function() {
this.server.respondWith('POST', '/test', 'Boosted')
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" method="post"><button id="b1" formaction="/test">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted')
})
it('handles basic form post with button formmethod properly', function() {
this.server.respondWith('POST', '/test', 'Boosted')
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test"><button id="b1" formmethod="post">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted')
})
it('handles basic form post properly w/ explicit action', function() {
this.server.respondWith('POST', '/test', 'Boosted')
var div = make('<div hx-target="this"><form id="f1" action="/test" method="post" hx-trigger="click" hx-boost="true"></form></div>')
var form = byId('f1')
form.click()
this.server.respond()
div.innerHTML.should.equal('Boosted')
})
it('handles basic form get properly', function() {
this.server.respondWith('GET', '/test', 'Boosted')
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test" method="get"><button id="b1">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted')
})
it('handles basic form with no explicit method property', function() {
this.server.respondWith('GET', '/test', 'Boosted')
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test"><button id="b1">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted')
})
it('does not boost forms with method="dialog"', function() {
make('<div hx-boost="true"><form id="f1" action="/test" method="dialog"><button id="b1">close</button></form></div>')
var form = byId('f1')
var internalData = htmx._('getInternalData')(form)
should.equal(undefined, internalData.boosted)
})
it('handles basic anchor properly w/ data-* prefix', function() {
this.server.respondWith('GET', '/test', 'Boosted')
var div = make('<div data-hx-target="this" data-hx-boost="true"><a id="a1" href="/test">Foo</a></div>')
var a = byId('a1')
a.click()
this.server.respond()
div.innerHTML.should.equal('Boosted')
})
it('overriding default swap style does not effect boosting', function() {
htmx.config.defaultSwapStyle = 'afterend'
try {
this.server.respondWith('GET', '/test', 'Boosted')
var a = make('<a hx-target="this" hx-boost="true" id="a1" href="/test">Foo</a>')
a.click()
this.server.respond()
a.innerHTML.should.equal('Boosted')
} finally {
htmx.config.defaultSwapStyle = 'innerHTML'
}
})
it('anchors w/ explicit targets are not boosted', function() {
var a = make('<a hx-target="this" hx-boost="true" id="a1" href="/test" target="_blank">Foo</a>')
var internalData = htmx._('getInternalData')(a)
should.equal(undefined, internalData.boosted)
})
it('includes an HX-Boosted Header', function() {
this.server.respondWith('GET', '/test', function(xhr) {
should.equal(xhr.requestHeaders['HX-Boosted'], 'true')
xhr.respond(200, {}, 'Boosted!')
})
var btn = make('<a hx-boost="true" hx-target="this" href="/test">Click Me!</a>')
btn.click()
this.server.respond()
btn.innerHTML.should.equal('Boosted!')
})
it('form get w/ search params in action property excludes search params', function() {
this.server.respondWith('GET', /\/test.*/, function(xhr) {
should.equal(undefined, getParameters(xhr).foo)
xhr.respond(200, {}, 'Boosted!')
})
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test?foo=bar" method="get"><button id="b1">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted!')
})
it('form post w/ query params in action property uses full url', function() {
this.server.respondWith('POST', /\/test.*/, function(xhr) {
should.equal(undefined, getParameters(xhr).foo)
xhr.respond(200, {}, 'Boosted!')
})
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test?foo=bar" method="post"><button id="b1">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted!')
})
it('form get with an unset action property', function() {
this.server.respondWith('GET', /\/*/, function(xhr) {
should.equal(undefined, getParameters(xhr).foo)
xhr.respond(200, {}, 'Boosted!')
})
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" method="get"><button id="b1">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted!')
})
it('form get with an empty action property', function() {
this.server.respondWith('GET', /\/*/, function(xhr) {
should.equal(undefined, getParameters(xhr).foo)
xhr.respond(200, {}, 'Boosted!')
})
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="" method="get"><button id="b1">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted!')
})
})