diff --git a/tests/transformRoute.test.ts b/tests/transformRoute.test.ts new file mode 100644 index 0000000..36f61dd --- /dev/null +++ b/tests/transformRoute.test.ts @@ -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(); + }); +}); diff --git a/tests/useRouteHistory.test.ts b/tests/useRouteHistory.test.ts index fa7db8c..490a88a 100644 --- a/tests/useRouteHistory.test.ts +++ b/tests/useRouteHistory.test.ts @@ -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("next/router"); +jest.unstable_mockModule("next/router", () => { return { - ...original, + ...jest.requireActual("next/router"), useRouter: jest.fn(), }; }); diff --git a/tests/useSessionActive.test.ts b/tests/useSessionActive.test.ts new file mode 100644 index 0000000..606bc19 --- /dev/null +++ b/tests/useSessionActive.test.ts @@ -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("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]); + }); +});