-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.ts
More file actions
632 lines (520 loc) · 18.6 KB
/
index.test.ts
File metadata and controls
632 lines (520 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import { describe, test, expect, beforeEach } from "bun:test";
import { Window } from "happy-dom";
describe("chunked-transfer extension", () => {
let window: Window;
let document: Document;
let mockApi: any;
let registeredExtension: any;
let target: HTMLElement;
beforeEach(async () => {
// Fresh DOM for each test
window = new Window();
document = window.document as unknown as Document;
globalThis.document = document;
// Create target element
target = document.createElement("div");
target.id = "target";
document.body.appendChild(target);
// Mock htmx API
mockApi = {
getTarget: () => target,
getSwapSpecification: () => ({ swapStyle: "innerHTML" }),
makeSettleInfo: () => ({ tasks: [], elts: [] }),
swap: (targetEl: HTMLElement, content: string) => {
// Real DOM manipulation for v2 API
targetEl.innerHTML = content;
},
selectAndSwap: (
swapStyle: string,
targetEl: HTMLElement,
elt: Element,
content: string,
) => {
// Real DOM manipulation for v1 API
targetEl.innerHTML = content;
},
settleImmediately: () => {},
withExtensions: (elt: Element, cb: (ext: any) => void) => {},
};
globalThis.htmx = {
defineExtension: (name: string, ext: any) => {
registeredExtension = ext;
},
} as any;
// Dynamically import to execute the extension IIFE
await import("./index.ts");
registeredExtension.init(mockApi);
});
describe("core functionality", () => {
test("registers extension with correct name", () => {
expect(globalThis.htmx.defineExtension).toBeDefined();
expect(registeredExtension).toBeDefined();
expect(registeredExtension.init).toBeDefined();
expect(registeredExtension.onEvent).toBeDefined();
});
test("ignores non-chunked responses", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return null;
// Non-chunked responses have Content-Length
if (header === "Content-Length") return "22";
return null;
},
response: "<p>Normal response</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
// Simulate progress - should do nothing for non-chunked
if (mockXhr.onprogress) {
mockXhr.onprogress();
}
// Target should still be empty (no swap happened)
expect(target.innerHTML).toBe("");
});
test("processes HTTP/2 streaming responses (no Transfer-Encoding or Content-Length)", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
// HTTP/2 streaming: no Transfer-Encoding, no Content-Length
return null;
},
response: "<p>HTTP/2 streamed content</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
// Verify onprogress handler was set
expect(mockXhr.onprogress).toBeDefined();
// Simulate progress event
mockXhr.onprogress!();
// Verify content was swapped
expect(target.innerHTML).toBe("<p>HTTP/2 streamed content</p>");
});
test("ignores events other than beforeRequest", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: () => "chunked",
response: "<p>Content</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
// This should not set up xhr.onprogress
registeredExtension.onEvent("htmx:afterRequest", event);
expect(mockXhr.onprogress).toBeNull();
});
});
describe("chunked response processing", () => {
test("processes chunked responses with v2 API (swap method)", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<p>Chunk 1</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
// Verify onprogress handler was set
expect(mockXhr.onprogress).toBeDefined();
// Simulate progress event
mockXhr.onprogress!();
// Verify content was swapped
expect(target.innerHTML).toBe("<p>Chunk 1</p>");
});
test("processes chunked responses with v1 API (selectAndSwap fallback)", () => {
const element = document.createElement("div");
// Remove v2 swap method to force v1 fallback
const originalSwap = mockApi.swap;
delete mockApi.swap;
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<p>Chunk from v1</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Verify content was swapped using v1 API
expect(target.innerHTML).toBe("<p>Chunk from v1</p>");
// Restore for other tests
mockApi.swap = originalSwap;
});
test("accumulates multiple chunks (default append behavior)", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<p>A</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
// Simulate three progressive chunks
mockXhr.response = "<p>A</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>A</p>");
mockXhr.response = "<p>A</p><p>B</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>A</p><p>B</p>");
mockXhr.response = "<p>A</p><p>B</p><p>C</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>A</p><p>B</p><p>C</p>");
});
});
describe("extension transformation chain", () => {
test("allows other extensions to transform response", () => {
const element = document.createElement("div");
// Mock extension that adds prefix to response
mockApi.withExtensions = (elt: Element, callback: (ext: any) => void) => {
callback({
transformResponse: (text: string) => `<div>transformed</div>${text}`,
});
};
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<p>content</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Verify content was transformed (div prepended)
expect(target.innerHTML).toBe("<div>transformed</div><p>content</p>");
});
});
describe("comment filtering (heartbeats)", () => {
test("ignores comment-only chunks", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<!-- heartbeat -->",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Target should remain empty (comment-only chunk ignored)
expect(target.innerHTML).toBe("");
});
test("ignores multi-line comment-only chunks", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<!-- \n heartbeat\n still processing\n -->",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Multi-line comment should also be ignored
expect(target.innerHTML).toBe("");
});
test("ignores multiple comments without content", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<!-- comment 1 --><!-- comment 2 --> ",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Multiple comments with only whitespace should be ignored
expect(target.innerHTML).toBe("");
});
test("processes chunks with comments and HTML content", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<!-- debug info --><p>Content</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Should process the chunk (has HTML beyond comments)
expect(target.innerHTML).toBe("<!-- debug info --><p>Content</p>");
});
test("processes chunks with comments between HTML", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<p>Start</p><!-- middle --><p>End</p>",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Should process the entire chunk
expect(target.innerHTML).toBe("<p>Start</p><!-- middle --><p>End</p>");
});
test("processes empty chunks (no comments)", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Empty chunks pass through (no content added, but swap is called)
expect(target.innerHTML).toBe("");
});
test("processes whitespace-only chunks (no comments)", () => {
const element = document.createElement("div");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: " \n \t ",
onprogress: null as any,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
mockXhr.onprogress!();
// Whitespace-only chunks (no comments) pass through and get trimmed by DOM
// The whitespace gets normalized to empty when set as innerHTML
expect(target.innerHTML.trim()).toBe("");
});
});
describe("hx-chunked-mode=swap", () => {
test("default mode (append) - accumulates all chunks", () => {
const element = document.createElement("div");
// No hx-chunked-mode attribute = defaults to append
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "",
onprogress: null as any,
_chunkedMode: undefined,
_chunkedLastLen: 0,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
// Verify mode was set to append
expect(mockXhr._chunkedMode).toBe("append");
// Simulate progressive chunks - full response each time
mockXhr.response = "<p>Loading...</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>Loading...</p>");
mockXhr.response = "<p>Loading...</p><p>50%</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>Loading...</p><p>50%</p>");
mockXhr.response = "<p>Loading...</p><p>50%</p><p>Done!</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>Loading...</p><p>50%</p><p>Done!</p>");
});
test("swap mode - only swaps new content (incremental)", () => {
const element = document.createElement("div");
element.setAttribute("hx-chunked-mode", "swap");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "",
onprogress: null as any,
_chunkedMode: undefined,
_chunkedLastLen: 0,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
// Verify mode was set to swap
expect(mockXhr._chunkedMode).toBe("swap");
// First chunk - everything is new
mockXhr.response = "<p>Loading...</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>Loading...</p>");
expect(mockXhr._chunkedLastLen).toBe(mockXhr.response.length);
// Second chunk - only new content swapped
mockXhr.response = "<p>Loading...</p><p>50%</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>50%</p>"); // Only the NEW part
expect(mockXhr._chunkedLastLen).toBe(mockXhr.response.length);
// Third chunk - only newest content swapped
mockXhr.response = "<p>Loading...</p><p>50%</p><p>Done!</p>";
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>Done!</p>"); // Only the NEWEST part
expect(mockXhr._chunkedLastLen).toBe(mockXhr.response.length);
});
test("swap mode - ignores duplicate progress events (same length)", () => {
const element = document.createElement("div");
element.setAttribute("hx-chunked-mode", "swap");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<p>Content</p>",
onprogress: null as any,
_chunkedMode: undefined,
_chunkedLastLen: 0,
};
const event = {
target: element,
detail: { xhr: mockXhr },
};
registeredExtension.onEvent("htmx:beforeRequest", event);
// First progress event
mockXhr.onprogress!();
expect(target.innerHTML).toBe("<p>Content</p>");
const firstHtml = target.innerHTML;
// Duplicate progress event - same response length
mockXhr.onprogress!();
// Should not swap again (innerHTML unchanged)
expect(target.innerHTML).toBe(firstHtml);
});
test("swap mode - prevents final htmx:beforeSwap", () => {
const element = document.createElement("div");
element.setAttribute("hx-chunked-mode", "swap");
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<p>Final</p>",
_chunkedMode: "swap",
};
const beforeSwapEvent = {
target: element,
detail: {
xhr: mockXhr,
shouldSwap: true, // Initially true
},
};
// Trigger beforeSwap event
registeredExtension.onEvent("htmx:beforeSwap", beforeSwapEvent);
// Verify shouldSwap was set to false
expect(beforeSwapEvent.detail.shouldSwap).toBe(false);
});
test("append mode - allows final htmx:beforeSwap", () => {
const element = document.createElement("div");
// Default append mode
const mockXhr = {
getResponseHeader: (header: string) => {
if (header === "Transfer-Encoding") return "chunked";
return null;
},
response: "<p>Final</p>",
_chunkedMode: "append",
};
const beforeSwapEvent = {
target: element,
detail: {
xhr: mockXhr,
shouldSwap: true,
},
};
// Trigger beforeSwap event
registeredExtension.onEvent("htmx:beforeSwap", beforeSwapEvent);
// Verify shouldSwap is still true (not prevented)
expect(beforeSwapEvent.detail.shouldSwap).toBe(true);
});
test("swap mode - non-chunked responses allow final swap", () => {
const element = document.createElement("div");
element.setAttribute("hx-chunked-mode", "swap");
const mockXhr = {
getResponseHeader: (header: string) => {
// NOT chunked - has Content-Length
if (header === "Content-Length") return "15";
return null;
},
response: "<p>Final</p>",
_chunkedMode: "swap",
};
const beforeSwapEvent = {
target: element,
detail: {
xhr: mockXhr,
shouldSwap: true,
},
};
// Trigger beforeSwap event
registeredExtension.onEvent("htmx:beforeSwap", beforeSwapEvent);
// Non-chunked responses should NOT be prevented
expect(beforeSwapEvent.detail.shouldSwap).toBe(true);
});
});
});