diff --git a/code/renderers/react/src/docs/jsxDecorator.test.tsx b/code/renderers/react/src/docs/jsxDecorator.test.tsx index b8868265333e..97c267a3b2a2 100644 --- a/code/renderers/react/src/docs/jsxDecorator.test.tsx +++ b/code/renderers/react/src/docs/jsxDecorator.test.tsx @@ -123,30 +123,55 @@ describe('renderJsx', () => { `); }); - it('forwardRef component', () => { - const MyExoticComponentRef = React.forwardRef( - function MyExoticComponent(props, _ref) { - return
{props.children}
; - } - ); + describe('forwardRef component', () => { + it('with no displayName', () => { + const MyExoticComponentRef = React.forwardRef( + function MyExoticComponent(props, _ref) { + return
{props.children}
; + } + ); + + expect(renderJsx(I am forwardRef!)) + .toMatchInlineSnapshot(` + + I am forwardRef! + + `); + }); - expect(renderJsx(I am forwardRef!)) - .toMatchInlineSnapshot(` - - I am forwardRef! - - `); + it('with displayName coming from docgen', () => { + const MyExoticComponentRef = React.forwardRef( + function MyExoticComponent(props, _ref) { + return
{props.children}
; + } + ); + (MyExoticComponentRef as any).__docgenInfo = { + displayName: 'ExoticComponent', + }; + expect(renderJsx(I am forwardRef!)) + .toMatchInlineSnapshot(` + + I am forwardRef! + + `); + }); - // if docgenInfo is present, it should use the displayName from there - (MyExoticComponentRef as any).__docgenInfo = { - displayName: 'ExoticComponent', - }; - expect(renderJsx(I am forwardRef!)) - .toMatchInlineSnapshot(` + it('with displayName coming from forwarded render function', () => { + const MyExoticComponentRef = React.forwardRef( + Object.assign( + function MyExoticComponent(props: any, _ref: any) { + return
{props.children}
; + }, + { displayName: 'ExoticComponent' } + ) + ); + expect(renderJsx(I am forwardRef!)) + .toMatchInlineSnapshot(` I am forwardRef! `); + }); }); it('memo component', () => { diff --git a/code/renderers/react/src/docs/jsxDecorator.tsx b/code/renderers/react/src/docs/jsxDecorator.tsx index e6b1934c2e6e..f3a6d3b05cff 100644 --- a/code/renderers/react/src/docs/jsxDecorator.tsx +++ b/code/renderers/react/src/docs/jsxDecorator.tsx @@ -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')