From 38870f2c5d3e7c02d2ee46fc50fb92792da25832 Mon Sep 17 00:00:00 2001 From: Gordon Farrell Date: Wed, 8 Jan 2025 14:47:49 -0700 Subject: [PATCH] add tests for new session storage utils --- .../src/app/components/EcrTableClient.tsx | 2 +- .../app/components/{helpers.ts => utils.ts} | 0 .../src/app/tests/components/Utils.test.tsx | 62 +++++++++++++++++++ .../__snapshots__/EcrTable.test.tsx.snap | 4 +- .../app/view-data/components/BackButton.tsx | 2 +- 5 files changed, 66 insertions(+), 4 deletions(-) rename containers/ecr-viewer/src/app/components/{helpers.ts => utils.ts} (100%) create mode 100644 containers/ecr-viewer/src/app/tests/components/Utils.test.tsx diff --git a/containers/ecr-viewer/src/app/components/EcrTableClient.tsx b/containers/ecr-viewer/src/app/components/EcrTableClient.tsx index 06fb4fc5d..5732c0a59 100644 --- a/containers/ecr-viewer/src/app/components/EcrTableClient.tsx +++ b/containers/ecr-viewer/src/app/components/EcrTableClient.tsx @@ -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[]; diff --git a/containers/ecr-viewer/src/app/components/helpers.ts b/containers/ecr-viewer/src/app/components/utils.ts similarity index 100% rename from containers/ecr-viewer/src/app/components/helpers.ts rename to containers/ecr-viewer/src/app/components/utils.ts diff --git a/containers/ecr-viewer/src/app/tests/components/Utils.test.tsx b/containers/ecr-viewer/src/app/tests/components/Utils.test.tsx new file mode 100644 index 000000000..5886ea266 --- /dev/null +++ b/containers/ecr-viewer/src/app/tests/components/Utils.test.tsx @@ -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); + }); + }); +}); diff --git a/containers/ecr-viewer/src/app/tests/components/__snapshots__/EcrTable.test.tsx.snap b/containers/ecr-viewer/src/app/tests/components/__snapshots__/EcrTable.test.tsx.snap index 472b0718c..72dbb6624 100644 --- a/containers/ecr-viewer/src/app/tests/components/__snapshots__/EcrTable.test.tsx.snap +++ b/containers/ecr-viewer/src/app/tests/components/__snapshots__/EcrTable.test.tsx.snap @@ -74,7 +74,7 @@ exports[`EcrTable Snapshot test for EcrTableLoading should match snapshot 1`] =