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
24 changes: 12 additions & 12 deletions packages/react-router/tests/link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4024,8 +4024,8 @@ describe('Link', () => {
),
})

const parseParams = vi.fn()
const stringifyParams = vi.fn()
let parseParams: any
let stringifyParams: any

const PostComponent = () => {
const params = useParams({ strict: false })
Expand All @@ -4036,14 +4036,14 @@ describe('Link', () => {
getParentRoute: () => rootRoute,
path: '$postId',
parseParams: (params) => {
parseParams(params)
parseParams = structuredClone(params) // clone object, because source will get mutated
return {
status: 'parsed',
postId: params.postId,
}
},
stringifyParams: (params) => {
stringifyParams(params)
stringifyParams = structuredClone(params) // clone object, because source will get mutated
return {
status: 'stringified',
postId: params.postId,
Expand All @@ -4061,7 +4061,7 @@ describe('Link', () => {
name: 'Go to post',
})

expect(stringifyParams).toHaveBeenCalledWith({ postId: 2 })
expect(stringifyParams).toEqual({ postId: 2 })

expect(postLink).toHaveAttribute('href', '/2')

Expand All @@ -4070,7 +4070,7 @@ describe('Link', () => {
const posts2Text = await screen.findByText('Post: 2')
expect(posts2Text).toBeInTheDocument()

expect(parseParams).toHaveBeenCalledWith({ status: 'parsed', postId: '2' })
expect(parseParams).toEqual({ postId: '2' })
})

test('when navigating to /$postId with params.parse and params.stringify', async () => {
Expand All @@ -4086,8 +4086,8 @@ describe('Link', () => {
),
})

const parseParams = vi.fn()
const stringifyParams = vi.fn()
let parseParams: any
let stringifyParams: any

const PostComponent = () => {
const params = useParams({ strict: false })
Expand All @@ -4099,14 +4099,14 @@ describe('Link', () => {
path: '$postId',
params: {
parse: (params) => {
parseParams(params)
parseParams = structuredClone(params) // clone object, because source will get mutated
return {
status: 'parsed',
postId: params.postId,
}
},
stringify: (params) => {
stringifyParams(params)
stringifyParams = structuredClone(params) // clone object, because source will get mutated
return {
status: 'stringified',
postId: params.postId,
Expand All @@ -4125,7 +4125,7 @@ describe('Link', () => {
name: 'Go to post',
})

expect(stringifyParams).toHaveBeenCalledWith({ postId: 2 })
expect(stringifyParams).toEqual({ postId: 2 })

expect(postLink).toHaveAttribute('href', '/2')

Expand All @@ -4134,7 +4134,7 @@ describe('Link', () => {
const posts2Text = await screen.findByText('Post: 2')
expect(posts2Text).toBeInTheDocument()

expect(parseParams).toHaveBeenCalledWith({ status: 'parsed', postId: '2' })
expect(parseParams).toEqual({ postId: '2' })
})

test('when navigating to /$postId with params.parse and params.stringify handles falsey inputs', async () => {
Expand Down
51 changes: 24 additions & 27 deletions packages/router-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1472,20 +1472,20 @@ export class RouterCore<
: this.resolvePathWithBase(fromPath, '.')

// Resolve the next params
let nextParams =
const nextParams =
dest.params === false || dest.params === null
? {}
: (dest.params ?? true) === true
? fromParams
: {
...fromParams,
...functionalUpdate(dest.params as any, fromParams),
}
: Object.assign(
fromParams,
functionalUpdate(dest.params as any, fromParams),
)

// Interpolate the path first to get the actual resolved path, then match against that
const interpolatedNextTo = interpolatePath({
path: nextTo,
params: nextParams ?? {},
params: nextParams,
parseCache: this.parsePathnameCache,
}).interpolatedPath

Expand All @@ -1495,23 +1495,20 @@ export class RouterCore<

// If there are any params, we need to stringify them
if (Object.keys(nextParams).length > 0) {
destRoutes
.map((route) => {
return (
route.options.params?.stringify ?? route.options.stringifyParams
)
})
.filter(Boolean)
.forEach((fn) => {
nextParams = { ...nextParams!, ...fn!(nextParams) }
})
for (const route of destRoutes) {
const fn =
route.options.params?.stringify ?? route.options.stringifyParams
if (fn) {
Object.assign(nextParams, fn(nextParams))
}
}
}

const nextPathname = interpolatePath({
// Use the original template path for interpolation
// This preserves the original parameter syntax including optional parameters
path: nextTo,
params: nextParams ?? {},
params: nextParams,
leaveWildcards: false,
leaveParams: opts.leaveParams,
decodeCharMap: this.pathParamsDecodeCharMap,
Expand All @@ -1521,20 +1518,20 @@ export class RouterCore<
// Resolve the next search
let nextSearch = fromSearch
if (opts._includeValidateSearch && this.options.search?.strict) {
let validatedSearch = {}
const validatedSearch = {}
destRoutes.forEach((route) => {
try {
if (route.options.validateSearch) {
validatedSearch = {
...validatedSearch,
...(validateSearch(route.options.validateSearch, {
if (route.options.validateSearch) {
try {
Object.assign(
validatedSearch,
validateSearch(route.options.validateSearch, {
...validatedSearch,
...nextSearch,
}) ?? {}),
}
}),
)
} catch {
// ignore errors here because they are already handled in matchRoutes
}
} catch {
// ignore errors here because they are already handled in matchRoutes
}
})
nextSearch = validatedSearch
Expand Down
24 changes: 12 additions & 12 deletions packages/solid-router/tests/link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3592,8 +3592,8 @@ describe('Link', () => {
),
})

const parseParams = vi.fn()
const stringifyParams = vi.fn()
let parseParams: any
let stringifyParams: any

const PostComponent = () => {
const params = useParams({ strict: false })
Expand All @@ -3604,14 +3604,14 @@ describe('Link', () => {
getParentRoute: () => rootRoute,
path: '$postId',
parseParams: (params) => {
parseParams(params)
parseParams = structuredClone(params) // clone object, because source will get mutated
return {
status: 'parsed',
postId: params.postId,
}
},
stringifyParams: (params) => {
stringifyParams(params)
stringifyParams = structuredClone(params) // clone object, because source will get mutated
return {
status: 'stringified',
postId: params.postId,
Expand All @@ -3629,7 +3629,7 @@ describe('Link', () => {
name: 'Go to post',
})

expect(stringifyParams).toHaveBeenCalledWith({ postId: 2 })
expect(stringifyParams).toEqual({ postId: 2 })

expect(postLink).toHaveAttribute('href', '/2')

Expand All @@ -3638,7 +3638,7 @@ describe('Link', () => {
const posts2Text = await screen.findByText('Post: 2')
expect(posts2Text).toBeInTheDocument()

expect(parseParams).toHaveBeenCalledWith({ status: 'parsed', postId: '2' })
expect(parseParams).toEqual({ postId: '2' })
})

test('when navigating to /$postId with params.parse and params.stringify', async () => {
Expand All @@ -3654,8 +3654,8 @@ describe('Link', () => {
),
})

const parseParams = vi.fn()
const stringifyParams = vi.fn()
let parseParams: any
let stringifyParams: any

const PostComponent = () => {
const params = useParams({ strict: false })
Expand All @@ -3667,14 +3667,14 @@ describe('Link', () => {
path: '$postId',
params: {
parse: (params) => {
parseParams(params)
parseParams = structuredClone(params) // clone object, because source will get mutated
return {
status: 'parsed',
postId: params.postId,
}
},
stringify: (params) => {
stringifyParams(params)
stringifyParams = structuredClone(params) // clone object, because source will get mutated
return {
status: 'stringified',
postId: params.postId,
Expand All @@ -3693,7 +3693,7 @@ describe('Link', () => {
name: 'Go to post',
})

expect(stringifyParams).toHaveBeenCalledWith({ postId: 2 })
expect(stringifyParams).toEqual({ postId: 2 })

expect(postLink).toHaveAttribute('href', '/2')

Expand All @@ -3702,7 +3702,7 @@ describe('Link', () => {
const posts2Text = await screen.findByText('Post: 2')
expect(posts2Text).toBeInTheDocument()

expect(parseParams).toHaveBeenCalledWith({ status: 'parsed', postId: '2' })
expect(parseParams).toEqual({ postId: '2' })
})

test('when navigating to /$postId with params.parse and params.stringify handles falsey inputs', async () => {
Expand Down