mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-13 20:36:35 +00:00
add unit tests for common/url/search-params.ts (#26115)
This commit is contained in:
parent
5cf8b39703
commit
8d95f0d95d
48
test/common/url/search-params.test.ts
Normal file
48
test/common/url/search-params.test.ts
Normal file
@ -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")
|
||||
);
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user