Skip to content

Commit

Permalink
feat: added test use session active (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran McDade authored and Fran McDade committed Nov 6, 2024
1 parent 8f06f8e commit 7ab2c8f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
21 changes: 21 additions & 0 deletions tests/transformRoute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { transformRoute } from "../src/hooks/authentication/session/useSessionActive";

describe("transformRoute", () => {
it("should return the first non-login route without the inactivity param", () => {
const routes = ["/login", "/route1?inactivityTimeout=true", "/route2"];
const result = transformRoute(routes);
expect(result).toBe("/route1");
});

it("should remove the inactivity param from the route", () => {
const routes = ["/route1?inactivityTimeout=true"];
const result = transformRoute(routes);
expect(result).toBe("/route1");
});

it("should return undefined if all routes are login routes", () => {
const routes = ["/login"];
const result = transformRoute(routes);
expect(result).toBeUndefined();
});
});
6 changes: 2 additions & 4 deletions tests/useRouteHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import Router, { NextRouter } from "next/router";
const ROOT_PATH = "/";
const ROUTES = ["/route1", "/route2", "/route3", "/route4"];

jest.unstable_mockModule("next/router", async () => {
const original =
jest.requireActual<typeof import("next/router")>("next/router");
jest.unstable_mockModule("next/router", () => {
return {
...original,
...jest.requireActual<typeof import("next/router")>("next/router"),
useRouter: jest.fn(),
};
});
Expand Down
74 changes: 74 additions & 0 deletions tests/useSessionActive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { jest } from "@jest/globals";
import { renderHook } from "@testing-library/react";
import { TransformRouteFn } from "../src/hooks/useRouteHistory";
import {
AUTH_STATUS,
AuthState,
} from "../src/providers/authentication/auth/types";

const AUTH_STATE_AUTHENTICATED_SETTLED: AuthState = {
isAuthenticated: true,
status: AUTH_STATUS.SETTLED,
};

const AUTH_STATE_PENDING: AuthState = {
isAuthenticated: false,
status: AUTH_STATUS.PENDING,
};

const AUTH_STATE_UNAUTHENTICATED_SETTLED: AuthState = {
isAuthenticated: false,
status: AUTH_STATUS.SETTLED,
};

const ROOT_PATH = "/";
const ROUTES = ["/login", "/route1", "/route2"];

jest.unstable_mockModule("next/router", () => {
return {
...jest.requireActual<typeof import("next/router")>("next/router"),
default: {
push: jest.fn(),
},
};
});
jest.unstable_mockModule("../src/hooks/useRouteHistory", () => ({
useRouteHistory: jest.fn(),
}));

const Router = (await import("next/router")).default;
const { useRouteHistory } = await import("../src/hooks/useRouteHistory");
const { useSessionActive } = await import(
"../src/hooks/authentication/session/useSessionActive"
);

const MOCK_USE_ROUTE_HISTORY = useRouteHistory as jest.MockedFunction<
typeof useRouteHistory
>;

describe("useSessionActive", () => {
beforeEach(() => {
MOCK_USE_ROUTE_HISTORY.mockReset();
MOCK_USE_ROUTE_HISTORY.mockReturnValue({
callbackUrl: jest.fn(
(transformFn?: TransformRouteFn | undefined) =>
transformFn?.(ROUTES) ?? ROOT_PATH
),
});
});

test("does not redirect if auth status is PENDING", () => {
renderHook(() => useSessionActive(AUTH_STATE_PENDING));
expect(Router.push).not.toHaveBeenCalled();
});

test("redirects if auth status is SETTLED", () => {
renderHook(() => useSessionActive(AUTH_STATE_UNAUTHENTICATED_SETTLED));
expect(Router.push).toHaveBeenCalled();
});

test("redirects to callback URL if auth status is SETTLED", () => {
renderHook(() => useSessionActive(AUTH_STATE_AUTHENTICATED_SETTLED));
expect(Router.push).toHaveBeenCalledWith(ROUTES[1]);
});
});

0 comments on commit 7ab2c8f

Please sign in to comment.