From 0ba632e319b2b47c24e07bbaf064367866dc6fcb Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 10 Jul 2023 13:03:14 +0200 Subject: [PATCH] fix: forward NavigateOptions in adaptForAppRouterInstance --- .../src/shared/lib/router/adapters.test.tsx | 65 +++++++++++++++++++ .../next/src/shared/lib/router/adapters.tsx | 10 +-- 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 packages/next/src/shared/lib/router/adapters.test.tsx diff --git a/packages/next/src/shared/lib/router/adapters.test.tsx b/packages/next/src/shared/lib/router/adapters.test.tsx new file mode 100644 index 0000000000000..fa8e48f2fc088 --- /dev/null +++ b/packages/next/src/shared/lib/router/adapters.test.tsx @@ -0,0 +1,65 @@ +import { adaptForAppRouterInstance } from './adapters' +import { NextRouter } from './router' + +describe('adaptForAppRouterInstance', () => { + beforeEach(() => jest.resetAllMocks()) + + const router = { + back: jest.fn(), + forward: jest.fn(), + reload: jest.fn(), + push: jest.fn(), + replace: jest.fn(), + prefetch: jest.fn(), + } as unknown as NextRouter + + const adapter = adaptForAppRouterInstance(router) + + it('should forward a call to `back()`', () => { + adapter.back() + expect(router.back).toHaveBeenCalled() + }) + + it('should forward a call to `forward()`', () => { + adapter.forward() + expect(router.forward).toHaveBeenCalled() + }) + + it('should forward a call to `reload()`', () => { + adapter.refresh() + expect(router.reload).toHaveBeenCalled() + }) + + it('should forward a call to `push()`', () => { + adapter.push('/foo') + expect(router.push).toHaveBeenCalledWith('/foo', undefined, { + scroll: undefined, + }) + }) + + it('should forward a call to `push()` with options', () => { + adapter.push('/foo', { scroll: false }) + expect(router.push).toHaveBeenCalledWith('/foo', undefined, { + scroll: false, + }) + }) + + it('should forward a call to `replace()`', () => { + adapter.replace('/foo') + expect(router.replace).toHaveBeenCalledWith('/foo', undefined, { + scroll: undefined, + }) + }) + + it('should forward a call to `replace()` with options', () => { + adapter.replace('/foo', { scroll: false }) + expect(router.replace).toHaveBeenCalledWith('/foo', undefined, { + scroll: false, + }) + }) + + it('should forward a call to `prefetch()`', () => { + adapter.prefetch('/foo') + expect(router.prefetch).toHaveBeenCalledWith('/foo') + }) +}) diff --git a/packages/next/src/shared/lib/router/adapters.tsx b/packages/next/src/shared/lib/router/adapters.tsx index ed237d3525fa0..ce68a8bec1e8b 100644 --- a/packages/next/src/shared/lib/router/adapters.tsx +++ b/packages/next/src/shared/lib/router/adapters.tsx @@ -1,6 +1,6 @@ import type { ParsedUrlQuery } from 'node:querystring' import React, { useMemo, useRef } from 'react' -import type { AppRouterInstance } from '../app-router-context' +import type { AppRouterInstance, NavigateOptions } from '../app-router-context' import { PathnameContext } from '../hooks-client-context' import type { NextRouter } from './router' import { isDynamicRoute } from './utils' @@ -24,11 +24,11 @@ export function adaptForAppRouterInstance( refresh(): void { router.reload() }, - push(href: string): void { - void router.push(href) + push(href: string, { scroll }: NavigateOptions = {}): void { + void router.push(href, undefined, { scroll }) }, - replace(href: string): void { - void router.replace(href) + replace(href: string, { scroll }: NavigateOptions = {}): void { + void router.replace(href, undefined, { scroll }) }, prefetch(href: string): void { void router.prefetch(href)