-
Notifications
You must be signed in to change notification settings - Fork 385
/
wp-element-mock.js
112 lines (103 loc) · 2.39 KB
/
wp-element-mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* External dependencies
*/
import {
Children,
cloneElement,
Component,
createContext,
createElement,
createRef,
forwardRef,
Fragment,
isValidElement,
memo,
StrictMode,
useState,
useEffect,
useContext,
useReducer,
useCallback,
useMemo,
useRef,
useImperativeHandle,
useLayoutEffect,
useDebugValue,
lazy,
Suspense,
} from 'react';
import { isString } from 'lodash';
export {
createPortal,
findDOMNode,
render,
unmountComponentAtNode,
} from 'react-dom';
// Just pass these through from React.
export {
Children,
cloneElement,
Component,
createContext,
createElement,
createRef,
forwardRef,
Fragment,
isValidElement,
memo,
StrictMode,
useState,
useEffect,
useContext,
useReducer,
useCallback,
useMemo,
useRef,
useImperativeHandle,
useLayoutEffect,
useDebugValue,
lazy,
Suspense,
};
// These 3 functions below have to be defined here to use the correct React functions imported above.
// The first 2 are copied straight from @wordpress/element:src/react.js
export function concatChildren( ...childrenArguments ) {
return childrenArguments.reduce( ( result, children, i ) => {
Children.forEach( children, ( child, j ) => {
if ( child && 'string' !== typeof child ) {
child = cloneElement( child, {
key: [ i, j ].join(),
} );
}
result.push( child );
} );
return result;
}, [] );
}
export function switchChildrenNodeName( children, nodeName ) {
return children && Children.map( children, ( elt, index ) => {
if ( isString( elt ) ) {
return createElement( nodeName, { key: index }, elt );
}
const { children: childrenProp, ...props } = elt.props;
return createElement( nodeName, { key: index, ...props }, childrenProp );
} );
}
// This function is copied straight from @wordpress/element:src/raw-html.js
export const RawHTML = ( { children, ...props } ) => {
// The DIV wrapper will be stripped by serializer, unless there are
// non-children props present.
return createElement( 'div', {
dangerouslySetInnerHTML: { __html: children },
...props,
} );
};
// This can be re-exported after being imported from the actual package.
const { isEmptyElement } = require.requireActual( '@wordpress/element' );
export {
isEmptyElement,
};
/* This mock module is explicitly *not* copying `renderToString`.
* It's possible to simple copy the entire complex function, but most likely not needed at
* all for any tests this repo will be doing.
*/