Skip to content

Commit aa4dba6

Browse files
committed
feat: tests for some functions from the url.ts
1 parent fc874d8 commit aa4dba6

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

vis/js/utils/url.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export const removeQueryParams = (...keys: string[]) => {
2323
window.history.pushState("", "", url.pathname + url.search);
2424
};
2525

26-
const addRemoveQueryParams = (paramsToAdd: Record<string, string>, paramsToRemove: string[]) => {
26+
export const addRemoveQueryParams = (
27+
paramsToAdd: Record<string, string>,
28+
paramsToRemove: string[]
29+
) => {
2730
const url = new URL(window.location.href);
2831

2932
Object.keys(paramsToAdd).forEach((key) => {
@@ -39,7 +42,7 @@ const addRemoveQueryParams = (paramsToAdd: Record<string, string>, paramsToRemov
3942

4043
/**
4144
* Changes page url based on the current Redux action.
42-
*
45+
*
4346
* @param {Object} action the Redux action object
4447
*/
4548
export const handleUrlAction = (action: any) => {

vis/test/utils/url.test.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2+
import {
3+
addQueryParam,
4+
addRemoveQueryParams,
5+
removeQueryParams,
6+
} from "../../js/utils/url";
7+
8+
let currentUrl: URL;
9+
10+
const mockPushState = vi.fn((_: any, __: any, url: string) => {
11+
currentUrl = new URL("https://example.com" + url);
12+
});
13+
14+
const setWindowHref = (href: string) => {
15+
currentUrl = new URL(href);
16+
Object.defineProperty(window, "location", {
17+
configurable: true,
18+
value: currentUrl,
19+
});
20+
};
21+
22+
beforeEach(() => {
23+
currentUrl = new URL(
24+
"https://example.com/?first_parameter=1&second_parameter=2"
25+
);
26+
Object.defineProperty(window, "location", {
27+
configurable: true,
28+
value: currentUrl,
29+
});
30+
31+
vi.stubGlobal("history", {
32+
pushState: mockPushState,
33+
});
34+
});
35+
36+
afterEach(() => {
37+
vi.restoreAllMocks();
38+
});
39+
40+
describe("Tests for query param functions", () => {
41+
beforeEach(() => {
42+
setWindowHref("https://example.com/?first_parameter=1&second_parameter=2");
43+
});
44+
45+
describe("The addQueryParam function", () => {
46+
it("Adding a new parameter correctly", () => {
47+
addQueryParam("external_parameter", "3");
48+
expect(currentUrl.searchParams.get("external_parameter")).toBe("3");
49+
expect(mockPushState).toHaveBeenCalled();
50+
});
51+
52+
it("Rewrite existing parameter", () => {
53+
addQueryParam("first_parameter", "updated");
54+
expect(currentUrl.searchParams.get("first_parameter")).toBe("updated");
55+
});
56+
57+
it("Works with empty string", () => {
58+
addQueryParam("newParam", "");
59+
expect(currentUrl.searchParams.get("newParam")).toBe("");
60+
});
61+
});
62+
63+
describe("The removeQueryParams function", () => {
64+
it("Deletes parameters", () => {
65+
removeQueryParams("first_parameter", "second_parameter");
66+
expect(currentUrl.searchParams.get("first_parameter")).toBeNull();
67+
expect(currentUrl.searchParams.get("second_parameter")).toBeNull();
68+
});
69+
70+
it("Don't crashes when parameter is not exists", () => {
71+
removeQueryParams("not_there");
72+
expect(currentUrl.searchParams.get("first_parameter")).toBe("1");
73+
});
74+
75+
it("Works without arguments", () => {
76+
removeQueryParams();
77+
expect(currentUrl.search).toBe("?first_parameter=1&second_parameter=2");
78+
});
79+
});
80+
81+
describe("The addRemoveQueryParams function", () => {
82+
it("Adding and removing parameters", () => {
83+
setWindowHref("https://example.com/?x=9&y=8");
84+
addRemoveQueryParams({ a: "1", b: "2" }, ["x"]);
85+
86+
expect(currentUrl.searchParams.get("a")).toBe("1");
87+
expect(currentUrl.searchParams.get("b")).toBe("2");
88+
expect(currentUrl.searchParams.get("x")).toBeNull();
89+
expect(currentUrl.searchParams.get("y")).toBe("8");
90+
});
91+
92+
it("Works with empty arguments", () => {
93+
setWindowHref("https://example.com/?keep=1");
94+
addRemoveQueryParams({}, []);
95+
expect(currentUrl.searchParams.get("keep")).toBe("1");
96+
});
97+
98+
it("Replace and remove existing parameters", () => {
99+
setWindowHref(
100+
"https://example.com/?first_parameter=second_parameter&remove=me"
101+
);
102+
addRemoveQueryParams({ first_parameter: "new" }, ["remove"]);
103+
expect(currentUrl.searchParams.get("first_parameter")).toBe("new");
104+
expect(currentUrl.searchParams.has("remove")).toBe(false);
105+
});
106+
});
107+
});

0 commit comments

Comments
 (0)