11/**
22 * Tests for content type detection utilities
33 */
4- import { containsHtml , determineEditorType } from './contentTypeUtils' ;
4+ import { containsHtml , determineEditorType , type EditorType } from './contentTypeUtils' ;
55
66describe ( 'contentTypeUtils' , ( ) => {
77 describe ( 'containsHtml' , ( ) => {
88 describe ( 'should return false for non-HTML content' , ( ) => {
99 test . each ( [
1010 [ 'empty string' , '' ] ,
1111 [ 'null' , null ] ,
12- [ 'undefined' , undefined ] ,
13- [ 'number' , 123 ] ,
1412 [ 'plain text' , 'This is just plain text' ] ,
1513 [ 'text with special characters' , 'Text with @#$%^&*()' ] ,
1614 [ 'text with quotes' , 'Text with "quotes" and \'apostrophes\'' ] ,
1715 [ 'text with newlines' , 'Line 1\nLine 2\nLine 3' ] ,
1816 [ 'text with angle brackets but not HTML' , '5 < 10 and 10 > 5' ] ,
1917 [ 'mathematical expressions' , 'x = y < z > w' ] ,
20- ] ) ( 'for %s' , ( description , input ) => {
18+ ] as const ) ( 'for %s' , ( _description : string , input : string | null ) => {
2119 expect ( containsHtml ( input ) ) . toBe ( false ) ;
2220 } ) ;
2321 } ) ;
@@ -65,25 +63,37 @@ describe('contentTypeUtils', () => {
6563 // Edge cases
6664 [ 'unclosed tag' , '<p>Unclosed paragraph' ] ,
6765 [ 'tag with newlines' , '<p>\nMultiline\ncontent\n</p>' ] ,
68- ] ) ( 'for %s' , ( description , input ) => {
66+ ] as const ) ( 'for %s' , ( _description : string , input : string ) => {
6967 expect ( containsHtml ( input ) ) . toBe ( true ) ;
7068 } ) ;
7169 } ) ;
70+
71+ describe ( 'edge cases' , ( ) => {
72+ test ( 'should handle very long content' , ( ) => {
73+ const longText = 'a' . repeat ( 10000 ) ;
74+ expect ( containsHtml ( longText ) ) . toBe ( false ) ;
75+
76+ const longHtml = `<p>${ longText } </p>` ;
77+ expect ( containsHtml ( longHtml ) ) . toBe ( true ) ;
78+ } ) ;
79+
80+ test ( 'should handle content with only whitespace' , ( ) => {
81+ expect ( containsHtml ( ' \n\t ' ) ) . toBe ( false ) ;
82+ } ) ;
83+ } ) ;
7284 } ) ;
7385
7486 describe ( 'determineEditorType' , ( ) => {
7587 describe ( 'should return "text" for non-HTML content' , ( ) => {
7688 test . each ( [
7789 [ 'empty string' , '' ] ,
7890 [ 'null' , null ] ,
79- [ 'undefined' , undefined ] ,
80- [ 'number' , 123 ] ,
8191 [ 'plain text' , 'This is just plain text content' ] ,
8292 [ 'long plain text' , 'Lorem ipsum ' . repeat ( 100 ) ] ,
8393 [ 'text with special chars' , 'Email: [email protected] , Phone: (555) 123-4567' ] , 8494 [ 'mathematical content' , '2 + 2 = 4, x < y, a > b' ] ,
8595 [ 'code-like content' , 'function() { return value < threshold; }' ] ,
86- ] ) ( 'for %s' , ( description , input ) => {
96+ ] as const ) ( 'for %s' , ( _description : string , input : string | null ) => {
8797 expect ( determineEditorType ( input ) ) . toBe ( 'text' ) ;
8898 } ) ;
8999 } ) ;
@@ -108,7 +118,7 @@ describe('contentTypeUtils', () => {
108118 // HTML entities
109119 [ 'content with entities' , 'Price: $100 & includes shipping – 50% off!' ] ,
110120 [ 'mixed content' , 'Introduction <p>Main content with © symbol</p> conclusion' ] ,
111- ] ) ( 'for %s' , ( description , input ) => {
121+ ] as const ) ( 'for %s' , ( _description : string , input : string ) => {
112122 expect ( determineEditorType ( input ) ) . toBe ( 'html' ) ;
113123 } ) ;
114124 } ) ;
@@ -159,8 +169,65 @@ describe('contentTypeUtils', () => {
159169 test ( 'should handle empty or minimal content' , ( ) => {
160170 expect ( determineEditorType ( '' ) ) . toBe ( 'text' ) ;
161171 expect ( determineEditorType ( ' ' ) ) . toBe ( 'text' ) ;
162- expect ( determineEditorType ( null ) ) . toBe ( 'text' ) ;
163- expect ( determineEditorType ( undefined ) ) . toBe ( 'text' ) ;
172+ expect ( determineEditorType ( null as any ) ) . toBe ( 'text' ) ;
173+ expect ( determineEditorType ( undefined as any ) ) . toBe ( 'text' ) ;
174+ } ) ;
175+ } ) ;
176+
177+ describe ( 'type safety' , ( ) => {
178+ test ( 'should return correct EditorType' , ( ) => {
179+ const result : EditorType = determineEditorType ( '<p>Test</p>' ) ;
180+ expect ( result ) . toBe ( 'html' ) ;
181+
182+ const result2 : EditorType = determineEditorType ( 'Plain text' ) ;
183+ expect ( result2 ) . toBe ( 'text' ) ;
184+ } ) ;
185+ } ) ;
186+
187+ describe ( 'performance considerations' , ( ) => {
188+ test ( 'should handle very large content efficiently' , ( ) => {
189+ const largeContent = 'This is a large text content. ' . repeat ( 1000 ) ;
190+ const start = Date . now ( ) ;
191+ const result = determineEditorType ( largeContent ) ;
192+ const end = Date . now ( ) ;
193+
194+ expect ( result ) . toBe ( 'text' ) ;
195+ expect ( end - start ) . toBeLessThan ( 100 ) ; // Should complete in under 100ms
196+ } ) ;
197+
198+ test ( 'should handle large HTML content efficiently' , ( ) => {
199+ const largeHtml = `<div>${ '<p>Paragraph content. </p>' . repeat ( 1000 ) } </div>` ;
200+ const start = Date . now ( ) ;
201+ const result = determineEditorType ( largeHtml ) ;
202+ const end = Date . now ( ) ;
203+
204+ expect ( result ) . toBe ( 'html' ) ;
205+ expect ( end - start ) . toBeLessThan ( 100 ) ; // Should complete in under 100ms
206+ } ) ;
207+ } ) ;
208+ } ) ;
209+
210+ describe ( 'function integration' , ( ) => {
211+ test ( 'containsHtml and determineEditorType should be consistent' , ( ) => {
212+ const testCases : Array < unknown > = [
213+ 'Plain text' ,
214+ '<p>HTML content</p>' ,
215+ 'Text with & entities' ,
216+ '' ,
217+ null ,
218+ undefined ,
219+ '<div><h1>Complex</h1><p>HTML</p></div>' ,
220+ ] ;
221+
222+ testCases . forEach ( ( content ) => {
223+ const hasHtml = containsHtml ( content as any ) ;
224+ const editorType = determineEditorType ( content as any ) ;
225+
226+ if ( hasHtml ) {
227+ expect ( editorType ) . toBe ( 'html' ) ;
228+ } else {
229+ expect ( editorType ) . toBe ( 'text' ) ;
230+ }
164231 } ) ;
165232 } ) ;
166233 } ) ;
0 commit comments