Repository navigation
TanStack: Render real link hrefs in the Link mock #35505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
huang-julien
merged 7 commits into
storybookjs:next
from
unpunnyfuns:fix/tanstack-react-link-href
Aug 12, 2026
+131
−2
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f0c2156
fix(tanstack-react): render real link hrefs in the Link mock
unpunnyfuns bfba261
fix(tanstack-react): preserve a story's onClick on the Link mock
unpunnyfuns 6ae28eb
test(tanstack-react): broaden Link mock coverage
unpunnyfuns c285a9b
Update code/frameworks/tanstack-react/src/export-mocks/react-router.ts
unpunnyfuns 83b36ee
refactor(tanstack-react): keep the Link mock navigation-only
unpunnyfuns 5ad9eb6
Merge branch 'next' into fix/tanstack-react-link-href
huang-julien b57cb21
Merge branch 'next' into fix/tanstack-react-link-href
huang-julien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
code/frameworks/tanstack-react/src/export-mocks/react-router-link.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // @vitest-environment happy-dom | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import React from 'react'; | ||
| import { cleanup, fireEvent, render, screen } from '@testing-library/react'; | ||
| import { | ||
| RouterProvider, | ||
| createMemoryHistory, | ||
| createRootRoute, | ||
| createRoute, | ||
| createRouter, | ||
| } from '@tanstack/react-router'; | ||
|
|
||
| import { Link } from './react-router.ts'; | ||
| import { onNavigate } from './spies.ts'; | ||
|
|
||
| async function renderWithRouter(linkElement: React.ReactNode) { | ||
| const root = createRootRoute(); | ||
| const index = createRoute({ | ||
| path: '/', | ||
| getParentRoute: () => root, | ||
| component: () => linkElement, | ||
| }); | ||
| const user = createRoute({ | ||
| path: '/users/$userId', | ||
| getParentRoute: () => root, | ||
| component: () => null, | ||
| }); | ||
| const files = createRoute({ | ||
| path: '/files/$', | ||
| getParentRoute: () => root, | ||
| component: () => null, | ||
| }); | ||
| const posts = createRoute({ | ||
| path: '/posts', | ||
| getParentRoute: () => root, | ||
| validateSearch: (search: Record<string, unknown>) => ({ | ||
| tag: (search.tag as string) ?? undefined, | ||
| page: (search.page as number) ?? undefined, | ||
| }), | ||
| component: () => null, | ||
| }); | ||
| const about = createRoute({ | ||
| path: '/about', | ||
| getParentRoute: () => root, | ||
| component: () => null, | ||
| }); | ||
| root.addChildren([index, user, files, posts, about]); | ||
| const router = createRouter({ | ||
| routeTree: root, | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| }); | ||
| render(<RouterProvider router={router as any} />); | ||
| return router; | ||
| } | ||
|
|
||
| describe('Link mock', () => { | ||
| beforeEach(() => { | ||
| onNavigate.mockClear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| cleanup(); | ||
| }); | ||
|
|
||
| it('interpolates path params into href and keeps params off the DOM', async () => { | ||
| await renderWithRouter( | ||
| <Link to="/users/$userId" params={{ userId: '42' }} data-testid="user-link"> | ||
| Ada | ||
| </Link> | ||
| ); | ||
|
|
||
| const link = await screen.findByTestId('user-link'); | ||
| expect(link.getAttribute('href')).toBe('/users/42'); | ||
| expect(link.hasAttribute('params')).toBe(false); | ||
| }); | ||
|
|
||
| it('renders a static route href with no params', async () => { | ||
| await renderWithRouter( | ||
| <Link to="/about" data-testid="about-link"> | ||
| About | ||
| </Link> | ||
| ); | ||
|
|
||
| const link = await screen.findByTestId('about-link'); | ||
| expect(link.getAttribute('href')).toBe('/about'); | ||
| }); | ||
|
|
||
| it('serializes search params into the href', async () => { | ||
| await renderWithRouter( | ||
| <Link to="/posts" search={{ tag: 'news', page: 2 }} data-testid="search-link"> | ||
| News | ||
| </Link> | ||
| ); | ||
|
|
||
| const link = await screen.findByTestId('search-link'); | ||
| const href = link.getAttribute('href') ?? ''; | ||
| expect(href.startsWith('/posts?')).toBe(true); | ||
| expect(href).toContain('tag=news'); | ||
| expect(href).toContain('page=2'); | ||
| expect(link.hasAttribute('search')).toBe(false); | ||
| }); | ||
|
|
||
| it('interpolates splat params', async () => { | ||
| await renderWithRouter( | ||
| <Link to="/files/$" params={{ _splat: 'reports/q2.pdf' }} data-testid="splat-link"> | ||
| Files | ||
| </Link> | ||
| ); | ||
|
|
||
| const link = await screen.findByTestId('splat-link'); | ||
| expect(link.getAttribute('href')).toBe('/files/reports/q2.pdf'); | ||
| }); | ||
|
|
||
| it('spies navigation instead of performing it', async () => { | ||
| const router = await renderWithRouter( | ||
| <Link to="/users/$userId" params={{ userId: '42' }} data-testid="user-link"> | ||
| Ada | ||
| </Link> | ||
| ); | ||
|
|
||
| fireEvent.click(await screen.findByTestId('user-link')); | ||
|
|
||
| expect(onNavigate).toHaveBeenCalledWith(expect.objectContaining({ to: '/users/$userId' })); | ||
| expect(router.state.location.pathname).toBe('/'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.