Skip to content

Commit 8455b0d

Browse files
committed
feat: tests for the unescapeHTML utils function
1 parent 688dc3b commit 8455b0d

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe } from "vitest";
2+
import { unescapeHTML } from "../../js/utils/unescapeHTMLentities";
3+
4+
describe("Unescape HTML entities function", () => {
5+
it("Single HTML entities decoded", () => {
6+
expect(unescapeHTML("&")).toBe("&");
7+
expect(unescapeHTML("&lt;")).toBe("<");
8+
expect(unescapeHTML("&gt;")).toBe(">");
9+
expect(unescapeHTML("&quot;")).toBe('"');
10+
expect(unescapeHTML("&#34;")).toBe('"');
11+
expect(unescapeHTML("&#39;")).toBe("'");
12+
expect(unescapeHTML("&#x2F;")).toBe("/");
13+
expect(unescapeHTML("&#x60;")).toBe("`");
14+
expect(unescapeHTML("&#x3D;")).toBe("=");
15+
});
16+
17+
it("Multiple entities in a single string decoded", () => {
18+
const input =
19+
"&lt;div class=&#34;test&#34;&gt;Hello &amp; Welcome&#x2F;&lt;&#x2F;div&gt;";
20+
const expected = '<div class="test">Hello & Welcome/</div>';
21+
expect(unescapeHTML(input)).toBe(expected);
22+
});
23+
24+
it("String unchanged if no entities present", () => {
25+
const input = "Plain text with no HTML entities";
26+
expect(unescapeHTML(input)).toBe(input);
27+
});
28+
29+
it("Empty string input handled", () => {
30+
expect(unescapeHTML("")).toBe("");
31+
});
32+
33+
it("Non-string input converted to string", () => {
34+
expect(unescapeHTML(123 as any as string)).toBe("123");
35+
expect(unescapeHTML(null as any)).toBe("null");
36+
expect(unescapeHTML(undefined as any)).toBe("undefined");
37+
expect(unescapeHTML(true as any)).toBe("true");
38+
});
39+
});

0 commit comments

Comments
 (0)