-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRenderer.test.ts
More file actions
1589 lines (1239 loc) · 53 KB
/
Copy pathRenderer.test.ts
File metadata and controls
1589 lines (1239 loc) · 53 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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import DOMPurify from 'dompurify';
import React from 'react';
import Renderer from '../core/Renderer';
// Mock React components for testing
const MockComponent: React.FC<any> = (props) => {
return React.createElement('div', props);
};
describe('Renderer', () => {
describe('detectUnclosedTags', () => {
it('should detect unclosed custom tags', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case 1: Unclosed tag
const html1 = '<custom-tag>content';
const result1 = detectUnclosedTags(html1);
expect(result1.has('custom-tag-1')).toBe(true);
// Test case 2: Closed tag
const html2 = '<custom-tag>content</custom-tag>';
const result2 = detectUnclosedTags(html2);
expect(result2.size).toBe(0);
// Test case 3: Self-closing tag
const html3 = '<custom-tag />';
const result3 = detectUnclosedTags(html3);
expect(result3.size).toBe(0);
});
it('should handle multiple tags correctly', () => {
const renderer = new Renderer({
components: {
'tag-a': MockComponent,
'tag-b': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case: One closed, one unclosed
const html = '<tag-a>content</tag-a><tag-b>content';
const result = detectUnclosedTags(html);
expect(result.size).toBe(1);
expect(result.has('tag-a-1')).toBe(false);
expect(result.has('tag-b-1')).toBe(true);
});
it('should ignore non-custom tags', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case: Standard HTML tags should be ignored
const html = '<div>content<p>paragraph';
const result = detectUnclosedTags(html);
expect(result.size).toBe(0);
});
it('should handle void elements correctly', () => {
const renderer = new Renderer({
components: {
img: MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case: Void elements should not be considered unclosed
const html = '<img src="image.png"><img src="image2.png" />';
const result = detectUnclosedTags(html);
expect(result.size).toBe(0);
// Test case: Unclosed void element
const html2 = '<img src="image.p';
const result2 = detectUnclosedTags(html2);
expect(result2.has('img-1')).toBe(true);
// Test case: Nested void elements
const html3 = '<div><img src="image.png"></div><p>';
const result3 = detectUnclosedTags(html3);
expect(result3.size).toBe(0);
});
it('should handle HTML comments correctly', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Comment inside closed tag - should be properly closed
const html1 = '<custom-tag><!-- <unclosed> comment --></custom-tag>';
const result1 = detectUnclosedTags(html1);
expect(result1.size).toBe(0);
// Comment before and after tag
const html2 = '<!-- comment --><custom-tag>content</custom-tag><!-- end -->';
const result2 = detectUnclosedTags(html2);
expect(result2.size).toBe(0);
// Comment before unclosed tag
const html3 = '<!-- comment --><custom-tag>content';
const result3 = detectUnclosedTags(html3);
expect(result3.has('custom-tag-1')).toBe(true);
});
it('should handle CDATA sections correctly', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// CDATA inside closed tag
const html1 = '<custom-tag><![CDATA[<unclosed>]]></custom-tag>';
const result1 = detectUnclosedTags(html1);
expect(result1.size).toBe(0);
// CDATA before tag
const html2 = '<![CDATA[some data]]><custom-tag>content</custom-tag>';
const result2 = detectUnclosedTags(html2);
expect(result2.size).toBe(0);
// CDATA before unclosed tag
const html3 = '<![CDATA[data]]><custom-tag>content';
const result3 = detectUnclosedTags(html3);
expect(result3.has('custom-tag-1')).toBe(true);
});
it('should handle escaped quotes in attributes', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Escaped double quotes in attribute
const html1 = '<custom-tag attr="he said \\"hello\\"">content</custom-tag>';
const result1 = detectUnclosedTags(html1);
expect(result1.size).toBe(0);
// Escaped single quotes in attribute
const html2 = "<custom-tag attr='it\\'s fine'>content</custom-tag>";
const result2 = detectUnclosedTags(html2);
expect(result2.size).toBe(0);
// Mixed quotes with escaped characters
const html3 = '<custom-tag a="\\"test\\"" b="\'value\'">content</custom-tag>';
const result3 = detectUnclosedTags(html3);
expect(result3.size).toBe(0);
});
it('should handle multiple comments and CDATA mixed with tags', () => {
const renderer = new Renderer({
components: {
'tag-a': MockComponent,
'tag-b': MockComponent,
},
});
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Complex HTML with comments, CDATA, and multiple tags
const html = '<!-- start --><tag-a><!-- inner --><![CDATA[data]]></tag-a><tag-b>unclosed';
const result = detectUnclosedTags(html);
expect(result.size).toBe(1);
expect(result.has('tag-b-1')).toBe(true);
expect(result.has('tag-a-1')).toBe(false);
});
});
describe('processHtml', () => {
it('should pass correct streamStatus to custom components', () => {
const components = {
'streaming-component': MockComponent,
};
const renderer = new Renderer({ components });
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Test case 1: Closed tag should have streamStatus="done"
const html1 = '<streaming-component>content</streaming-component>';
renderer.processHtml(html1);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
streamStatus: 'done',
}),
);
createElementSpy.mockClear();
// Note: Testing unclosed tags is more complex because html-react-parser
// won't call the replace function for malformed HTML. In a real streaming
// scenario, the HTML would be processed incrementally as it's received.
createElementSpy.mockRestore();
});
it('should handle self-closing tags correctly', () => {
const components = {
'self-closing': MockComponent,
};
const renderer = new Renderer({ components });
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Test case: Self-closing tag should have streamStatus="done"
const html = '<self-closing />';
renderer.processHtml(html);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
streamStatus: 'done',
}),
);
createElementSpy.mockRestore();
});
it('should correctly process mixed content with custom components', () => {
const components = {
'component-a': MockComponent,
'component-b': MockComponent,
};
const renderer = new Renderer({ components });
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Test case: Mixed content with multiple custom components
const html = '<p>Some text</p><component-a>Content A</component-a><component-b />More text';
renderer.processHtml(html);
// Verify that component-a was called with streamStatus="done" (closed tag)
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
streamStatus: 'done',
}),
);
createElementSpy.mockRestore();
});
it('should handle streaming scenario with unclosed tags', () => {
const components = {
'streaming-tag': MockComponent,
};
const renderer = new Renderer({ components });
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// In a real streaming scenario, we might process HTML incrementally
// For this test, we simulate the state at different points in the stream
// First, process incomplete HTML (unclosed tag)
const html1 = '<streaming-tag>Partial content';
renderer.processHtml(html1);
// Then, process complete HTML (closed tag)
createElementSpy.mockClear();
const html2 = '<streaming-tag>Partial content</streaming-tag>';
renderer.processHtml(html2);
// Verify that the component was called with streamStatus="done" (closed tag)
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
streamStatus: 'done',
}),
);
createElementSpy.mockRestore();
});
it('should handle multiple instances of the same component with correct streamStatus', () => {
const components = {
'multi-instance': MockComponent,
};
const renderer = new Renderer({ components });
const createElementSpy = jest.spyOn(React, 'createElement');
// Scenario: First instance is closed (done), second instance is open (loading)
const html = '<multi-instance>Instance 1</multi-instance><multi-instance>Instance 2';
renderer.processHtml(html);
// Filter calls to MockComponent
const calls = createElementSpy.mock.calls.filter((call) => call[0] === MockComponent);
expect(calls.length).toBe(2);
// First instance should be done
expect(calls[0][1]).toEqual(
expect.objectContaining({
streamStatus: 'done',
}),
);
// Second instance should be loading
expect(calls[1][1]).toEqual(
expect.objectContaining({
streamStatus: 'loading',
}),
);
createElementSpy.mockRestore();
});
it('should mark all instances as done when all are closed', () => {
const components = {
'multi-instance': MockComponent,
};
const renderer = new Renderer({ components });
const createElementSpy = jest.spyOn(React, 'createElement');
const html =
'<multi-instance>Instance 1</multi-instance><multi-instance>Instance 2</multi-instance>';
renderer.processHtml(html);
const calls = createElementSpy.mock.calls.filter((call) => call[0] === MockComponent);
expect(calls.length).toBe(2);
expect(calls[0][1]).toEqual(expect.objectContaining({ streamStatus: 'done' }));
expect(calls[1][1]).toEqual(expect.objectContaining({ streamStatus: 'done' }));
createElementSpy.mockRestore();
});
it('should handle nested instances of the same component correctly', () => {
const components = {
'multi-instance': MockComponent,
};
const renderer = new Renderer({ components });
const createElementSpy = jest.spyOn(React, 'createElement');
// Outer open, Inner closed
const html = '<multi-instance>Outer <multi-instance>Inner</multi-instance>';
renderer.processHtml(html);
const calls = createElementSpy.mock.calls.filter((call) => call[0] === MockComponent);
expect(calls.length).toBe(2);
// Inner instance (processed first as children are created before parent)
expect(calls[0][1]).toEqual(expect.objectContaining({ streamStatus: 'done' }));
// Outer instance
expect(calls[1][1]).toEqual(expect.objectContaining({ streamStatus: 'loading' }));
createElementSpy.mockRestore();
});
it('should handle deep nesting with mixed states', () => {
const components = {
'multi-instance': MockComponent,
};
const renderer = new Renderer({ components });
const createElementSpy = jest.spyOn(React, 'createElement');
// Level 1: Open
// Level 2: Open
// Level 3: Closed
const html = '<multi-instance>1<multi-instance>2<multi-instance>3</multi-instance>';
renderer.processHtml(html);
const calls = createElementSpy.mock.calls.filter((call) => call[0] === MockComponent);
expect(calls.length).toBe(3);
// Level 3 (Inner most) - Done
expect(calls[0][1]).toEqual(expect.objectContaining({ streamStatus: 'done' }));
// Level 2 - Loading
expect(calls[1][1]).toEqual(expect.objectContaining({ streamStatus: 'loading' }));
// Level 1 - Loading
expect(calls[2][1]).toEqual(expect.objectContaining({ streamStatus: 'loading' }));
createElementSpy.mockRestore();
});
it('should handle interleaved components correctly', () => {
const components = {
'comp-a': MockComponent,
'comp-b': MockComponent,
};
const renderer = new Renderer({ components });
const createElementSpy = jest.spyOn(React, 'createElement');
// comp-a closed, comp-b closed, comp-a open
const html = '<comp-a>A1</comp-a><comp-b>B1</comp-b><comp-a>A2';
renderer.processHtml(html);
const callsA = createElementSpy.mock.calls.filter(
(call) => call[0] === MockComponent && (call[1] as any).domNode.name === 'comp-a',
);
const callsB = createElementSpy.mock.calls.filter(
(call) => call[0] === MockComponent && (call[1] as any).domNode.name === 'comp-b',
);
expect(callsA.length).toBe(2);
expect(callsB.length).toBe(1);
expect(callsA[0][1]).toEqual(expect.objectContaining({ streamStatus: 'done' }));
expect(callsB[0][1]).toEqual(expect.objectContaining({ streamStatus: 'done' }));
expect(callsA[1][1]).toEqual(expect.objectContaining({ streamStatus: 'loading' }));
createElementSpy.mockRestore();
});
it('should merge class and className attributes correctly', () => {
const components = {
'test-component': MockComponent,
};
const renderer = new Renderer({
components,
dompurifyConfig: { ALLOWED_ATTR: ['class', 'className'] },
});
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Test case 1: Only class attribute
const html1 = '<test-component class="custom-class">content</test-component>';
renderer.processHtml(html1);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: 'custom-class',
}),
);
createElementSpy.mockClear();
// Test case 2: Only className attribute
const html2 = '<test-component className="existing-class">content</test-component>';
renderer.processHtml(html2);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: 'existing-class',
}),
);
createElementSpy.mockClear();
// Test case 3: Both class and className attributes
const html3 =
'<test-component class="new-class" className="existing-class">content</test-component>';
renderer.processHtml(html3);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: 'existing-class new-class',
}),
);
createElementSpy.mockClear();
// Test case 4: Multiple classes in class attribute
const html4 =
'<test-component class="class1 class2" className="existing-class">content</test-component>';
renderer.processHtml(html4);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: 'existing-class class1 class2',
}),
);
createElementSpy.mockClear();
// Test case 5: Empty class attribute
const html5 = '<test-component class="" className="existing-class">content</test-component>';
renderer.processHtml(html5);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: 'existing-class',
}),
);
createElementSpy.mockClear();
// Test case 6: Empty className attribute
const html6 = '<test-component class="new-class" className="">content</test-component>';
renderer.processHtml(html6);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: 'new-class',
}),
);
createElementSpy.mockClear();
// Test case 7: Both empty
const html7 = '<test-component class="" className="">content</test-component>';
renderer.processHtml(html7);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: '',
}),
);
createElementSpy.mockRestore();
});
it('should handle class attribute merging with special characters', () => {
const components = {
'test-component': MockComponent,
};
const renderer = new Renderer({
components,
dompurifyConfig: { ALLOWED_ATTR: ['class', 'className'] },
});
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Test case: Classes with special characters
const html =
'<test-component class="btn btn-primary" className="custom-component">content</test-component>';
renderer.processHtml(html);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: 'custom-component btn btn-primary',
}),
);
createElementSpy.mockRestore();
});
it('should preserve other attributes while merging class and className', () => {
const components = {
'test-component': MockComponent,
};
const renderer = new Renderer({
components,
dompurifyConfig: { ALLOWED_ATTR: ['class', 'classname', 'id', 'data-test'] },
});
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Test case: Multiple attributes including class and className
const html =
'<test-component id="test-id" class="custom-class" classname="existing-class" data-test="value">content</test-component>';
renderer.processHtml(html);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
id: 'test-id',
className: 'existing-class custom-class',
'data-test': 'value',
}),
);
createElementSpy.mockRestore();
});
});
describe('configureDOMPurify', () => {
it('should configure DOMPurify with custom components', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
'another-tag': MockComponent,
},
});
// Access private method for testing
const configureDOMPurify = (renderer as any).configureDOMPurify.bind(renderer);
const config = configureDOMPurify();
expect(config.ADD_TAGS).toContain('custom-tag');
expect(config.ADD_TAGS).toContain('another-tag');
});
it('should merge with user provided dompurifyConfig', () => {
const userConfig = {
ALLOWED_TAGS: ['div', 'span'],
ALLOWED_ATTR: ['class', 'id'],
};
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
dompurifyConfig: userConfig,
});
// Access private method for testing
const configureDOMPurify = (renderer as any).configureDOMPurify.bind(renderer);
const config = configureDOMPurify();
expect(config.ADD_TAGS).toContain('custom-tag');
expect(config.ALLOWED_TAGS).toContain('div');
expect(config.ALLOWED_TAGS).toContain('span');
expect(config.ALLOWED_ATTR).toContain('class');
expect(config.ALLOWED_ATTR).toContain('id');
});
it('should handle empty components', () => {
const renderer = new Renderer({});
// Access private method for testing
const configureDOMPurify = (renderer as any).configureDOMPurify.bind(renderer);
const config = configureDOMPurify();
expect(config.ADD_TAGS).toEqual([]);
});
it('should deduplicate tags in ADD_TAGS', () => {
const userConfig = {
ADD_TAGS: ['custom-tag', 'custom-tag2'],
};
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
dompurifyConfig: userConfig,
});
// Access private method for testing
const configureDOMPurify = (renderer as any).configureDOMPurify.bind(renderer);
const config = configureDOMPurify();
expect(config.ADD_TAGS).toEqual(['custom-tag', 'custom-tag2']);
expect(config.ALLOWED_TAGS).toBeUndefined();
});
});
describe('detectUnclosedTags edge cases', () => {
it('should handle nested tags correctly', () => {
const renderer = new Renderer({
components: {
'outer-tag': MockComponent,
'inner-tag': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case: Properly nested and closed tags
const html1 = '<outer-tag><inner-tag>content</inner-tag></outer-tag>';
const result1 = detectUnclosedTags(html1);
expect(result1.size).toBe(0);
// Test case: Inner tag unclosed
const html2 = '<outer-tag><inner-tag>content</outer-tag>';
const result2 = detectUnclosedTags(html2);
expect(result2.size).toBe(1);
expect(result2.has('inner-tag-1')).toBe(true);
expect(result2.has('outer-tag-1')).toBe(false);
// Test case: Outer tag unclosed
const html3 = '<outer-tag><inner-tag>content</inner-tag>';
const result3 = detectUnclosedTags(html3);
expect(result3.size).toBe(1);
expect(result3.has('outer-tag-1')).toBe(true);
expect(result3.has('inner-tag-1')).toBe(false);
});
it('should handle case insensitive tag names', () => {
const renderer = new Renderer({
components: {
'test-tag': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case: Mixed case tags
const html1 = '<Test-Tag>content';
const result1 = detectUnclosedTags(html1);
expect(result1.has('test-tag-1')).toBe(true);
const html2 = '<TEST-TAG>content</TEST-TAG>';
const result2 = detectUnclosedTags(html2);
expect(result2.size).toBe(0);
});
it('should handle tags with attributes', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case: Tag with attributes, unclosed
const html1 = '<custom-tag class="test" id="my-id">content';
const result1 = detectUnclosedTags(html1);
expect(result1.has('custom-tag-1')).toBe(true);
// Test case: Tag with attributes, closed
const html2 = '<custom-tag class="test" id="my-id">content</custom-tag>';
const result2 = detectUnclosedTags(html2);
expect(result2.size).toBe(0);
});
it('should handle malformed HTML gracefully', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case: Malformed closing tag
const html1 = '<custom-tag>content</custom-tag';
const result1 = detectUnclosedTags(html1);
expect(result1.has('custom-tag-1')).toBe(true);
// Test case: Missing closing bracket
const html2 = '<custom-tag>content';
const result2 = detectUnclosedTags(html2);
expect(result2.has('custom-tag-1')).toBe(true);
// Test case: Extra closing tags
const html3 = '<custom-tag>content</custom-tag></custom-tag>';
const result3 = detectUnclosedTags(html3);
expect(result3.size).toBe(0);
});
it('should handle empty string', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
const result = detectUnclosedTags('');
expect(result.size).toBe(0);
});
it('should handle same tag multiple times', () => {
const renderer = new Renderer({
components: {
'custom-tag': MockComponent,
},
});
// Access private method for testing
const detectUnclosedTags = (renderer as any).detectUnclosedTags.bind(renderer);
// Test case: Multiple instances, some closed, some not
const html = '<custom-tag>first</custom-tag><custom-tag>second';
const result = detectUnclosedTags(html);
expect(result.has('custom-tag-2')).toBe(true);
});
});
describe('streamStatus integration', () => {
it('should set streamStatus to loading for unclosed tags in processHtml', () => {
const components = {
'streaming-tag': MockComponent,
};
const renderer = new Renderer({ components });
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Note: Due to html-react-parser behavior, we need to simulate the scenario
// where we have valid HTML but want to test the streamStatus logic
// We'll use a mock to control the detectUnclosedTags result
const mockUnclosedTags = new Set(['streaming-tag-1']);
jest.spyOn(renderer as any, 'detectUnclosedTags').mockReturnValue(mockUnclosedTags);
const html = '<streaming-tag>content</streaming-tag>';
renderer.processHtml(html);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
streamStatus: 'loading',
}),
);
createElementSpy.mockRestore();
jest.restoreAllMocks();
});
it('should set streamStatus to done for closed tags in processHtml', () => {
const components = {
'streaming-tag': MockComponent,
};
const renderer = new Renderer({ components });
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Mock detectUnclosedTags to return empty set (all tags closed)
jest.spyOn(renderer as any, 'detectUnclosedTags').mockReturnValue(new Set());
const html = '<streaming-tag>content</streaming-tag>';
renderer.processHtml(html);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
streamStatus: 'done',
}),
);
createElementSpy.mockRestore();
jest.restoreAllMocks();
});
it('should handle complex nested structures with partial component mapping', () => {
const ArticleComponent: React.FC<any> = (props) => React.createElement('article', props);
const SectionComponent: React.FC<any> = (props) => React.createElement('section', props);
// Only map article and section, not div or p
const components = {
article: ArticleComponent,
section: SectionComponent,
};
const renderer = new Renderer({ components });
const createElementSpy = jest.spyOn(React, 'createElement');
const html = '<article><section><div><p>mixed content</p></div></section></article>';
renderer.processHtml(html);
// Only article and section should be matched as components
const articleCalls = createElementSpy.mock.calls.filter(
(call) => call[0] === ArticleComponent,
);
const sectionCalls = createElementSpy.mock.calls.filter(
(call) => call[0] === SectionComponent,
);
expect(articleCalls).toHaveLength(1);
expect(sectionCalls).toHaveLength(1);
createElementSpy.mockRestore();
});
it('should handle recursive nesting with same component type', () => {
const DivComponent: React.FC<any> = (props) => React.createElement('div', props);
const components = {
div: DivComponent,
};
const renderer = new Renderer({ components });
const createElementSpy = jest.spyOn(React, 'createElement');
const html = '<div><div><div>deeply nested divs</div></div></div>';
renderer.processHtml(html);
// Should create 3 div components
const divCalls = createElementSpy.mock.calls.filter((call) => call[0] === DivComponent);
expect(divCalls).toHaveLength(3);
createElementSpy.mockRestore();
});
it('should preserve component hierarchy in children prop', () => {
const ParentComponent: React.FC<any> = (props) => React.createElement('div', props);
const ChildComponent: React.FC<any> = (props) => React.createElement('span', props);
const GrandchildComponent: React.FC<any> = (props) => React.createElement('strong', props);
const components = {
div: ParentComponent,
span: ChildComponent,
strong: GrandchildComponent,
};
const renderer = new Renderer({ components });
const createElementSpy = jest.spyOn(React, 'createElement');
const html = '<div><span><strong>deep content</strong></span></div>';
renderer.processHtml(html);
// Verify the hierarchy is preserved in children
const parentCall = createElementSpy.mock.calls.find((call) => call[0] === ParentComponent);
expect(parentCall).toBeDefined();
expect(parentCall![1]).toHaveProperty('children');
createElementSpy.mockRestore();
});
});
describe('className merging edge cases', () => {
it('should handle undefined class attributes', () => {
const components = {
'test-component': MockComponent,
};
const renderer = new Renderer({ components });
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Test case: Only other attributes, no class or className
const html = '<test-component id="test-id" data-value="test">content</test-component>';
renderer.processHtml(html);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
id: 'test-id',
'data-value': 'test',
className: '',
}),
);
createElementSpy.mockRestore();
});
it('should handle whitespace in class attributes', () => {
const components = {
'test-component': MockComponent,
};
const renderer = new Renderer({
components,
dompurifyConfig: { ALLOWED_ATTR: ['class', 'classname'] },
});
// Mock createElement to capture props
const createElementSpy = jest.spyOn(React, 'createElement');
// Test case: Extra whitespace in class attributes
const html =
'<test-component class=" class1 class2 " classname=" existing ">content</test-component>';
renderer.processHtml(html);
expect(createElementSpy).toHaveBeenCalledWith(
MockComponent,
expect.objectContaining({
className: 'existing class1 class2',
}),
);
createElementSpy.mockRestore();
});
it('should handle classname attribute (lowercase)', () => {
const components = {