Skip to content

Commit 617082d

Browse files
committed
test: add unit tests for escapeText function to validate special character handling
1 parent 896b8e3 commit 617082d

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, expect, it } from "vitest";
2+
import { escapeText } from "./index";
3+
4+
describe("escapeText", () => {
5+
it("should handle empty string", () => {
6+
expect(escapeText("")).toBe("");
7+
});
8+
9+
it("should return unchanged string when no special characters present", () => {
10+
expect(escapeText("hello world")).toBe("hello world");
11+
});
12+
13+
it("should escape single less than symbol", () => {
14+
expect(escapeText("<")).toBe("&lt;");
15+
});
16+
17+
it("should escape single ampersand", () => {
18+
expect(escapeText("&")).toBe("&amp;");
19+
});
20+
21+
it("should escape multiple less than symbols", () => {
22+
expect(escapeText("a < b < c")).toBe("a &lt; b &lt; c");
23+
});
24+
25+
it("should escape multiple ampersands", () => {
26+
expect(escapeText("a & b & c")).toBe("a &amp; b &amp; c");
27+
});
28+
29+
it("should escape mixed special characters", () => {
30+
expect(escapeText("a < b & c")).toBe("a &lt; b &amp; c");
31+
});
32+
33+
it("should handle special characters at string boundaries", () => {
34+
expect(escapeText("<start>&end")).toBe("&lt;start>&amp;end");
35+
});
36+
37+
it("should handle HTML-like content", () => {
38+
expect(escapeText("<div>&nbsp;</div>")).toBe("&lt;div>&amp;nbsp;&lt;/div>");
39+
});
40+
41+
it("should handle consecutive special characters", () => {
42+
expect(escapeText("<<&&")).toBe("&lt;&lt;&amp;&amp;");
43+
});
44+
45+
it("should handle long text with multiple special characters", () => {
46+
const input = "This is a <test> with multiple & special < characters & more";
47+
const expected = "This is a &lt;test> with multiple &amp; special &lt; characters &amp; more";
48+
expect(escapeText(input)).toBe(expected);
49+
});
50+
51+
it("should handle special characters with surrounding whitespace", () => {
52+
expect(escapeText(" < & ")).toBe(" &lt; &amp; ");
53+
});
54+
55+
it("should maintain other special characters unchanged", () => {
56+
expect(escapeText("\"'!@#$%^*()_+")).toBe("\"'!@#$%^*()_+");
57+
});
58+
59+
it("should handle unicode characters correctly", () => {
60+
expect(escapeText("→ < & →")).toBe("→ &lt; &amp; →");
61+
});
62+
});

0 commit comments

Comments
 (0)