1+ import type { ElementType } from 'react' ;
12import type { ReactTestInstance } from 'react-test-renderer' ;
23import {
34 EXPECTED_COLOR ,
@@ -55,12 +56,20 @@ export function checkHostElement(
5556 }
5657}
5758
59+ export type FormatElementOptions = {
60+ // Minimize used space.
61+ minimal ?: boolean ;
62+ } ;
63+
5864/***
5965 * Format given element as a pretty-printed string.
6066 *
6167 * @param element Element to format.
6268 */
63- export function formatElement ( element : ReactTestInstance | null ) {
69+ export function formatElement (
70+ element : ReactTestInstance | null ,
71+ { minimal = false } : FormatElementOptions = { } ,
72+ ) {
6473 if ( element == null ) {
6574 return ' null' ;
6675 }
@@ -74,7 +83,7 @@ export function formatElement(element: ReactTestInstance | null) {
7483 // This prop is needed persuade the prettyFormat that the element is
7584 // a ReactTestRendererJSON instance, so it is formatted as JSX.
7685 $$typeof : Symbol . for ( 'react.test.json' ) ,
77- type : element . type ,
86+ type : formatElementType ( element . type ) ,
7887 props : defaultMapProps ( props ) ,
7988 children : childrenToDisplay ,
8089 } ,
@@ -83,18 +92,47 @@ export function formatElement(element: ReactTestInstance | null) {
8392 printFunctionName : false ,
8493 printBasicPrototype : false ,
8594 highlight : true ,
95+ min : minimal ,
8696 } ,
8797 ) ,
8898 2 ,
8999 ) ;
90100}
91101
92- export function formatElementArray ( elements : ReactTestInstance [ ] ) {
102+ export function formatElementType ( type : ElementType ) : string {
103+ if ( typeof type === 'function' ) {
104+ return type . displayName ?? type . name ;
105+ }
106+
107+ // if (typeof type === 'object') {
108+ // console.log('OBJECT', type);
109+ // }
110+
111+ if ( typeof type === 'object' && 'type' in type ) {
112+ // @ts -expect-error: despite typing this can happen
113+ const nestedType = formatElementType ( type . type ) ;
114+ if ( nestedType ) {
115+ return nestedType ;
116+ }
117+ }
118+
119+ if ( typeof type === 'object' && 'render' in type ) {
120+ // @ts -expect-error: despite typing this can happen
121+ const nestedType = formatElementType ( type . render ) ;
122+ if ( nestedType ) {
123+ return nestedType ;
124+ }
125+ }
126+
127+ return `${ type } ` ;
128+ }
129+
130+ export function formatElementArray ( elements : ReactTestInstance [ ] , options ?: FormatElementOptions ) {
93131 if ( elements . length === 0 ) {
94132 return ' (no elements)' ;
95133 }
96134
97- return redent ( elements . map ( formatElement ) . join ( '\n' ) , 2 ) ;
135+ return redent ( elements . map ( ( element ) => formatElement ( element , options ) ) . join ( '\n' ) , 2 ) ;
98136}
99137
100138export function formatMessage (
0 commit comments