From 8d95f0d95d83e6b8a59c2b2e01f2765d63ebe54b Mon Sep 17 00:00:00 2001 From: Christoph <15820871+chrisv-dev@users.noreply.github.com> Date: Wed, 9 Jul 2025 13:11:28 +0200 Subject: [PATCH] add unit tests for common/url/search-params.ts (#26115) --- test/common/url/search-params.test.ts | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/common/url/search-params.test.ts diff --git a/test/common/url/search-params.test.ts b/test/common/url/search-params.test.ts new file mode 100644 index 0000000000..f77d88bb59 --- /dev/null +++ b/test/common/url/search-params.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it, vi, afterEach } from "vitest"; + +import { + addSearchParam, + createSearchParam, + extractSearchParam, + extractSearchParamsObject, + removeSearchParam, +} from "../../../src/common/url/search-params"; + +const sortQueryString = (querystring: string): string => + querystring.split("&").sort().join("&"); + +vi.mock("../../../src/common/dom/get_main_window", () => ({ + mainWindow: { location: { search: "?param1=ab+c¶m2" } }, +})); + +afterEach(() => { + vi.resetAllMocks(); +}); + +describe("Search Params Tests", () => { + it("should extract all search params from window object", () => { + expect(extractSearchParamsObject()).toEqual({ param1: "ab c", param2: "" }); + }); + + it("should return value for specified search param from window object", () => { + expect(extractSearchParam("param1")).toEqual("ab c"); + }); + + it("should create query string from given object", () => { + expect( + sortQueryString(createSearchParam({ param1: "ab c", param2: "" })) + ).toEqual(sortQueryString("param1=ab+c¶m2=")); + }); + + it("should return query string which combines provided param object and window.location.search", () => { + expect( + sortQueryString(addSearchParam({ param4: "", param3: "x y" })) + ).toEqual(sortQueryString("param1=ab+c¶m2=¶m3=x+y¶m4=")); + }); + + it("should return query string from window.location.search but remove the provided param from it", () => { + expect(sortQueryString(removeSearchParam("param2"))).toEqual( + sortQueryString("param1=ab+c") + ); + }); +});