Debian Codegraph @

test/tests/end2end/basic-history.js

raw symbols SCIP
1 describe('hx-push-url and hx-replace-url attributes', function() {
2
3 beforeEach(() => {
4 setupTest(this.currentTest)
5 // Clear any existing history state
6 if (history.state && history.state.htmx) {
7 history.replaceState(null, '', location.pathname);
8 }
9 })
10
11 afterEach(() => {
12 cleanupTest()
13 })
14
15 it('should push URL to history with hx-push-url="true"', async function() {
16 mockResponse('GET', '/test', 'Test Response');
17
18 let historyEventFired = false;
19 let eventPath = null;
20
21 // Listen for the history event
22 const handler = (event) => {
23 historyEventFired = true;
24 eventPath = event.detail.path;
25 };
26
27 document.addEventListener('htmx:after:history:push', handler);
28
29 try {
30 let btn = createProcessedHTML('<button hx-get="/test" hx-push-url="true">Click me</button>');
31 btn.click()
32 await forRequest();
33
34 playground().textContent.should.equal('Test Response');
35 historyEventFired.should.equal(true);
36 eventPath.should.equal('/test');
37 } finally {
38 document.removeEventListener('htmx:after:history:push', handler);
39 }
40 });
41
42 it('should replace URL in history with hx-replace-url="true"', async function() {
43 mockResponse('GET', '/test', 'Test Response');
44
45 let historyEventFired = false;
46 let eventPath = null;
47
48 // Listen for the history event
49 const handler = (event) => {
50 historyEventFired = true;
51 eventPath = event.detail.path;
52 };
53
54 document.addEventListener('htmx:after:history:replace', handler);
55
56 try {
57 let btn = createProcessedHTML('<button hx-get="/test" hx-replace-url="true">Click me</button>');
58 btn.click()
59 await forRequest();
60
61 playground().textContent.should.equal('Test Response');
62 historyEventFired.should.equal(true);
63 eventPath.should.equal('/test');
64 } finally {
65 document.removeEventListener('htmx:after:history:replace', handler);
66 }
67 });
68
69 it('should handle custom URL with hx-push-url="/custom"', async function() {
70 mockResponse('GET', '/test', 'Test Response');
71
72 let historyEventFired = false;
73 let eventPath = null;
74
75 // Listen for the history event
76 const handler = (event) => {
77 historyEventFired = true;
78 eventPath = event.detail.path;
79 };
80
81 document.addEventListener('htmx:after:history:push', handler);
82
83 try {
84 let btn = createProcessedHTML('<button hx-get="/test" hx-push-url="/custom">Click me</button>');
85 btn.click()
86 await forRequest();
87
88 playground().textContent.should.equal('Test Response');
89 historyEventFired.should.equal(true);
90 eventPath.should.equal('/custom');
91 } finally {
92 document.removeEventListener('htmx:after:history:push', handler);
93 }
94 });
95
96 it('should not push to history when hx-push-url="false"', async function() {
97 mockResponse('GET', '/test', 'Test Response');
98
99 let historyEventFired = false;
100
101 // Listen for the history event (should not fire)
102 const handler = (event) => {
103 historyEventFired = true;
104 };
105
106 document.addEventListener('htmx:after:history:push', handler);
107
108 try {
109 let btn = createProcessedHTML('<button hx-get="/test" hx-push-url="false">Click me</button>');
110 btn.click()
111 await forRequest();
112
113 playground().textContent.should.equal('Test Response');
114 historyEventFired.should.equal(false);
115 } finally {
116 document.removeEventListener('htmx:after:history:push', handler);
117 }
118 });
119
120 it('should fire htmx:before:history:update event', async function() {
121 mockResponse('GET', '/test', 'Test Response');
122
123 let beforeEventFired = false;
124 let eventDetails = null;
125
126 // Listen for the before history update event
127 const handler = (event) => {
128 beforeEventFired = true;
129 eventDetails = event.detail;
130 };
131
132 document.addEventListener('htmx:before:history:update', handler);
133
134 try {
135 let btn = createProcessedHTML('<button hx-get="/test" hx-push-url="true">Click me</button>');
136 btn.click()
137 await forRequest();
138
139 playground().textContent.should.equal('Test Response');
140 beforeEventFired.should.equal(true);
141 eventDetails.history.type.should.equal('push');
142 eventDetails.history.path.should.equal('/test');
143 } finally {
144 document.removeEventListener('htmx:before:history:update', handler);
145 }
146 });
147
148 // this test replaces body innerHTML, so we skip it to avoid breaking other tests, but it can be run manually to verify the behavior
149 it.skip('should use innerHTML swap when restoring history even if default swap is none', async function() {
150 let originalSwap = htmx.config.defaultSwap;
151 htmx.config.defaultSwap = 'none';
152
153 try {
154 mockResponse('GET', '/restore-test', '<div id="restored">Restored Content</div>');
155
156 htmx.__restoreHistory('/restore-test');
157 await forRequest();
158
159 document.body.innerHTML.should.include('Restored Content');
160 } finally {
161 htmx.config.defaultSwap = originalSwap;
162 }
163 });
164 });
165
166
167 describe('outerSync swap into document.body', function() {
168 let savedAttrs;
169 let savedChildren;
170
171 beforeEach(() => {
172 setupTest(this.currentTest);
173 savedAttrs = [...document.body.attributes].map(a => ({ name: a.name, value: a.value }));
174 savedChildren = [...document.body.childNodes];
175 });
176
177 afterEach(() => {
178 for (let a of [...document.body.attributes]) document.body.removeAttribute(a.name);
179 for (let a of savedAttrs) document.body.setAttribute(a.name, a.value);
180 document.body.replaceChildren(...savedChildren);
181 cleanupTest();
182 });
183
184 it('syncs body attributes from response and replaces children', async function() {
185 document.body.setAttribute('data-original', 'yes');
186 await htmx.swap({
187 target: document.body,
188 swap: 'outerSync',
189 text: '<html><body class="injected" data-test-x="1"><div id="bodyswap-marker"></div></body></html>',
190 sourceElement: document.body
191 });
192 document.body.classList.contains('injected').should.equal(true);
193 document.body.getAttribute('data-test-x').should.equal('1');
194 (document.body.getAttribute('data-original') === null).should.equal(true);
195 document.getElementById('bodyswap-marker').should.not.equal(null);
196 });
197
198 it('outerHTML on body auto-upgrades to outerSync and syncs attributes', async function() {
199 document.body.setAttribute('data-original', 'yes');
200 await htmx.swap({
201 target: document.body,
202 swap: 'outerHTML',
203 text: '<html><body class="injected" data-test-x="1"><div id="bodyswap-marker"></div></body></html>',
204 sourceElement: document.body
205 });
206 document.body.classList.contains('injected').should.equal(true);
207 document.body.getAttribute('data-test-x').should.equal('1');
208 (document.body.getAttribute('data-original') === null).should.equal(true);
209 document.getElementById('bodyswap-marker').should.not.equal(null);
210 });
211 });
212
213 describe('full-page response strip auto-upgrade', function() {
214
215 beforeEach(() => { setupTest(this.currentTest); });
216 afterEach(() => { cleanupTest(); });
217
218 it('innerHTML on full-page response strips body wrapper', async function() {
219 playground().innerHTML = '<div id="target">old</div>';
220 await htmx.swap({
221 target: '#target',
222 swap: 'innerHTML',
223 text: '<html><body><span id="new-child">new</span></body></html>',
224 sourceElement: playground()
225 });
226 let target = playground().querySelector('#target');
227 target.should.not.equal(null);
228 (target.querySelector('body') === null).should.equal(true);
229 target.querySelector('#new-child').should.not.equal(null);
230 target.querySelector('#new-child').textContent.should.equal('new');
231 });
232
233 it('innerMorph on full-page response strips body wrapper', async function() {
234 playground().innerHTML = '<div id="target"><span id="orig">old</span></div>';
235 await htmx.swap({
236 target: '#target',
237 swap: 'innerMorph',
238 text: '<html><body><span id="new-child">new</span></body></html>',
239 sourceElement: playground()
240 });
241 let target = playground().querySelector('#target');
242 target.should.not.equal(null);
243 (target.querySelector('body') === null).should.equal(true);
244 target.querySelector('#new-child').should.not.equal(null);
245 });
246
247 it('beforeend on full-page response strips body wrapper', async function() {
248 playground().innerHTML = '<div id="target"><span id="orig">old</span></div>';
249 await htmx.swap({
250 target: '#target',
251 swap: 'beforeend',
252 text: '<html><body><span id="appended">added</span></body></html>',
253 sourceElement: playground()
254 });
255 let target = playground().querySelector('#target');
256 (target.querySelector('body') === null).should.equal(true);
257 target.querySelector('#orig').should.not.equal(null);
258 target.querySelector('#appended').should.not.equal(null);
259 });
260
261 it('partial response is unaffected by strip auto-upgrade', async function() {
262 playground().innerHTML = '<div id="target">old</div>';
263 await htmx.swap({
264 target: '#target',
265 swap: 'innerHTML',
266 text: '<span id="partial-child">partial</span>',
267 sourceElement: playground()
268 });
269 let target = playground().querySelector('#target');
270 target.querySelector('#partial-child').should.not.equal(null);
271 target.querySelector('#partial-child').textContent.should.equal('partial');
272 });
273 });
274
275 describe('outerSync processes inserted nodes correctly', function() {
276
277 beforeEach(() => { setupTest(this.currentTest); });
278 afterEach(() => { cleanupTest(); });
279
280 it('processes hx-trigger="load" elements after outerSync swap (issue #3807)', async function() {
281 // Simulate the history restore scenario: outerSync into a target with a full-page response
282 // containing an element with hx-trigger="load". The load trigger must fire on the
283 // live DOM node, not on the detached <body> fragment.
284 mockResponse('GET', '/load-target', 'loaded by hx-trigger="load"');
285
286 let target = createProcessedHTML('<div id="sync-target"><p>old content</p></div>');
287
288 await htmx.swap({
289 target: '#sync-target',
290 swap: 'outerSync',
291 text: '<html><body><div id="sync-target"><span id="load-elt" hx-get="/load-target" hx-trigger="load" hx-swap="innerHTML">loading...</span></div></body></html>',
292 sourceElement: target
293 });
294
295 // The load trigger should have fired and issued a request
296 await forRequest();
297
298 let elt = document.getElementById('load-elt');
299 elt.should.not.equal(null);
300 elt.textContent.should.equal('loaded by hx-trigger="load"');
301 });
302
303 it('initializes htmx attributes on nodes inserted via outerSync', async function() {
304 let target = createProcessedHTML('<div id="sync-target"><p>old</p></div>');
305
306 await htmx.swap({
307 target: '#sync-target',
308 swap: 'outerSync',
309 text: '<html><body><div id="sync-target"><button id="btn" hx-get="/test" hx-swap="innerHTML">click</button></div></body></html>',
310 sourceElement: target
311 });
312
313 let btn = document.getElementById('btn');
314 btn.should.not.equal(null);
315 assert.isNotNull(btn._htmx, 'button should be initialized by htmx');
316 });
317 });
318
319 describe('hx-history-elt scopes history restore', function() {
320
321 beforeEach(() => { setupTest(this.currentTest); });
322 afterEach(() => { cleanupTest(); });
323
324 it('restoring history with hx-history-elt swaps only that element and leaves siblings intact', async function() {
325 playground().innerHTML = `
326 <div id="sentinel">untouched</div>
327 <main hx-history-elt><p id="orig">old</p></main>
328 <div id="sentinel-after">also untouched</div>
329 `;
330 htmx.process(playground());
331
332 let response = `<html><head><title>x</title></head><body>
333 <header>HEADER LEAK</header>
334 <main hx-history-elt><p id="new">new</p></main>
335 <footer>FOOTER LEAK</footer>
336 </body></html>`;
337 mockResponse('GET', '/restore-test', response);
338
339 htmx.__restoreHistory('/restore-test');
340 await forRequest();
341
342 document.getElementById('sentinel').should.not.equal(null);
343 document.getElementById('sentinel').textContent.should.equal('untouched');
344 document.getElementById('sentinel-after').should.not.equal(null);
345 document.getElementById('sentinel-after').textContent.should.equal('also untouched');
346
347 let elt = playground().querySelector('[hx-history-elt]');
348 elt.should.not.equal(null);
349 elt.querySelector('#new').should.not.equal(null);
350 (elt.querySelector('#orig') === null).should.equal(true);
351
352 document.body.textContent.should.not.include('HEADER LEAK');
353 document.body.textContent.should.not.include('FOOTER LEAK');
354 });
355 });