From e3ac1c1871ed2dac33c267d7471c0ca25e98aed2 Mon Sep 17 00:00:00 2001 From: SeSo Date: Wed, 11 Dec 2024 16:57:55 -0800 Subject: [PATCH] chore: fix vitest tests after using the session util Signed-off-by: SeSo --- .../tests/routes/dashboard/page.test.tsx | 47 +++++++++---------- bciers/libs/utils/src/sessionUtils.ts | 4 +- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/bciers/apps/dashboard/tests/routes/dashboard/page.test.tsx b/bciers/apps/dashboard/tests/routes/dashboard/page.test.tsx index cd0102877e..afcbfd5896 100644 --- a/bciers/apps/dashboard/tests/routes/dashboard/page.test.tsx +++ b/bciers/apps/dashboard/tests/routes/dashboard/page.test.tsx @@ -1,7 +1,12 @@ import { render, screen } from "@testing-library/react"; import { describe, expect, vi } from "vitest"; import DashboardPage from "apps/dashboard/app/dashboard/page"; -import { auth } from "@bciers/testConfig/mocks"; +import { getSessionRole } from "@bciers/utils/src/sessionUtils"; + +vi.mock("@bciers/utils/src/sessionUtils", () => ({ + getSessionRole: vi.fn(), +})); + const roles = [ "cas_admin", "cas_analyst", @@ -109,9 +114,7 @@ describe("Registration dashboard page", () => { }); it("renders the dashboard page with the correct tiles", async () => { - auth.mockReturnValueOnce({ - user: { app_role: "cas_admin" }, - }); + (getSessionRole as ReturnType).mockResolvedValue("cas_admin"); render(await DashboardPage()); @@ -126,9 +129,7 @@ describe("Registration dashboard page", () => { }); it("renders the correct links for each tile", async () => { - auth.mockReturnValueOnce({ - user: { app_role: "cas_admin" }, - }); + (getSessionRole as ReturnType).mockResolvedValue("cas_admin"); render(await DashboardPage()); @@ -204,9 +205,9 @@ describe("Registration dashboard page", () => { }); it("renders the Note component for industry_admin role", async () => { - auth.mockReturnValueOnce({ - user: { app_role: "industry_admin" }, - }); + (getSessionRole as ReturnType).mockResolvedValue( + "industry_admin", + ); render(await DashboardPage()); @@ -216,9 +217,9 @@ describe("Registration dashboard page", () => { }); it("renders the Note component for industry_user role", async () => { - auth.mockReturnValueOnce({ - user: { app_role: "industry_user" }, - }); + (getSessionRole as ReturnType).mockResolvedValue( + "industry_user", + ); render(await DashboardPage()); @@ -228,9 +229,7 @@ describe("Registration dashboard page", () => { }); it("does not render the Note component for cas_admin", async () => { - auth.mockReturnValueOnce({ - user: { app_role: "cas_admin" }, - }); + (getSessionRole as ReturnType).mockResolvedValue("cas_admin"); render(await DashboardPage()); @@ -238,9 +237,9 @@ describe("Registration dashboard page", () => { }); it("does not render the Note component for cas_analyst", async () => { - auth.mockReturnValueOnce({ - user: { app_role: "cas_analyst" }, - }); + (getSessionRole as ReturnType).mockResolvedValue( + "cas_analyst", + ); render(await DashboardPage()); @@ -248,9 +247,9 @@ describe("Registration dashboard page", () => { }); it("renders the dashboard-pending-message card for cas_pending role", async () => { - auth.mockReturnValueOnce({ - user: { app_role: "cas_pending" }, - }); + (getSessionRole as ReturnType).mockResolvedValue( + "cas_pending", + ); render(await DashboardPage()); @@ -262,9 +261,7 @@ describe("Registration dashboard page", () => { it.each(roles)( "does not render the dashboard-pending-message card for role: %s", async (role) => { - auth.mockReturnValueOnce({ - user: { app_role: role }, - }); + (getSessionRole as ReturnType).mockResolvedValue(role); render(await DashboardPage()); diff --git a/bciers/libs/utils/src/sessionUtils.ts b/bciers/libs/utils/src/sessionUtils.ts index 67fb037bb4..2ad261d041 100644 --- a/bciers/libs/utils/src/sessionUtils.ts +++ b/bciers/libs/utils/src/sessionUtils.ts @@ -12,13 +12,13 @@ const getSessionRole = async () => { return session.user.app_role as FrontEndRoles; }; -// useSessionFole is for client components +// useSessionRole is for client components const useSessionRole = () => { const session = useSession(); if (!session?.data?.user?.app_role) { throw new Error("Failed to retrieve session role"); } - return session.data.user.app_role; + return session.data.user.app_role as FrontEndRoles; }; export { getSessionRole, useSessionRole };