Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.0] - 2026-02-26
## [0.3.0] - 2026-04-28

### Added

- Add `disableForTesting` option to `useNavigationGuard` to make the hook a complete no-op without requiring `NavigationGuardProvider` context, intended for test and storybook environments.
- Support Next.js 16 as peer dependency and add E2E CI targets for v16.0 and v16.1.
- Add `disableForTesting` prop to `NavigationGuardProvider`. When set, the library skips its host-environment hooks (no `popstate` / `beforeunload` / `click` listeners, no `window.history` augmentation), but still overrides Next.js's App Router and Pages Router contexts so navigation through Next.js routers (including mock routers in tests) keeps evaluating registered guards. `useNavigationGuard` continues to register normally so `enabled` / `confirm` / `active` / `accept` / `reject` work as in production. Pass `{ mockConfirm }` to additionally replace every guard's `confirm` during evaluation, useful for asserting on navigation attempts without rendering the confirmation UI.
- Support Next.js 16 as peer dependency and add E2E CI targets for v16.0, v16.1, and v16.2.

### Fixed

- Guard against null `history.state` in `createHandlePopState` ([#48](https://github.com/LayerXcom/next-navigation-guard/pull/48)). @sethcarlton
- Fix import types ([#38](https://github.com/LayerXcom/next-navigation-guard/pull/38)). @lluiscab

## [0.2.0] - 2025-06-28

Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,29 @@ pnpm install next-navigation-guard
```

See working example in example/ directory and its `NavigationGuardToggle` component.

## Testing / Storybook

Pass `disableForTesting` to `NavigationGuardProvider` and the library will skip its host-environment hooks (no `popstate` / `beforeunload` / `click` listeners, no `window.history` augmentation). The App Router / Pages Router context overrides remain in place, so navigation through Next.js routers (incl. mock routers in tests) still evaluates registered guards. `useNavigationGuard` keeps registering normally, so the `enabled` predicate, the `confirm` callback, and `active` / `accept` / `reject` work as in production.

```tsx
// each guard's own confirm runs as in production
render(
<NavigationGuardProvider disableForTesting>
<MyComponent />
</NavigationGuardProvider>
);
```

To bypass the confirmation UI entirely in tests (and assert which navigations were attempted), pass `mockConfirm`. It replaces every registered guard's `confirm` during evaluation; per-guard `enabled` predicates still run.

```tsx
const mockConfirm = jest.fn().mockResolvedValue(true);
render(
<NavigationGuardProvider disableForTesting={{ mockConfirm }}>
<MyComponent />
</NavigationGuardProvider>
);
// drive navigation through your mock router…
expect(mockConfirm).toHaveBeenCalledWith({ to: "/foo", type: "push" });
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@playwright/test": "^1.52.0",
"@types/node": "^20",
"@types/react": "^18.3.5",
"next": "^14.2.11",
"next": "^14.2.35",
"playwright": "^1.52.0",
"react": "^18.3.1",
"tsup": "^8.2.4",
Expand Down
90 changes: 45 additions & 45 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/components/InterceptAppRouterProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import { AppRouterContext } from "next/dist/shared/lib/app-router-context.shared-runtime";
import React, { MutableRefObject } from "react";
import { useInterceptedAppRouter } from "../hooks/useInterceptedAppRouter";
import { GuardDef } from "../types";
import { GuardDef, NavigationGuardCallback } from "../types";

export function InterceptAppRouterProvider({
guardMapRef,
mockConfirmRef,
children,
}: {
guardMapRef: MutableRefObject<Map<string, GuardDef>>;
mockConfirmRef: MutableRefObject<NavigationGuardCallback | undefined>;
children: React.ReactNode;
}) {
const interceptedRouter = useInterceptedAppRouter({ guardMapRef });
const interceptedRouter = useInterceptedAppRouter({
guardMapRef,
mockConfirmRef,
});
if (!interceptedRouter) {
return <>{children}</>;
}
Expand Down
7 changes: 6 additions & 1 deletion src/components/InterceptPagesRouterProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import { GuardDef, NavigationGuardCallback } from "../types";

export function InterceptPagesRouterProvider({
guardMapRef,
mockConfirmRef,
children,
}: {
guardMapRef: MutableRefObject<Map<string, GuardDef>>;
mockConfirmRef: MutableRefObject<NavigationGuardCallback | undefined>;
children: React.ReactNode;
}) {
const interceptedRouter = useInterceptedPagesRouter({ guardMapRef });
const interceptedRouter = useInterceptedPagesRouter({
guardMapRef,
mockConfirmRef,
});
if (!interceptedRouter) {
return <>{children}</>;
}
Expand Down
Loading
Loading