Skip to content

Commit

Permalink
fix: forward NavigateOptions in adaptForAppRouterInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey committed Jul 10, 2023
1 parent c6b163f commit 0ba632e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
65 changes: 65 additions & 0 deletions packages/next/src/shared/lib/router/adapters.test.tsx
Original file line number Diff line number Diff line change
@@ -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')
})
})
10 changes: 5 additions & 5 deletions packages/next/src/shared/lib/router/adapters.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand Down

0 comments on commit 0ba632e

Please sign in to comment.