Skip to content

Commit

Permalink
add tests for new session storage utils
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonfarrell committed Jan 8, 2025
1 parent ac01ce0 commit 38870f2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { usePathname, useSearchParams, useRouter } from "next/navigation";
import { range } from "../view-data/utils/utils";
import classNames from "classnames";
import Link from "next/link";
import { saveToSessionStorage } from "./helpers";
import { saveToSessionStorage } from "./utils";

type EcrTableClientProps = {
data: EcrDisplay[];
Expand Down
62 changes: 62 additions & 0 deletions containers/ecr-viewer/src/app/tests/components/Utils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
saveToSessionStorage,
retrieveFromSessionStorage,
} from "../../components/utils.ts";

describe("Session Storage saving utils", () => {
beforeEach(() => {
sessionStorage.clear();
});

describe("saveToSessionStorage", () => {
it("should save a string value to session storage", () => {
const key = "Bread";
const value = "Baguette";
saveToSessionStorage(key, value);

expect(sessionStorage.getItem(key)).toBe(JSON.stringify(value));
});

it("should save an object value to session storage", () => {
const key = "testKey";
const value = { name: "Heironymous", age: 37 };
saveToSessionStorage(key, value);

expect(sessionStorage.getItem(key)).toBe(JSON.stringify(value));
});
});

describe("retrieveFromSessionStorage", () => {
it("should retrieve a string value from session storage", () => {
const key = "fruit";
const value = "Apples";
sessionStorage.setItem(key, JSON.stringify(value));

const retrievedValue = retrieveFromSessionStorage(key);
expect(retrievedValue).toBe(value);
});

it("should retrieve an object value from session storage", () => {
const key = "patient";
const value = { name: "Arabelle", age: 22 };
sessionStorage.setItem(key, JSON.stringify(value));

const retrievedValue = retrieveFromSessionStorage(key);
expect(retrievedValue).toEqual(value);
});

it("should return null if the key does not exist", () => {
const key = "nonExistentKey";
const retrievedValue = retrieveFromSessionStorage(key);

expect(retrievedValue).toBeNull();
});

it("should throw an error for invalid JSON in session storage", () => {
const key = "aKey";
sessionStorage.setItem(key, "invalid-json");

expect(() => retrieveFromSessionStorage(key)).toThrow(SyntaxError);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ exports[`EcrTable Snapshot test for EcrTableLoading should match snapshot 1`] =
</div>
</th>
<th
class="library-condition-colum"
class="library-condition-column"
data-sortable="false"
id="reportable_condition-header"
role="columnheader"
Expand Down Expand Up @@ -953,7 +953,7 @@ exports[`EcrTable should match snapshot 1`] = `
</div>
</th>
<th
class="library-condition-colum"
class="library-condition-column"
data-sortable="false"
id="reportable_condition-header"
role="columnheader"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Icon } from "@trussworks/react-uswds";
import Link from "next/link";
import classNames from "classnames";
import { env } from "next-runtime-env";
import { retrieveFromSessionStorage } from "../../components/helpers";
import { retrieveFromSessionStorage } from "../../components/utils";

interface BackButtonProps {
className?: string;
Expand Down

0 comments on commit 38870f2

Please sign in to comment.