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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon Docs: Support Stencil based display names in source snippets #26592

Merged
merged 1 commit into from Mar 21, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 43 additions & 18 deletions code/renderers/react/src/docs/jsxDecorator.test.tsx
Expand Up @@ -123,30 +123,55 @@ describe('renderJsx', () => {
`);
});

it('forwardRef component', () => {
const MyExoticComponentRef = React.forwardRef<FC, PropsWithChildren>(
function MyExoticComponent(props, _ref) {
return <div>{props.children}</div>;
}
);
describe('forwardRef component', () => {
it('with no displayName', () => {
const MyExoticComponentRef = React.forwardRef<FC, PropsWithChildren>(
function MyExoticComponent(props, _ref) {
return <div>{props.children}</div>;
}
);

expect(renderJsx(<MyExoticComponentRef>I am forwardRef!</MyExoticComponentRef>))
.toMatchInlineSnapshot(`
<React.ForwardRef>
I am forwardRef!
</React.ForwardRef>
`);
});

expect(renderJsx(<MyExoticComponentRef>I am forwardRef!</MyExoticComponentRef>))
.toMatchInlineSnapshot(`
<React.ForwardRef>
I am forwardRef!
</React.ForwardRef>
`);
it('with displayName coming from docgen', () => {
const MyExoticComponentRef = React.forwardRef<FC, PropsWithChildren>(
function MyExoticComponent(props, _ref) {
return <div>{props.children}</div>;
}
);
(MyExoticComponentRef as any).__docgenInfo = {
displayName: 'ExoticComponent',
};
expect(renderJsx(<MyExoticComponentRef>I am forwardRef!</MyExoticComponentRef>))
.toMatchInlineSnapshot(`
<ExoticComponent>
I am forwardRef!
</ExoticComponent>
`);
});

// if docgenInfo is present, it should use the displayName from there
(MyExoticComponentRef as any).__docgenInfo = {
displayName: 'ExoticComponent',
};
expect(renderJsx(<MyExoticComponentRef>I am forwardRef!</MyExoticComponentRef>))
.toMatchInlineSnapshot(`
it('with displayName coming from forwarded render function', () => {
const MyExoticComponentRef = React.forwardRef<FC, PropsWithChildren>(
Object.assign(
function MyExoticComponent(props: any, _ref: any) {
return <div>{props.children}</div>;
},
{ displayName: 'ExoticComponent' }
)
);
expect(renderJsx(<MyExoticComponentRef>I am forwardRef!</MyExoticComponentRef>))
.toMatchInlineSnapshot(`
<ExoticComponent>
I am forwardRef!
</ExoticComponent>
`);
});
});

it('memo component', () => {
Expand Down
2 changes: 2 additions & 0 deletions code/renderers/react/src/docs/jsxDecorator.tsx
Expand Up @@ -130,6 +130,8 @@ export const renderJsx = (code: React.ReactElement, options?: JSXOptions) => {
return el.type.displayName;
} else if (getDocgenSection(el.type, 'displayName')) {
return getDocgenSection(el.type, 'displayName');
} else if (el.type.render?.displayName) {
return el.type.render.displayName;
} else if (
typeof el.type === 'symbol' ||
(el.type.$$typeof && typeof el.type.$$typeof === 'symbol')
Expand Down