Skip to content

Commit

Permalink
test: added test cases for getURLParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
niloofar-deriv committed Feb 27, 2024
1 parent cdbb700 commit 5be4e9d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/constants/url.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export const queryParameters = {
action: "action",
} as const;

export type queryParameters = keyof typeof queryParameters;
export type QueryParameters = keyof typeof queryParameters;
29 changes: 20 additions & 9 deletions src/utils/__test__/url.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,31 @@ describe("URLUtils.getWebsocketURL", () => {
});

describe("URLUtils.getURLParameters", () => {
beforeEach(() => {
test("returns the value for the 'lang' key", () => {
Object.defineProperty(window, "location", {
value: { search: "?lang=ES&action=test" },
value: { search: "?lang=ES" },
});

const URLParameters = URLUtils.getURLParameters("lang");
expect(URLParameters).toBe("ES");
});

test("returns the value for lang key", () => {
const result = URLUtils.getURLParameters("lang");
expect(result).toBe("ES");
test("returns the value for the 'lang' key when we have multiple query parameters", () => {
Object.defineProperty(window, "location", {
value: { search: "?lang=ES&action=test" },
});

const URLParameters = URLUtils.getURLParameters("lang");
expect(URLParameters).toBe("ES");
});

test("returns the value for action key", () => {
const result = URLUtils.getURLParameters("action");
expect(result).toBe("test");
test("returns null when we don't have value for the passed key", () => {
Object.defineProperty(window, "location", {
value: { search: "?lang=ES" },
});

const URLParameters = URLUtils.getURLParameters("action");
expect(URLParameters).toBeNull();
});
});

Expand All @@ -234,7 +245,7 @@ describe("URLUtils.normalizePath", () => {
});

test("removes invalid characters", () => {
const result = URLUtils.normalizePath("inval!d_c#haracters");
const result = URLUtils.normalizePath("inval!d_characters");
expect(result).toBe("invald_characters");
});

Expand Down
13 changes: 7 additions & 6 deletions src/utils/url.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LocalStorageConstants, AppIDConstants, URLConstants } from "../constants";
import { queryParameters } from "../constants/url.constants";
import { QueryParameters } from "../constants/url.constants";
import { getActiveLoginid, getAppId, getEnvironmentFromLoginid } from "./websocket.utils";

/**
Expand Down Expand Up @@ -113,14 +113,15 @@ export const getWebsocketURL = () => {
};

/**
* Extracts query parameters from the URL by parsing the current window's URL search parameters for the specified keys.
* It returns the query parameters associated with the given keys.
* Extracts query parameters from the URL by parsing the current window's URL search parameters for the specified key.
* It returns the query parameters associated with the given key.
*
* @returns {Array<string | null>} An array of strings containing query parameters associated with the given key.
* @param {QueryParameters} key - The query parameter we want. (you can see all of them in the URLConstants.queryParameters)
* @returns {string | null} A string containing query parameter associated with the given key.
*/
export const getURLParameters = (keys: queryParameters[]) => {
export const getURLParameters = (key: QueryParameters) => {
const searchParams = new URLSearchParams(window.location.search);
return keys.map((key) => searchParams.get(key));
return searchParams.get(key);
};

/**
Expand Down

0 comments on commit 5be4e9d

Please sign in to comment.