Skip to content
Merged
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
4 changes: 3 additions & 1 deletion packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,9 @@ export class Router<
)
let pathname: string
if (dest.to) {
pathname = this.resolvePathWithBase(fromPath, `${dest.to}`)
const resolvePathTo =
fromMatch?.fullPath || this.latestLocation.pathname
pathname = this.resolvePathWithBase(resolvePathTo, `${dest.to}`)
} else {
const fromRouteByFromPathRouteId =
this.routesById[
Expand Down
60 changes: 60 additions & 0 deletions packages/react-router/tests/navigate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,63 @@ describe('router.navigate navigation using layout routes resolves correctly', ()
expect(router.state.location.search).toStrictEqual({ 'foo=bar': 3 })
})
})

describe('relative navigation', () => {
it('should navigate to a child route', async () => {
const { router } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts'] }),
)

await router.load()

expect(router.state.location.pathname).toBe('/posts')

await router.navigate({
from: '/posts',
to: './$slug',
params: { slug: 'tkdodo' },
})

await router.invalidate()

expect(router.state.location.pathname).toBe('/posts/tkdodo')
})

it('should navigate to a parent route', async () => {
const { router } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
)

await router.load()

expect(router.state.location.pathname).toBe('/posts/tanner')

await router.navigate({
to: '..',
})

await router.invalidate()

expect(router.state.location.pathname).toBe('/posts')
})

it('should navigate to a sibling route', async () => {
const { router } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
)

await router.load()

expect(router.state.location.pathname).toBe('/posts/tanner')

await router.navigate({
from: '/posts/$slug',
to: '.',
params: { slug: 'tkdodo' },
})

await router.invalidate()

expect(router.state.location.pathname).toBe('/posts/tkdodo')
})
})