From d44cd6ff310cbe860442ce11bf9657a5c89f854b Mon Sep 17 00:00:00 2001 From: asharonbaltazar <58940073+asharonbaltazar@users.noreply.github.com> Date: Mon, 23 Sep 2024 10:13:35 -0400 Subject: [PATCH] test(ui): write tests for `Profile` (#786) * test: write tests for `Profile` * chore: add test for `undefined` states --- react-app/src/features/profile/index.tsx | 48 +++++++----- .../src/features/profile/profile.test.tsx | 74 +++++++++++++++++++ 2 files changed, 102 insertions(+), 20 deletions(-) create mode 100644 react-app/src/features/profile/profile.test.tsx diff --git a/react-app/src/features/profile/index.tsx b/react-app/src/features/profile/index.tsx index 4b48f8403..fb5a41d0b 100644 --- a/react-app/src/features/profile/index.tsx +++ b/react-app/src/features/profile/index.tsx @@ -4,26 +4,35 @@ import { RoleDescriptionStrings } from "shared-types"; import { convertStateAbbrToFullName } from "@/utils"; import config from "@/config"; -export const Profile = () => { - const { data } = useGetUser(); - const stateAbbreviations = data?.user?.["custom:state"]; - const fullStateNames = stateAbbreviations - ?.split(",") - .map((abbr) => convertStateAbbrToFullName(abbr)) +const getRoleDescriptionsFromUser = (roles: string | undefined) => { + if (roles === undefined) { + return ""; + } + + return roles + .split(",") + .map((role) => RoleDescriptionStrings[role]) .join(", "); +}; + +const getFullStateNamesFromUser = (states: string | undefined) => { + if (states === undefined) { + return ""; + } - // Returns comma-separated string of user role descriptions: - function rolesDescriptions(roles: string | undefined) { - const rolesArray: string[] | undefined = roles?.split(","); + return states.split(",").map(convertStateAbbrToFullName).join(", "); +}; - const descriptiveRolesArray = rolesArray?.map((role) => { - return RoleDescriptionStrings[role]; - }); +export const Profile = () => { + const { data: userData } = useGetUser(); - if (descriptiveRolesArray) { - return descriptiveRolesArray.join(", "); - } - } + const fullStateNames = getFullStateNamesFromUser( + userData.user["custom:state"], + ); + + const userRoles = getRoleDescriptionsFromUser( + userData.user["custom:cms-roles"], + ); return ( <> @@ -32,7 +41,6 @@ export const Profile = () => {
- {/* using bg-sky-50 for a11y compliance */}
{

Full Name

- {data?.user?.given_name} {data?.user?.family_name} + {userData?.user?.given_name} {userData?.user?.family_name}

Role

-

{rolesDescriptions(data?.user?.["custom:cms-roles"])}

+

{userRoles}

Email

-

{data?.user?.email}

+

{userData?.user?.email}

diff --git a/react-app/src/features/profile/profile.test.tsx b/react-app/src/features/profile/profile.test.tsx new file mode 100644 index 000000000..187408cc8 --- /dev/null +++ b/react-app/src/features/profile/profile.test.tsx @@ -0,0 +1,74 @@ +import { render, screen } from "@testing-library/react"; +import { describe, vi, test, expect } from "vitest"; +import { Profile } from "."; +import * as api from "@/api"; + +vi.mock("@/api", async (importOriginals) => ({ + ...((await importOriginals()) as object), + useGetUser: vi.fn(() => ({ + data: { + user: { + "custom:state": "MD,OH,NY", + "custom:cms-roles": "onemac-micro-statesubmitter", + given_name: "George", + family_name: "Harrison", + email: "george@example.com", + }, + }, + })), +})); + +describe("Profile", () => { + test("renders state names", () => { + render(); + + const states = screen.getByText("Maryland, Ohio, New York"); + + expect(states).toBeInTheDocument(); + }); + + test("renders nothing if user has no states", () => { + vi.spyOn(api, "useGetUser").mockImplementationOnce(() => ({ + data: { + // @ts-expect-error - avoid mocking whole user object for test + user: { + "custom:state": undefined, + "custom:cms-roles": "onemac-micro-statesubmitter", + given_name: "George", + family_name: "Harrison", + email: "george@example.com", + }, + }, + })); + + render(); + + const accessGranted = screen.queryByText("Access Granted"); + + expect(accessGranted).not.toBeInTheDocument(); + }); + + test("renders roles", () => { + render(); + + const states = screen.getByText("State Submitter"); + + expect(states).toBeInTheDocument(); + }); + + test("renders full name", () => { + render(); + + const states = screen.getByText("George Harrison"); + + expect(states).toBeInTheDocument(); + }); + + test("renders email", () => { + render(); + + const states = screen.getByText("george@example.com"); + + expect(states).toBeInTheDocument(); + }); +});