Skip to content

Commit

Permalink
chore: fix vitest tests after using the session util
Browse files Browse the repository at this point in the history
Signed-off-by: SeSo <[email protected]>
  • Loading branch information
Sepehr-Sobhani committed Dec 12, 2024
1 parent dcb143e commit e3ac1c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
47 changes: 22 additions & 25 deletions bciers/apps/dashboard/tests/routes/dashboard/page.test.tsx
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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<typeof vi.fn>).mockResolvedValue("cas_admin");

render(await DashboardPage());

Expand All @@ -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<typeof vi.fn>).mockResolvedValue("cas_admin");

render(await DashboardPage());

Expand Down Expand Up @@ -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<typeof vi.fn>).mockResolvedValue(
"industry_admin",
);

render(await DashboardPage());

Expand All @@ -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<typeof vi.fn>).mockResolvedValue(
"industry_user",
);

render(await DashboardPage());

Expand All @@ -228,29 +229,27 @@ 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<typeof vi.fn>).mockResolvedValue("cas_admin");

render(await DashboardPage());

expect(screen.queryByTestId("note")).not.toBeInTheDocument();
});

it("does not render the Note component for cas_analyst", async () => {
auth.mockReturnValueOnce({
user: { app_role: "cas_analyst" },
});
(getSessionRole as ReturnType<typeof vi.fn>).mockResolvedValue(
"cas_analyst",
);

render(await DashboardPage());

expect(screen.queryByTestId("note")).not.toBeInTheDocument();
});

it("renders the dashboard-pending-message card for cas_pending role", async () => {
auth.mockReturnValueOnce({
user: { app_role: "cas_pending" },
});
(getSessionRole as ReturnType<typeof vi.fn>).mockResolvedValue(
"cas_pending",
);

render(await DashboardPage());

Expand All @@ -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<typeof vi.fn>).mockResolvedValue(role);

render(await DashboardPage());

Expand Down
4 changes: 2 additions & 2 deletions bciers/libs/utils/src/sessionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit e3ac1c1

Please sign in to comment.