From 0d7d68f8b4b17e86551fe3f16f541f4b2c4305b2 Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 24 Sep 2025 12:30:58 +0200 Subject: [PATCH] test: add unit tests for escapeHTML --- frontend/__tests__/utils/misc.spec.ts | 42 ++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/frontend/__tests__/utils/misc.spec.ts b/frontend/__tests__/utils/misc.spec.ts index fc0a29781..893282946 100644 --- a/frontend/__tests__/utils/misc.spec.ts +++ b/frontend/__tests__/utils/misc.spec.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "vitest"; -import { getErrorMessage, isObject } from "../../src/ts/utils/misc"; +import { getErrorMessage, isObject, escapeHTML } from "../../src/ts/utils/misc"; import { getLanguageDisplayString, removeLanguageSize, @@ -123,6 +123,46 @@ describe("misc.ts", () => { }); }); + describe("escapeHTML", () => { + it("should escape HTML characters correctly", () => { + const tests = [ + { + input: "hello world", + expected: "hello world", + }, + { + input: "", + expected: "<script>alert('xss')</script>", + }, + { + input: 'Hello "world" & friends', + expected: "Hello "world" & friends", + }, + { + input: "Click `here` to continue", + expected: "Click `here` to continue", + }, + { + input: null, + expected: null, + }, + { + input: undefined, + expected: undefined, + }, + { + input: "", + expected: "", + }, + ]; + + tests.forEach((test) => { + const result = escapeHTML(test.input); + expect(result).toBe(test.expected); + }); + }); + }); + describe("getErrorMesssage", () => { it("should correctly get the error message", () => { const tests = [