Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
okaycj committed Nov 5, 2024
1 parent 28dd918 commit ce46124
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
37 changes: 12 additions & 25 deletions packages/data/src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { enableFetchMocks } from "jest-fetch-mock";
import { ApiResponse, Child } from "./types";
import * as utils from "./utils";
import { get, getUuids, patch } from "./utils";

enableFetchMocks();

/**
* Helper function for this set of tests. This will update the current URL to
* the value provided in argument href.
*
* @param href - URL to be set.
* @returns Supplied URL.
*/
const setLocationHref = (href: string) => {
/**
* Helper function for this set of tests. This will update the current URL to
* the value provided in argument href.
*/
delete global.window.location;
global.window = Object.create(window);
global.window.location = { href };
return href;
};

test("Api get function", async () => {
const child = { id: new Date().toString() } as Child;
const data: ApiResponse<Child> = { data: child };
Expand All @@ -41,9 +24,11 @@ test("Get UUIDs from URL when study is preview", () => {
study: "1647e101-282a-4fde-a32b-4f493d14f57e",
child: "8a2b2f04-63eb-485a-8e55-7b9362368f19",
};
setLocationHref(
`https://localhost:8000/exp/studies/j/${data.study}/${data.child}/preview/`,
);
jest
.spyOn(utils, "getLocationHref")
.mockReturnValue(
`https://localhost:8000/exp/studies/j/${data.study}/${data.child}/preview/`,
);
expect(getUuids()).toEqual(data);
});

Expand All @@ -52,14 +37,16 @@ test("Get UUIDs from URL", () => {
study: "1647e101-282a-4fde-a32b-4f493d14f57e",
child: "8a2b2f04-63eb-485a-8e55-7b9362368f19",
};
setLocationHref(
`https://localhost:8000/studies/j/${data.study}/${data.child}/`,
);
jest
.spyOn(utils, "getLocationHref")
.mockReturnValue(
`https://localhost:8000/studies/j/${data.study}/${data.child}/`,
);
expect(getUuids()).toEqual(data);
});

test("Get UUIDs Error", () => {
setLocationHref("https://mit.edu");
jest.spyOn(utils, "getLocationHref").mockReturnValue("https://mit.edu");
expect(() => {
getUuids();
}).toThrow("URL is different than expected.");
Expand Down
10 changes: 9 additions & 1 deletion packages/data/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,25 @@ export const csrfToken = () => {
);
};

/**
* Wrapper for location.href. This is mainly to make testing easier.
*
* @returns Href
*/
export const getLocationHref = () => window.location.href;

/**
* Get Study and Child UUID from URL.
*
* @returns Object containing UUIDs.
*/
export const getUuids = () => {
const locationHref = window.location.href;
const locationHref = getLocationHref();
const uuids = locationHref.replace("preview/", "").split("/").slice(-3, -1);
if (locationHref.includes("studies/j/") && uuids && uuids.length === 2) {
return { study: uuids[0], child: uuids[1] };
} else {
console.log(locationHref);
throw new URLWrongError();
}
};

0 comments on commit ce46124

Please sign in to comment.