Skip to content
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

Supported basePath for to prop when passed as string. Resolving acces… #227

Merged
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 src/ui/link/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const Link = forwardRef<HTMLButtonElement | HTMLAnchorElement, LinkProps>(
)) ||
''
: to;
const staticBasePath =
href == null && typeof to === 'string' ? routeAttributes.basePath : '';

const triggerPrefetch = useCallback(() => {
// ignore if async route not ready yet
Expand Down Expand Up @@ -145,7 +147,7 @@ const Link = forwardRef<HTMLButtonElement | HTMLAnchorElement, LinkProps>(
validLinkType,
{
...rest,
href: linkDestination,
href: `${staticBasePath}${linkDestination}`,
target,
onClick: handleLinkPress,
onKeyDown: handleLinkPress,
Expand Down
24 changes: 24 additions & 0 deletions src/ui/link/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ describe('<Link />', () => {
expect(linkElement).toHaveAttribute('href', newPath);
});

it('should support the `to` prop with basePath', () => {
renderInRouter('my link', { to: '/my-page/1?foo=bar' }, '/base');
const linkElement = screen.getByRole('link', { name: 'my link' });
expect(linkElement).toHaveAttribute('href', `/base/my-page/1?foo=bar`);
});

it('should push history with correct link given basePath', async () => {
const user = userEvent.setup();
renderInRouter(
'my link',
{
to: '/my-page/1?foo=bar',
},
'/base'
);

await user.click(screen.getByRole('link', { name: 'my link' }));

expect(HistoryMock.push).toHaveBeenCalledWith(
'/base/my-page/1?foo=bar',
undefined
);
});

it('should pass props to the child element', () => {
renderInRouter('my link', {
...defaultProps,
Expand Down
Loading