| 1 |
describe('hx-boost attribute', function() { |
| 2 |
beforeEach(function() { |
| 3 |
this.server = makeServer() |
| 4 |
clearWorkArea() |
| 5 |
}) |
| 6 |
afterEach(function() { |
| 7 |
this.server.restore() |
| 8 |
clearWorkArea() |
| 9 |
}) |
| 10 |
|
| 11 |
it('handles basic anchor properly', function() { |
| 12 |
this.server.respondWith('GET', '/test', 'Boosted') |
| 13 |
var div = make('<div hx-target="this" hx-boost="true"><a id="a1" href="/test">Foo</a></div>') |
| 14 |
var a = byId('a1') |
| 15 |
a.click() |
| 16 |
this.server.respond() |
| 17 |
div.innerHTML.should.equal('Boosted') |
| 18 |
}) |
| 19 |
|
| 20 |
it('handles basic form post properly', function() { |
| 21 |
this.server.respondWith('POST', '/test', 'Boosted') |
| 22 |
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test" method="post"><button id="b1">Submit</button></form></div>') |
| 23 |
var btn = byId('b1') |
| 24 |
btn.click() |
| 25 |
this.server.respond() |
| 26 |
div.innerHTML.should.equal('Boosted') |
| 27 |
}) |
| 28 |
|
| 29 |
it('handles basic form post with button formaction properly', function() { |
| 30 |
this.server.respondWith('POST', '/test', 'Boosted') |
| 31 |
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" method="post"><button id="b1" formaction="/test">Submit</button></form></div>') |
| 32 |
var btn = byId('b1') |
| 33 |
btn.click() |
| 34 |
this.server.respond() |
| 35 |
div.innerHTML.should.equal('Boosted') |
| 36 |
}) |
| 37 |
|
| 38 |
it('handles basic form post with button formmethod properly', function() { |
| 39 |
this.server.respondWith('POST', '/test', 'Boosted') |
| 40 |
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test"><button id="b1" formmethod="post">Submit</button></form></div>') |
| 41 |
var btn = byId('b1') |
| 42 |
btn.click() |
| 43 |
this.server.respond() |
| 44 |
div.innerHTML.should.equal('Boosted') |
| 45 |
}) |
| 46 |
|
| 47 |
it('handles basic form post properly w/ explicit action', function() { |
| 48 |
this.server.respondWith('POST', '/test', 'Boosted') |
| 49 |
var div = make('<div hx-target="this"><form id="f1" action="/test" method="post" hx-trigger="click" hx-boost="true"></form></div>') |
| 50 |
var form = byId('f1') |
| 51 |
form.click() |
| 52 |
this.server.respond() |
| 53 |
div.innerHTML.should.equal('Boosted') |
| 54 |
}) |
| 55 |
|
| 56 |
it('handles basic form get properly', function() { |
| 57 |
this.server.respondWith('GET', '/test', 'Boosted') |
| 58 |
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test" method="get"><button id="b1">Submit</button></form></div>') |
| 59 |
var btn = byId('b1') |
| 60 |
btn.click() |
| 61 |
this.server.respond() |
| 62 |
div.innerHTML.should.equal('Boosted') |
| 63 |
}) |
| 64 |
|
| 65 |
it('handles basic form with no explicit method property', function() { |
| 66 |
this.server.respondWith('GET', '/test', 'Boosted') |
| 67 |
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="/test"><button id="b1">Submit</button></form></div>') |
| 68 |
var btn = byId('b1') |
| 69 |
btn.click() |
| 70 |
this.server.respond() |
| 71 |
div.innerHTML.should.equal('Boosted') |
| 72 |
}) |
| 73 |
|
| 74 |
it('does not boost forms with method="dialog"', function() { |
| 75 |
make('<div hx-boost="true"><form id="f1" action="/test" method="dialog"><button id="b1">close</button></form></div>') |
| 76 |
var form = byId('f1') |
| 77 |
|
| 78 |
var internalData = htmx._('getInternalData')(form) |
| 79 |
should.equal(undefined, internalData.boosted) |
| 80 |
}) |
| 81 |
|
| 82 |
it('handles basic anchor properly w/ data-* prefix', function() { |
| 83 |
this.server.respondWith('GET', '/test', 'Boosted') |
| 84 |
var div = make('<div data-hx-target="this" data-hx-boost="true"><a id="a1" href="/test">Foo</a></div>') |
| 85 |
var a = byId('a1') |
| 86 |
a.click() |
| 87 |
this.server.respond() |
| 88 |
div.innerHTML.should.equal('Boosted') |
| 89 |
}) |
| 90 |
|
| 91 |
it('overriding default swap style does not effect boosting', function() { |
| 92 |
htmx.config.defaultSwapStyle = 'afterend' |
| 93 |
try { |
| 94 |
this.server.respondWith('GET', '/test', 'Boosted') |
| 95 |
var a = make('<a hx-target="this" hx-boost="true" id="a1" href="/test">Foo</a>') |
| 96 |
a.click() |
| 97 |
this.server.respond() |
| 98 |
a.innerHTML.should.equal('Boosted') |
| 99 |
} finally { |
| 100 |
htmx.config.defaultSwapStyle = 'innerHTML' |
| 101 |
} |
| 102 |
}) |
| 103 |
|
| 104 |
it('anchors w/ explicit targets are not boosted', function() { |
| 105 |
var a = make('<a hx-target="this" hx-boost="true" id="a1" href="/test" target="_blank">Foo</a>') |
| 106 |
var internalData = htmx._('getInternalData')(a) |
| 107 |
should.equal(undefined, internalData.boosted) |
| 108 |
}) |
| 109 |
|
| 110 |
it('includes an HX-Boosted Header', function() { |
| 111 |
this.server.respondWith('GET', '/test', function(xhr) { |
| 112 |
should.equal(xhr.requestHeaders['HX-Boosted'], 'true') |
| 113 |
xhr.respond(200, {}, 'Boosted!') |
| 114 |
}) |
| 115 |
|
| 116 |
var btn = make('<a hx-boost="true" hx-target="this" href="/test">Click Me!</a>') |
| 117 |
btn.click() |
| 118 |
this.server.respond() |
| 119 |
btn.innerHTML.should.equal('Boosted!') |
| 120 |
}) |
| 121 |
|
| 122 |
it('form get w/ search params in action property excludes search params', function() { |
| 123 |
this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 124 |
should.equal(undefined, getParameters(xhr).foo) |
| 125 |
xhr.respond(200, {}, 'Boosted!') |
| 126 |
}) |
| 127 |
|
| 128 |
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>') |
| 129 |
var btn = byId('b1') |
| 130 |
btn.click() |
| 131 |
this.server.respond() |
| 132 |
div.innerHTML.should.equal('Boosted!') |
| 133 |
}) |
| 134 |
|
| 135 |
it('form post w/ query params in action property uses full url', function() { |
| 136 |
this.server.respondWith('POST', /\/test.*/, function(xhr) { |
| 137 |
should.equal(undefined, getParameters(xhr).foo) |
| 138 |
xhr.respond(200, {}, 'Boosted!') |
| 139 |
}) |
| 140 |
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>') |
| 141 |
var btn = byId('b1') |
| 142 |
btn.click() |
| 143 |
this.server.respond() |
| 144 |
div.innerHTML.should.equal('Boosted!') |
| 145 |
}) |
| 146 |
|
| 147 |
it('form get with an unset action properly submits', function() { |
| 148 |
this.server.respondWith('GET', /\/*/, function(xhr) { |
| 149 |
xhr.respond(200, {}, 'Boosted!') |
| 150 |
}) |
| 151 |
|
| 152 |
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" method="get"><button id="b1">Submit</button></form></div>') |
| 153 |
var btn = byId('b1') |
| 154 |
btn.click() |
| 155 |
this.server.respond() |
| 156 |
div.innerHTML.should.equal('Boosted!') |
| 157 |
}) |
| 158 |
|
| 159 |
it('form get with no action properly clears existing parameters on submit', function() { |
| 160 |
|
| 161 |
var path = location.href |
| 162 |
if (!path.includes('foo=bar')) { |
| 163 |
if (!path.includes('?')) { |
| 164 |
path += '?foo=bar' |
| 165 |
} else { |
| 166 |
path += '&foo=bar' |
| 167 |
} |
| 168 |
} |
| 169 |
history.replaceState({ htmx: true }, '', path) |
| 170 |
|
| 171 |
this.server.respondWith('GET', /\/*/, function(xhr) { |
| 172 |
|
| 173 |
should.equal(undefined, getParameters(xhr).foo) |
| 174 |
xhr.respond(200, {}, 'Boosted!') |
| 175 |
}) |
| 176 |
|
| 177 |
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" method="get"><button id="b1">Submit</button></form></div>') |
| 178 |
var btn = byId('b1') |
| 179 |
btn.click() |
| 180 |
this.server.respond() |
| 181 |
div.innerHTML.should.equal('Boosted!') |
| 182 |
}) |
| 183 |
|
| 184 |
it('form get with an empty action properly clears existing parameters on submit', function() { |
| 185 |
|
| 186 |
var path = location.href |
| 187 |
if (!path.includes('foo=bar')) { |
| 188 |
if (!path.includes('?')) { |
| 189 |
path += '?foo=bar' |
| 190 |
} else { |
| 191 |
path += '&foo=bar' |
| 192 |
} |
| 193 |
} |
| 194 |
history.replaceState({ htmx: true }, '', path) |
| 195 |
|
| 196 |
this.server.respondWith('GET', /\/*/, function(xhr) { |
| 197 |
|
| 198 |
should.equal(undefined, getParameters(xhr).foo) |
| 199 |
xhr.respond(200, {}, 'Boosted!') |
| 200 |
}) |
| 201 |
|
| 202 |
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" action="" method="get"><button id="b1">Submit</button></form></div>') |
| 203 |
var btn = byId('b1') |
| 204 |
btn.click() |
| 205 |
this.server.respond() |
| 206 |
div.innerHTML.should.equal('Boosted!') |
| 207 |
}) |
| 208 |
|
| 209 |
if (/headlesschrome/i.test(navigator.userAgent)) { |
| 210 |
it('ctrlKey mouse click does not boost', function() { |
| 211 |
|
| 212 |
this.server.respondWith('GET', '/test', 'Boosted') |
| 213 |
var div = make('<div hx-target="this" hx-boost="true"><a id="a1" href="/test">Foo</a></div>') |
| 214 |
var a = byId('a1') |
| 215 |
var evt = new MouseEvent('click', { ctrlKey: true }) |
| 216 |
a.dispatchEvent(evt) |
| 217 |
this.server.respond() |
| 218 |
div.innerHTML.should.not.equal('Boosted') |
| 219 |
}) |
| 220 |
} |
| 221 |
}) |