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: 4 additions & 0 deletions code/frameworks/nextjs/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const config: BuildEntries = {
exportEntries: ['./navigation.mock'],
entryPoint: './src/export-mocks/navigation/index.ts',
},
{
exportEntries: ['./link.mock'],
entryPoint: './src/export-mocks/link/index.tsx',
},
{
exportEntries: ['./router.mock'],
entryPoint: './src/export-mocks/router/index.ts',
Expand Down
5 changes: 5 additions & 0 deletions code/frameworks/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"./image-context": "./dist/image-context.js",
"./images/next-image": "./dist/images/next-image.js",
"./images/next-legacy-image": "./dist/images/next-legacy-image.js",
"./link.mock": {
"types": "./dist/export-mocks/link/index.d.ts",
"code": "./src/export-mocks/link/index.tsx",
"default": "./dist/export-mocks/link/index.js"
},
"./navigation.mock": {
"types": "./dist/export-mocks/navigation/index.d.ts",
"code": "./src/export-mocks/navigation/index.ts",
Expand Down
45 changes: 45 additions & 0 deletions code/frameworks/nextjs/src/export-mocks/link/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';

import { fn } from 'storybook/test';

const linkAction = fn().mockName('next/link::Link');

const MockLink = React.forwardRef<HTMLAnchorElement, any>(function MockLink(
{
href,
as: _as,
replace,
scroll,
shallow,
prefetch,
passHref,
legacyBehavior,
locale,
onClick,
children,
...rest
},
ref
) {
const hrefString =
typeof href === 'object'
? `${href.pathname || ''}${href.query ? '?' + new URLSearchParams(href.query).toString() : ''}${href.hash || ''}`
: href;

const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();
onClick?.(e);
linkAction(hrefString, { replace, scroll, shallow, prefetch, locale });
};

return (
<a ref={ref} href={hrefString} onClick={handleClick} {...rest}>
{children}
</a>
);
Comment on lines +35 to +39

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if legacyBehavior is true, this should pass the props onto the child instead of rendering an <a> itself. #33861

See here: https://github.com/vercel/next.js/blob/b0595b17ae331a2e1cccff35da8d093f0636c8e1/packages/next/src/client/link.tsx#L680-L690

});

MockLink.displayName = 'NextLink';

export default MockLink;
export { MockLink as Link };
1 change: 1 addition & 0 deletions code/frameworks/nextjs/src/export-mocks/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const mapping = {
'next/navigation': '@storybook/nextjs/navigation.mock',
'next/router': '@storybook/nextjs/router.mock',
'next/cache': '@storybook/nextjs/cache.mock',
'next/link': '@storybook/nextjs/link.mock',
...getCompatibilityAliases(),
};

Expand Down