-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathformatters.test.ts
More file actions
76 lines (61 loc) · 2.36 KB
/
formatters.test.ts
File metadata and controls
76 lines (61 loc) · 2.36 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
import { describe, it, expect } from 'vitest';
import { escapeHtml, formatLargeText } from './formatters';
describe('escapeHtml', () => {
it('escapes ampersands', () => {
expect(escapeHtml('a & b')).toBe('a & b');
});
it('escapes less than', () => {
expect(escapeHtml('a < b')).toBe('a < b');
});
it('escapes greater than', () => {
expect(escapeHtml('a > b')).toBe('a > b');
});
it('escapes double quotes', () => {
expect(escapeHtml('He said "hello"')).toBe('He said "hello"');
});
it('escapes single quotes', () => {
expect(escapeHtml("It's fine")).toBe("It's fine");
});
it('escapes all special characters together', () => {
expect(escapeHtml('<script>"alert(\'xss\')&"</script>')).toBe(
'<script>"alert('xss')&"</script>'
);
});
});
describe('formatLargeText', () => {
it('returns empty string for empty input', () => {
expect(formatLargeText('')).toBe('');
});
it('wraps simple text in paragraph tags', () => {
expect(formatLargeText('Hello world')).toBe('<p>Hello world</p>');
});
it('converts single newlines to br tags', () => {
expect(formatLargeText('Line1\nLine2')).toBe('<p>Line1<br>Line2</p>');
});
it('converts double newlines to paragraph breaks with proper nesting', () => {
const result = formatLargeText('Line1\n\nLine2');
expect(result).toBe('<p>Line1</p><p class="mt-3">Line2</p>');
});
it('handles multiple paragraph breaks correctly', () => {
const result = formatLargeText('Para1\n\nPara2\n\nPara3');
expect(result).toBe('<p>Para1</p><p class="mt-3">Para2</p><p class="mt-3">Para3</p>');
});
it('escapes HTML in the input', () => {
const result = formatLargeText('<script>alert("xss")</script>');
expect(result).toContain('<script>');
expect(result).not.toContain('<script>');
});
it('formats inline code with backticks', () => {
const result = formatLargeText('Use `code` here');
expect(result).toContain('<code');
expect(result).toContain('>code</code>');
});
it('formats bold text', () => {
const result = formatLargeText('This is **bold** text');
expect(result).toContain('<strong>bold</strong>');
});
it('formats italic text', () => {
const result = formatLargeText('This is *italic* text');
expect(result).toContain('<em>italic</em>');
});
});