Skip to content

Commit

Permalink
feat: test linting (#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 df666cb commit 8f06f8e
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions tests/useRouteHistory.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest } from "@jest/globals";
import { act, renderHook } from "@testing-library/react";
import Router from "next/router";
import Router, { NextRouter } from "next/router";

const ROOT_PATH = "/";
const ROUTES = ["/route1", "/route2", "/route3", "/route4"];
Expand All @@ -9,7 +9,6 @@ jest.unstable_mockModule("next/router", async () => {
const original =
jest.requireActual<typeof import("next/router")>("next/router");
return {
__esModule: true,
...original,
useRouter: jest.fn(),
};
Expand All @@ -22,44 +21,26 @@ const { useRouter } = await import("next/router");
const { useRouteRoot } = await import("../src/hooks/useRouteRoot");
const { useRouteHistory } = await import("../src/hooks/useRouteHistory");

const MOCK_USE_ROUTER = useRouter as jest.MockedFunction<typeof useRouter>;
const MOCK_USE_ROUTER = useRouter as jest.MockedFunction<
() => Partial<NextRouter>
>;
const MOCK_USE_ROUTE_ROOT = useRouteRoot as jest.MockedFunction<
typeof useRouteRoot
>;

describe("useRouteHistory", () => {
beforeEach(async () => {
beforeEach(() => {
MOCK_USE_ROUTE_ROOT.mockReset();
MOCK_USE_ROUTER.mockReset();
MOCK_USE_ROUTER.mockReturnValue({
asPath: ROUTES[0],
back: jest.fn(),
basePath: "",
beforePopState: jest.fn(),
events: {
emit: jest.fn(),
off: jest.fn(),
on: jest.fn(),
},
forward: jest.fn(),
isFallback: false,
isLocaleDomain: false,
isPreview: false,
isReady: true,
pathname: ROUTES[0],
prefetch: jest.fn(() => Promise.resolve()),
push: jest.fn(() => Promise.resolve(true)),
query: {},
reload: jest.fn(),
replace: jest.fn(() => Promise.resolve(true)),
route: ROUTES[0],
});
MOCK_USE_ROUTE_ROOT.mockReturnValue(ROOT_PATH);
});

test("returns the root path when no previous route exists", () => {
const { result } = renderHook(() => useRouteHistory());
expect(result.current.callbackUrl()).toEqual(ROOT_PATH);
expect(result.current.callbackUrl()).toBe(ROOT_PATH);
});

test("updates history on route change", () => {
Expand Down Expand Up @@ -93,11 +74,12 @@ describe("useRouteHistory", () => {
test("uses transform function if provided", () => {
const { result } = renderHook(() => useRouteHistory(4));
act(() => {
Router.events.emit("routeChangeComplete", ROUTES[1]); // Route is third in stack at index 2 e.g. [3, 2, 1, 0]
Router.events.emit("routeChangeComplete", ROUTES[1]);
Router.events.emit("routeChangeComplete", ROUTES[2]);
Router.events.emit("routeChangeComplete", ROUTES[3]); // Route is first in stack at index 0 e.g. [3, 2, 1, 0]
Router.events.emit("routeChangeComplete", ROUTES[3]);
});
const transformFn = (routes: string[]): string => routes[2]; // Pick the third route in the stack at index 2.
// After emitting the routes, the history stack is [ROUTES[3], ROUTES[2], ROUTES[1], ROUTES[0]].
const transformFn = (routes: string[]): string => routes[2];
expect(result.current.callbackUrl(transformFn)).toBe(ROUTES[1]);
});

Expand Down

0 comments on commit 8f06f8e

Please sign in to comment.