-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added test use session active (#178)
- Loading branch information
Fran McDade
authored and
Fran McDade
committed
Nov 6, 2024
1 parent
8f06f8e
commit 7ab2c8f
Showing
3 changed files
with
97 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); |