| 1 |
describe('hx-boost attribute', async function() { |
| 2 |
|
| 3 |
beforeEach(() => { |
| 4 |
setupTest(this.currentTest) |
| 5 |
}) |
| 6 |
|
| 7 |
afterEach(() => { |
| 8 |
cleanupTest() |
| 9 |
}) |
| 10 |
|
| 11 |
it('handles basic anchor properly', async function () { |
| 12 |
mockResponse('GET', '/test', 'Boosted') |
| 13 |
createProcessedHTML('<div hx-target:inherited="this" hx-swap:inherited="outerHTML" hx-boost:inherited="true"><a id="a1" href="/test">Foo</a></div>') |
| 14 |
find('#a1').click() |
| 15 |
await forRequest() |
| 16 |
playground().innerHTML.should.equal('Boosted') |
| 17 |
}) |
| 18 |
|
| 19 |
it('handles basic form post properly', async function () { |
| 20 |
mockResponse('POST', '/test', 'Boosted') |
| 21 |
createProcessedHTML('<div hx-target:inherited="this" hx-swap:inherited="outerHTML" hx-boost:inherited="true"><form id="f1" action="/test" method="post"><button id="b1">Submit</button></form></div>') |
| 22 |
find('#b1').click() |
| 23 |
await forRequest() |
| 24 |
playground().innerHTML.should.equal('Boosted') |
| 25 |
}) |
| 26 |
|
| 27 |
it('handles basic form post with button formaction properly', async function () { |
| 28 |
mockResponse('POST', '/test', 'Boosted') |
| 29 |
createProcessedHTML('<div hx-target:inherited="this" hx-swap:inherited="outerHTML" hx-boost:inherited="true"><form id="f1" action="/bad" method="post"><button id="b1" formaction="/test">Submit</button></form></div>') |
| 30 |
find('#b1').click() |
| 31 |
await forRequest() |
| 32 |
playground().innerHTML.should.equal('Boosted') |
| 33 |
}) |
| 34 |
|
| 35 |
it('handles basic form post with button formmethod properly', async function() { |
| 36 |
mockResponse('POST', '/test', 'Boosted') |
| 37 |
createProcessedHTML('<div hx-target:inherited="this" hx-swap:inherited="outerHTML" hx-boost:inherited="true"><form id="f1" action="/test" method="get"><button id="b1" formmethod="post">Submit</button></form></div>') |
| 38 |
find('#b1').click() |
| 39 |
await forRequest() |
| 40 |
playground().innerHTML.should.equal('Boosted') |
| 41 |
}) |
| 42 |
|
| 43 |
it('handles basic form post with button formmethod & formaction properly', async function() { |
| 44 |
mockResponse('POST', '/test', 'Boosted') |
| 45 |
createProcessedHTML('<div hx-target:inherited="this" hx-swap:inherited="outerHTML" hx-boost:inherited="true"><form id="f1" action="/bad" method="get"><button id="b1" formmethod="post" formaction="/test">Submit</button></form></div>') |
| 46 |
find('#b1').click() |
| 47 |
await forRequest() |
| 48 |
playground().innerHTML.should.equal('Boosted') |
| 49 |
}) |
| 50 |
|
| 51 |
it('handles basic form post properly w/ explicit action', async function() { |
| 52 |
mockResponse('POST', '/test', 'Boosted') |
| 53 |
createProcessedHTML('<div hx-target:inherited="this" hx-swap:inherited="outerHTML"><form id="f1" action="/test" method="post" hx-boost="true"></form></div>') |
| 54 |
find('#f1').requestSubmit() |
| 55 |
await forRequest() |
| 56 |
playground().innerHTML.should.equal('Boosted') |
| 57 |
}) |
| 58 |
|
| 59 |
it('handles basic form get properly', async function() { |
| 60 |
debug(this) |
| 61 |
mockResponse('GET', '/test', 'Boosted') |
| 62 |
createProcessedHTML('<div hx-target:inherited="this" hx-swap:inherited="outerHTML" hx-boost:inherited="true"><form id="f1" action="/test" method="get"><button id="b1">Submit</button></form></div>') |
| 63 |
find('#b1').click() |
| 64 |
await forRequest() |
| 65 |
playground().innerHTML.should.equal('Boosted') |
| 66 |
}) |
| 67 |
|
| 68 |
it('handles basic form with no explicit method property', async function() { |
| 69 |
mockResponse('GET', '/test', 'Boosted') |
| 70 |
createProcessedHTML('<div hx-target:inherited="this" hx-swap:inherited="outerHTML" hx-boost:inherited="true"><form id="f1" action="/test"><button id="b1">Submit</button></form></div>') |
| 71 |
find("#f1").getAttribute("data-htmx-powered").should.equal('true') |
| 72 |
find('#b1').click() |
| 73 |
await forRequest() |
| 74 |
playground().innerHTML.should.equal('Boosted') |
| 75 |
}) |
| 76 |
|
| 77 |
it('does not boost forms with method="dialog"', async function() { |
| 78 |
mockResponse('GET', '/test', 'Boosted') |
| 79 |
createProcessedHTML('<div hx-boost:inherited="true"><form id="f1" action="/test" method="dialog"><button id="b1">close</button></form></div>') |
| 80 |
find('#b1').click() |
| 81 |
fetchMock.calls.length.should.equal(0) |
| 82 |
}) |
| 83 |
|
| 84 |
it('does not boost forms with buttons with formmethod="dialog"', async function() { |
| 85 |
mockResponse('GET', '/test', 'Boosted') |
| 86 |
createProcessedHTML('<div hx-boost:inherited="true"><form id="f1" action="/test" method="get"><button formmethod="dialog" id="b1">close</button></form></div>') |
| 87 |
find('#b1').click() |
| 88 |
fetchMock.calls.length.should.equal(0) |
| 89 |
}) |
| 90 |
|
| 91 |
it('hx-boost with advanced syntax applies swap config', async function() { |
| 92 |
mockResponse('GET', '/test', 'Clicked') |
| 93 |
createProcessedHTML('<a hx-boost="swap:outerHTML" hx-target="this" id="a1" href="/test">Click Me</a>') |
| 94 |
find('#a1').click() |
| 95 |
await forRequest() |
| 96 |
should.equal(document.querySelector('#a1'), null) |
| 97 |
}) |
| 98 |
|
| 99 |
it('hx-boost with advanced syntax applies target config', async function() { |
| 100 |
mockResponse('GET', '/test', 'New Content') |
| 101 |
createProcessedHTML('<div id="target">Old</div><a hx-boost="target:#target" id="a1" href="/test">Click</a>') |
| 102 |
find('#a1').click() |
| 103 |
await forRequest() |
| 104 |
find('#target').innerHTML.should.equal('New Content') |
| 105 |
}) |
| 106 |
|
| 107 |
it('hx-boost with advanced syntax applies select config', async function() { |
| 108 |
mockResponse('GET', '/test', '<div><span id="keep">Keep</span><span>Ignore</span></div>') |
| 109 |
createProcessedHTML('<div id="result"></div><a hx-boost="select:#keep" hx-target="#result" id="a1" href="/test">Click</a>') |
| 110 |
find('#a1').click() |
| 111 |
await forRequest() |
| 112 |
find('#result').innerHTML.should.contain('Keep') |
| 113 |
find('#result').innerHTML.should.not.contain('Ignore') |
| 114 |
}) |
| 115 |
|
| 116 |
it('hx-boost with multiple advanced configs', async function() { |
| 117 |
mockResponse('GET', '/test', '<div id="main"><span id="result">Success</span></div>') |
| 118 |
createProcessedHTML('<div id="main">Old</div><a hx-boost="swap:outerHTML target:#main select:#result" id="a1" href="/test">Click</a>') |
| 119 |
find('#a1').click() |
| 120 |
await forRequest() |
| 121 |
should.equal(document.querySelector('#main'), null) |
| 122 |
find('#result').innerHTML.should.equal('Success') |
| 123 |
}) |
| 124 |
|
| 125 |
it('boost config overrides explicit attributes', async function() { |
| 126 |
mockResponse('GET', '/test', 'Clicked') |
| 127 |
createProcessedHTML('<a hx-boost="swap:outerHTML" hx-swap="innerHTML" hx-target="this" id="a1" href="/test">Click</a>') |
| 128 |
find('#a1').click() |
| 129 |
await forRequest() |
| 130 |
should.equal(document.querySelector('#a1'), null) |
| 131 |
}) |
| 132 |
|
| 133 |
it('hx-boost true still works as before', async function() { |
| 134 |
mockResponse('GET', '/test', 'Clicked') |
| 135 |
createProcessedHTML('<div id="result"></div><a hx-boost="true" hx-target="#result" id="a1" href="/test">Click Me</a>') |
| 136 |
find('#a1').click() |
| 137 |
await forRequest() |
| 138 |
find('#result').innerHTML.should.equal('Clicked') |
| 139 |
}) |
| 140 |
|
| 141 |
it('hx-boost with hx-push-url=false does not push history', async function() { |
| 142 |
let pushCount = 0 |
| 143 |
let originalPushState = history.pushState.bind(history) |
| 144 |
history.pushState = (...args) => { pushCount++; originalPushState(...args) } |
| 145 |
try { |
| 146 |
mockResponse('GET', '/test', 'Clicked') |
| 147 |
createProcessedHTML('<div id="result"></div><a hx-boost="true" hx-push-url="false" hx-target="#result" id="a1" href="/test">Click Me</a>') |
| 148 |
find('#a1').click() |
| 149 |
await forRequest() |
| 150 |
pushCount.should.equal(0) |
| 151 |
} finally { |
| 152 |
history.pushState = originalPushState |
| 153 |
} |
| 154 |
}) |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
await forRequest() |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
await forRequest() |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
await forRequest() |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
await forRequest() |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
await forRequest() |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
await forRequest() |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
}) |