-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.spec.tsx
231 lines (207 loc) · 5.5 KB
/
index.spec.tsx
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import test from "tape";
import flattenChildren from "./index";
import TestRenderer, { ReactTestRendererTree } from "react-test-renderer";
import React, { Fragment, FunctionComponent, ReactNode } from "react";
import { isElement } from "react-is";
const Assert: FunctionComponent<{
assert: (result: ReturnType<typeof flattenChildren>) => void;
children: ReactNode
}> = (props: any) => {
const result = flattenChildren(props.children);
props.assert(result);
return <div>{result}</div>;
};
function getRenderedChildren(rendererTree: ReactTestRendererTree | null) {
if (!rendererTree || !rendererTree.rendered) throw new Error("No render");
// if rendered is an array, return the array of children from each tree
if (Array.isArray(rendererTree.rendered)) {
return rendererTree.rendered.reduce((acc: Array<any>, tree: ReactTestRendererTree) => {
if (tree.props && tree.props.children) {
return acc.concat(tree.props.children);
} else {
throw new Error("No rendered props.children in one of the trees");
}
}, []);
}
// if rendered is a single tree
if (!rendererTree.rendered.props || !rendererTree.rendered.props.children)
throw new Error("No rendered props.children");
return rendererTree.rendered.props.children as Array<any>;
}
test("simple children", function(t) {
t.plan(5);
TestRenderer.create(
<Assert
assert={result => {
// this inner function tests the return value of flattenChildren
t.equal(result.length, 4, "array length");
t.equal(
isElement(result[0]) && result[0].key,
".0",
"0th element key"
);
t.equal(result[1], "two", "1st text child");
t.equal(
isElement(result[2]) && result[2].key,
".2",
"2nd element key"
);
t.equal(result[3], "10", "3rd number child");
}}
>
{/* these are our elements */}
<span>one</span>
two
<span>three</span>
10
</Assert>
);
});
test("conditional children", function(t) {
t.plan(4);
TestRenderer.create(
<Assert
assert={result => {
t.equal(result.length, 3, "array length");
t.equal(
isElement(result[0]) && result[0].key,
".0",
"0th element key"
);
t.equal(
isElement(result[1]) && result[1].key,
".2",
"2nd element key"
);
t.equal(
isElement(result[2]) && result[2].key,
".4",
"4th element key"
);
}}
>
<span>one</span>
{false}
<span>three</span>
{null}
<span>five</span>
</Assert>
);
});
test("keyed children", function(t) {
t.plan(2);
TestRenderer.create(
<Assert
assert={result => {
t.equal(result.length, 5, "array length");
t.deepEqual(
result.map((c: any) => c.key),
[".$one", ".$two", undefined, ".$four", ".4"],
"element keys"
);
}}
>
<span key="one">one</span>
<span key="two">two</span>
three
<span key="four">four</span>
<span>five</span>
</Assert>
);
});
test("fragment children", function(t) {
t.plan(2);
TestRenderer.create(
<Assert
assert={result => {
t.equal(result.length, 3, "array length");
t.deepEqual(
result.map((c: any) => c.key),
[".0..$one", ".0..$two", ".1..$three"],
"element keys"
);
}}
>
<>
<span key="one">one</span>
<span key="two">two</span>
</>
<>
<span key="three">three</span>
</>
</Assert>
);
});
test("keyed fragment children", function(t) {
t.plan(2);
TestRenderer.create(
<Assert
assert={result => {
t.equal(result.length, 3, "array length");
t.deepEqual(
result.map((c: any) => c.key),
[".$apple..$one", ".$apple..$two", ".$banana..$three"],
"element keys"
);
}}
>
<Fragment key="apple">
<span key="one">one</span>
<span key="two">two</span>
</Fragment>
<Fragment key="banana">
<span key="three">three</span>
</Fragment>
</Assert>
);
});
test("array children", function(t) {
t.plan(2);
TestRenderer.create(
<Assert
assert={result => {
t.equal(result.length, 5, "array length");
t.deepEqual(
result.map((c: any) => c.key),
[".0", undefined, ".1:$apple", ".1:2", ".2"],
"element keys"
);
}}
>
<span>one</span>
{["two", <span key="apple">three</span>, <span>four</span>]}
<span>five</span>
</Assert>
);
});
test("renders through to react", function(t) {
t.plan(3);
const result = TestRenderer.create(
<Assert
assert={result => {
t.equal(result.length, 6, "array length");
}}
>
<span>head</span>
<Fragment key="apple">
<span key="one">one</span>
<span key="two">two</span>
</Fragment>
<span>body</span>
{false}
<Fragment key="banana">
<span key="three">three</span>
</Fragment>
<span>foot</span>
</Assert>
).toTree();
// and this tests that the array returned by flattenChildren will be rendered
// correctly by React
const children = getRenderedChildren(result);
t.equal(children.length, 6, "props.children.length");
t.deepEqual(
children.map((c: any) => c.key),
[".0", ".$apple..$one", ".$apple..$two", ".2", ".$banana..$three", ".5"],
"element keys"
);
});