1
+ import type { ElementType } from 'react' ;
1
2
import type { ReactTestInstance } from 'react-test-renderer' ;
2
3
import {
3
4
EXPECTED_COLOR ,
@@ -55,12 +56,20 @@ export function checkHostElement(
55
56
}
56
57
}
57
58
59
+ export type FormatElementOptions = {
60
+ // Minimize used space.
61
+ minimal ?: boolean ;
62
+ } ;
63
+
58
64
/***
59
65
* Format given element as a pretty-printed string.
60
66
*
61
67
* @param element Element to format.
62
68
*/
63
- export function formatElement ( element : ReactTestInstance | null ) {
69
+ export function formatElement (
70
+ element : ReactTestInstance | null ,
71
+ { minimal = false } : FormatElementOptions = { } ,
72
+ ) {
64
73
if ( element == null ) {
65
74
return ' null' ;
66
75
}
@@ -74,7 +83,7 @@ export function formatElement(element: ReactTestInstance | null) {
74
83
// This prop is needed persuade the prettyFormat that the element is
75
84
// a ReactTestRendererJSON instance, so it is formatted as JSX.
76
85
$$typeof : Symbol . for ( 'react.test.json' ) ,
77
- type : element . type ,
86
+ type : formatElementType ( element . type ) ,
78
87
props : defaultMapProps ( props ) ,
79
88
children : childrenToDisplay ,
80
89
} ,
@@ -83,18 +92,47 @@ export function formatElement(element: ReactTestInstance | null) {
83
92
printFunctionName : false ,
84
93
printBasicPrototype : false ,
85
94
highlight : true ,
95
+ min : minimal ,
86
96
} ,
87
97
) ,
88
98
2 ,
89
99
) ;
90
100
}
91
101
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 ) {
93
131
if ( elements . length === 0 ) {
94
132
return ' (no elements)' ;
95
133
}
96
134
97
- return redent ( elements . map ( formatElement ) . join ( '\n' ) , 2 ) ;
135
+ return redent ( elements . map ( ( element ) => formatElement ( element , options ) ) . join ( '\n' ) , 2 ) ;
98
136
}
99
137
100
138
export function formatMessage (
0 commit comments