Debian Codegraph @

test/tests/attributes/hx-boost.js

raw symbols SCIP
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 // // it('overriding default swap style does not effect boosting', async function() {
157 // // htmx.config.defaultSwapStyle = 'afterend'
158 // // try {
159 // // mockResponse('GET', '/test', 'Boosted')
160 // // var a = make('<a hx-target:inherited="this" hx-boost="true" id="a1" href="/test">Foo</a>')
161 // // a.click()
162 // // this.server.respond()
163 // // a.innerHTML.should.equal('Boosted')
164 // // } finally {
165 // // htmx.config.defaultSwapStyle = 'innerHTML'
166 // // }
167 // // })
168 //
169 // it('anchors w/ explicit targets are not boosted', async function() {
170 // mockResponse('GET', '/test', 'Boosted')
171 // initHTML('<a hx-target="this" hx-boost="true" id="a1" href="/test" target="_blank">Foo</a>')
172 // find('#a1').click()
173 // fetchMock.calls.length.should.equal(0)
174 // })
175
176 // it('includes an HX-Boosted Header', async function() {
177 // mockResponse('GET', '/test', "Boosted!")
178 // createProcessedHTML('<a hx-boost="true" hx-target="this" hx-swap="outerHTML" href="/test">Click Me!</a>')
179 // find("a").click()
180 await forRequest()
181 // fetchMock.getLastCall().request.headers["HX-Boosted"].should.equal("true")
182 // fetchMock.getLastCall().request.headers["HX-Request"].should.equal("true")
183 // playground().innerHTML.should.equal('Boosted!')
184 // })
185
186 // it('form get w/ search params in action property excludes search params', async function() {
187 // mockResponse('GET', /\/test.*/, async function(xhr) {
188 // should.equal(undefined, getParameters(xhr).foo)
189 // xhr.respond(200, {}, 'Boosted!')
190 // })
191 //
192 // initHTML('<div hx-target:inherited="this" hx-boost="true"><form id="f1" action="/test?foo=bar" method="get"><button id="b1">Submit</button></form></div>')
193 // find('#b1').click()
194 await forRequest()
195 // playground().innerHTML.should.equal('Boosted!')
196 // })
197 //
198 // it('form post w/ query params in action property uses full url', async function() {
199 // mockResponse('POST', /\/test.*/, async function(xhr) {
200 // should.equal(undefined, getParameters(xhr).foo)
201 // xhr.respond(200, {}, 'Boosted!')
202 // })
203 // initHTML('<div hx-target:inherited="this" hx-boost="true"><form id="f1" action="/test?foo=bar" method="post"><button id="b1">Submit</button></form></div>')
204 // find('#b1').click()
205 await forRequest()
206 // playground().innerHTML.should.equal('Boosted!')
207 // })
208 //
209 // it('form get with an unset action properly submits', async function() {
210 // mockResponse('GET', /\/*/, async function(xhr) {
211 // xhr.respond(200, {}, 'Boosted!')
212 // })
213 //
214 // initHTML('<div hx-target:inherited="this" hx-boost="true"><form id="f1" method="get"><button id="b1">Submit</button></form></div>')
215 // find('#b1').click()
216 await forRequest()
217 // playground().innerHTML.should.equal('Boosted!')
218 // })
219 //
220 // it('form get with no action properly clears existing parameters on submit', async function() {
221 // /// add a foo=bar to the current url
222 // var path = location.href
223 // if (!path.includes('foo=bar')) {
224 // if (!path.includes('?')) {
225 // path += '?foo=bar'
226 // } else {
227 // path += '&foo=bar'
228 // }
229 // }
230 // history.replaceState({ htmx: true }, '', path)
231 //
232 // mockResponse('GET', /\/*/, async function(xhr) {
233 // // foo should not be present because the form is a get with no action
234 // should.equal(undefined, getParameters(xhr).foo)
235 // xhr.respond(200, {}, 'Boosted!')
236 // })
237 //
238 // initHTML('<div hx-target:inherited="this" hx-boost="true"><form id="f1" method="get"><button id="b1">Submit</button></form></div>')
239 // find('#b1').click()
240 await forRequest()
241 // playground().innerHTML.should.equal('Boosted!')
242 // })
243 //
244 // it('form get with an empty action properly clears existing parameters on submit', async function() {
245 // /// add a foo=bar to the current url
246 // var path = location.href
247 // if (!path.includes('foo=bar')) {
248 // if (!path.includes('?')) {
249 // path += '?foo=bar'
250 // } else {
251 // path += '&foo=bar'
252 // }
253 // }
254 // history.replaceState({ htmx: true }, '', path)
255 //
256 // mockResponse('GET', /\/*/, async function(xhr) {
257 // // foo should not be present because the form is a get with no action
258 // should.equal(undefined, getParameters(xhr).foo)
259 // xhr.respond(200, {}, 'Boosted!')
260 // })
261 //
262 // initHTML('<div hx-target:inherited="this" hx-boost="true"><form id="f1" action="" method="get"><button id="b1">Submit</button></form></div>')
263 // find('#b1').click()
264 await forRequest()
265 // playground().innerHTML.should.equal('Boosted!')
266 // })
267 //
268 // if (/headlesschrome/i.test(navigator.userAgent)) {
269 // it('ctrlKey mouse click does not boost', async function() {
270 // // Test only works well in playwright with chome for code coverage as otherwise it opens a new tab breaking things
271 // mockResponse('GET', '/test', 'Boosted')
272 // initHTML('<div hx-target:inherited="this" hx-boost="true"><a id="a1" href="/test">Foo</a></div>')
273 // var a = byId('a1')
274 // var evt = new MouseEvent('click', { ctrlKey: true })
275 // a.dispatchEvent(evt)
276 // this.server.respond()
277 // playground().innerHTML.should.not.equal('Boosted')
278 // })
279 // }
280
281 })