Skip to content

Commit 0d7d68f

Browse files
committed
test: add unit tests for escapeHTML
1 parent 5da0713 commit 0d7d68f

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

frontend/__tests__/utils/misc.spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import { getErrorMessage, isObject } from "../../src/ts/utils/misc";
2+
import { getErrorMessage, isObject, escapeHTML } from "../../src/ts/utils/misc";
33
import {
44
getLanguageDisplayString,
55
removeLanguageSize,
@@ -123,6 +123,46 @@ describe("misc.ts", () => {
123123
});
124124
});
125125

126+
describe("escapeHTML", () => {
127+
it("should escape HTML characters correctly", () => {
128+
const tests = [
129+
{
130+
input: "hello world",
131+
expected: "hello world",
132+
},
133+
{
134+
input: "<script>alert('xss')</script>",
135+
expected: "&lt;script&gt;alert(&#39;xss&#39;)&lt;&#x2F;script&gt;",
136+
},
137+
{
138+
input: 'Hello "world" & friends',
139+
expected: "Hello &quot;world&quot; &amp; friends",
140+
},
141+
{
142+
input: "Click `here` to continue",
143+
expected: "Click &#x60;here&#x60; to continue",
144+
},
145+
{
146+
input: null,
147+
expected: null,
148+
},
149+
{
150+
input: undefined,
151+
expected: undefined,
152+
},
153+
{
154+
input: "",
155+
expected: "",
156+
},
157+
];
158+
159+
tests.forEach((test) => {
160+
const result = escapeHTML(test.input);
161+
expect(result).toBe(test.expected);
162+
});
163+
});
164+
});
165+
126166
describe("getErrorMesssage", () => {
127167
it("should correctly get the error message", () => {
128168
const tests = [

0 commit comments

Comments
 (0)