import * as React from 'react'; import { ReactShallowRenderer } from '../../src'; import { elementSymbol } from '../../src/constants'; import { compare } from '../helpers/compare'; describe('ReactShallowRenderer', () => { const ForwardRefComponentReturnsArray: React.ForwardRefExoticComponent< React.RefAttributes // tslint:disable-next-line:variable-name > = React.forwardRef((((_props: {}, ref: React.Ref) => [

First

,

Second

, ]) as unknown) as React.RefForwardingComponent); const ComponentWithChildren: React.RefForwardingComponent< HTMLParagraphElement > = ({ children }, ref) =>

{children}

; const ForwardRefComponentWithChildren: React.ForwardRefExoticComponent< React.PropsWithChildren<{}> & React.RefAttributes > = React.forwardRef(ComponentWithChildren); const ForwardRefComponentWithForwardRefChildren: React.ForwardRefExoticComponent< React.PropsWithChildren<{}> & React.RefAttributes > = React.forwardRef(({ children }, ref) => (
I have children! {children}
)); const UnknownForwardRefComponent: React.FunctionComponent = React.forwardRef< unknown, React.PropsWithChildren<{}> >(() =>

Unknown name

); const ComponentWithUnknownForwardRefChild: React.FunctionComponent = () => (
); describe('toJSON', () => { it('renders a forward red function component with forward ref children (without ref)', () => { const element = (

I am a child!

); const renderer = new ReactShallowRenderer(element); compare(renderer.toJSON(), { $$typeof: elementSymbol, type: 'div', key: null, ref: null, props: { children: [ { $$typeof: elementSymbol, type: 'React.forwardRef(ComponentWithChildren)', key: null, ref: null, props: { children: ['I have children!'], }, _owner: null, _store: {}, }, { $$typeof: elementSymbol, type: 'p', key: null, ref: null, props: { children: ['I am a child!'], }, _owner: null, _store: {}, }, ], }, _owner: null, _store: {}, }); }); it('renders a forward red function component with forward ref children (with ref)', () => { const element = ( null}>

I am a child!

); const renderer = new ReactShallowRenderer(element); compare(renderer.toJSON(), { $$typeof: elementSymbol, type: 'div', key: null, ref: null, props: { children: [ { $$typeof: elementSymbol, type: 'React.forwardRef(ComponentWithChildren)', key: null, ref: () => null, props: { children: ['I have children!'], }, _owner: null, _store: {}, }, { $$typeof: elementSymbol, type: 'p', key: null, ref: null, props: { children: ['I am a child!'], }, _owner: null, _store: {}, }, ], }, _owner: null, _store: {}, }); }); it('renders a forward ref function component with an unknown name', () => { const element = ; const renderer = new ReactShallowRenderer(element); compare(renderer.toJSON(), { $$typeof: elementSymbol, type: 'div', key: null, ref: null, props: { children: [ { $$typeof: elementSymbol, type: 'React.forwardRef(Unknown)', key: null, ref: null, props: { children: [], }, _owner: null, _store: {}, }, ], }, _owner: null, _store: {}, }); }); it('renders a forward ref function component that returns an array (without ref)', () => { const element = ; const renderer = new ReactShallowRenderer(element); compare(renderer.toJSON(), [ { $$typeof: elementSymbol, type: 'p', key: '1', ref: null, props: { children: ['First'], }, _owner: null, _store: {}, }, { $$typeof: elementSymbol, type: 'p', key: '2', ref: null, props: { children: ['Second'], }, _owner: null, _store: {}, }, ]); }); it('renders a forward ref function component that returns an array (with ref)', () => { const element = null} />; const renderer = new ReactShallowRenderer(element); compare(renderer.toJSON(), [ { $$typeof: elementSymbol, type: 'p', key: '1', ref: () => null, props: { children: ['First'], }, _owner: null, _store: {}, }, { $$typeof: elementSymbol, type: 'p', key: '2', ref: null, props: { children: ['Second'], }, _owner: null, _store: {}, }, ]); }); }); });