Skip to content

Commit 9436441

Browse files
fix: used ts instead of js
1 parent 18f0db9 commit 9436441

File tree

2 files changed

+91
-21
lines changed

2 files changed

+91
-21
lines changed

src/schedule-and-details/introducing-section/contentTypeUtils.test.js renamed to src/schedule-and-details/introducing-section/contentTypeUtils.test.ts

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
/**
22
* Tests for content type detection utilities
33
*/
4-
import { containsHtml, determineEditorType } from './contentTypeUtils';
4+
import { containsHtml, determineEditorType, type EditorType } from './contentTypeUtils';
55

66
describe('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 &amp; includes shipping &ndash; 50% off!'],
110120
['mixed content', 'Introduction <p>Main content with &copy; 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 &amp; 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
});

src/schedule-and-details/introducing-section/contentTypeUtils.js renamed to src/schedule-and-details/introducing-section/contentTypeUtils.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,36 @@
33
* determining appropriate editor type for TinyMCE editor
44
*/
55

6+
// Define the supported editor types
7+
export type EditorType = 'text' | 'html';
8+
69
/**
710
* Detects if content contains HTML tags
8-
* @param {string} content - The content to analyze
9-
* @returns {boolean} - True if content contains HTML tags
11+
* @param content - The content to analyze
12+
* @returns True if content contains HTML tags
1013
*/
11-
export const containsHtml = (content) => {
12-
if (!content || typeof content !== 'string') {
14+
export const containsHtml = (content: string | null): boolean => {
15+
if (!content) {
1316
return false;
1417
}
1518

1619
// Check for common HTML patterns
17-
const htmlPatterns = [
20+
const htmlPatterns: RegExp[] = [
1821
/<\/?[a-z][\s\S]*>/i, // HTML tags
1922
/&[a-z]+;/i, // HTML entities
2023
/&#\d+;/, // Numeric entities
2124
];
2225

23-
return htmlPatterns.some(pattern => pattern.test(content));
26+
return htmlPatterns.some((pattern) => pattern.test(content));
2427
};
2528

2629
/**
2730
* Determines the appropriate editor type based on content analysis
28-
* @param {string} content - The content to analyze
29-
* @returns {string} - The recommended editor type ('text' or 'html')
31+
* @param content - The content to analyze
32+
* @returns The recommended editor type ('text' or 'html')
3033
*/
31-
export const determineEditorType = (content) => {
32-
if (!content || typeof content !== 'string') {
34+
export const determineEditorType = (content: string | null): EditorType => {
35+
if (!content) {
3336
return 'text';
3437
}
3538

0 commit comments

Comments
 (0)