-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathparseReactElement.spec.js
More file actions
210 lines (195 loc) · 4.57 KB
/
parseReactElement.spec.js
File metadata and controls
210 lines (195 loc) · 4.57 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* @flow */
import React, { Fragment } from 'react';
import parseReactElement from './parseReactElement';
const options = {};
describe('parseReactElement', () => {
it('should parse a react element with a string as children', () => {
expect(parseReactElement(<h1>Hello world</h1>, options)).toEqual({
type: 'ReactElement',
displayName: 'h1',
defaultProps: {},
props: {},
childrens: [
{
type: 'string',
value: 'Hello world',
},
],
});
});
it('should parse a react element with a function as children', () => {
expect(
parseReactElement(<h1>{() => <div>hello world</div>}</h1>, options)
).toEqual({
childrens: [
{
childrens: [
{
childrens: [{ type: 'string', value: 'hello world' }],
defaultProps: {},
displayName: 'div',
props: {},
type: 'ReactElement',
},
],
type: 'ReactFunction',
},
],
defaultProps: {},
displayName: 'h1',
props: {},
type: 'ReactElement',
});
});
it('should filter empty childrens', () => {
expect(
parseReactElement(
<h1>
Hello
{null}
{true}
{false}
{''}
world
</h1>,
options
)
).toEqual({
type: 'ReactElement',
displayName: 'h1',
defaultProps: {},
props: {},
childrens: [
{
type: 'string',
value: 'Hello',
},
{
type: 'string',
value: 'world',
},
],
});
});
it('should parse a single depth react element', () => {
expect(parseReactElement(<aaa foo="41" />, options)).toEqual({
type: 'ReactElement',
displayName: 'aaa',
props: {
foo: '41',
},
defaultProps: {},
childrens: [],
});
});
it('should parse a react element with an object as props', () => {
expect(
parseReactElement(<div a={{ aa: '1', bb: { cc: '3' } }} />, options)
).toEqual({
type: 'ReactElement',
displayName: 'div',
defaultProps: {},
props: {
a: { aa: '1', bb: { cc: '3' } },
},
childrens: [],
});
});
it('should parse a react element with another react element as props', () => {
expect(parseReactElement(<div a={<span b="42" />} />, options)).toEqual({
type: 'ReactElement',
displayName: 'div',
defaultProps: {},
props: {
a: <span b="42" />,
},
childrens: [],
});
});
it('should parse the react element defaultProps', () => {
const Foo = () => {};
Foo.defaultProps = {
bar: 'Hello Bar!',
baz: 'Hello Baz!',
};
expect(
parseReactElement(<Foo foo="Hello Foo!" bar="Hello world!" />, options)
).toEqual({
type: 'ReactElement',
displayName: 'Foo',
defaultProps: {
bar: 'Hello Bar!',
baz: 'Hello Baz!',
},
props: {
bar: 'Hello world!',
baz: 'Hello Baz!',
foo: 'Hello Foo!',
},
childrens: [],
});
});
it('should extract the component key', () => {
expect(parseReactElement(<div key="foo-1" />, options)).toEqual({
type: 'ReactElement',
displayName: 'div',
defaultProps: {},
props: {
key: 'foo-1',
},
childrens: [],
});
});
it('should extract the component ref', () => {
const refFn = () => 'foo';
expect(parseReactElement(<div ref={refFn} />, options)).toEqual({
type: 'ReactElement',
displayName: 'div',
defaultProps: {},
props: {
ref: refFn,
},
childrens: [],
});
// eslint-disable-next-line react/no-string-refs
expect(parseReactElement(<div ref="foo" />, options)).toEqual({
type: 'ReactElement',
displayName: 'div',
defaultProps: {},
props: {
ref: 'foo',
},
childrens: [],
});
});
it('should parse a react fragment', () => {
expect(
parseReactElement(
<Fragment key="foo">
<div />
<div />
</Fragment>,
options
)
).toEqual({
type: 'ReactFragment',
key: 'foo',
childrens: [
{
type: 'ReactElement',
displayName: 'div',
defaultProps: {},
props: {},
childrens: [],
},
{
type: 'ReactElement',
displayName: 'div',
defaultProps: {},
props: {},
childrens: [],
},
],
});
});
});