Skip to content
Closed
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
30 changes: 30 additions & 0 deletions packages/react-router/tests/router.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
screen,
waitFor,
} from '@testing-library/react'
import { z } from 'zod'
import {
Link,
Outlet,
Expand Down Expand Up @@ -74,6 +75,17 @@ function createTestRouter(initialHistory?: RouterHistory) {
path: '$',
})

const routeWithParamsParseStringify = createRoute({
getParentRoute: () => rootRoute,
path: '/paramsParseStringify/$id',
params: {
parse: (params) => ({
id: z.number().int().parse(Number(params.id)),
}),
stringify: ({ id }) => ({ id: `${id}` }),
},
})

const routeTree = rootRoute.addChildren([
indexRoute,
postsRoute.addChildren([postIdRoute]),
Expand All @@ -85,6 +97,7 @@ function createTestRouter(initialHistory?: RouterHistory) {
pathSegmentLayoutSplatIndexRoute,
pathSegmentLayoutSplatSplatRoute,
]),
routeWithParamsParseStringify,
])

const router = createRouter({ routeTree, history })
Expand Down Expand Up @@ -580,3 +593,20 @@ describe('router matches URLs to route definitions', () => {
])
})
})

describe('matchRoute', () => {
it('should match route with custom parse/stringify functions', async () => {
const { router } = createTestRouter(
createMemoryHistory({
initialEntries: ['/paramsParseStringify/123'],
}),
)
await act(() => router.load())
const result = router.matchRoute({
to: '/paramsParseStringify/$id',
params: { id: 123 },
})
expect(result).toBeTruthy()
expect((result as any).id).toBe(123)
})
})