-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-condition.test.js
203 lines (182 loc) · 5.51 KB
/
react-condition.test.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
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
import React from "react";
import TestRenderer from "react-test-renderer";
import { If, ElseIf, Else, Switch, Case, Default } from "./react-condition.js";
function expectRenderToEqual(actual, expected) {
expect(TestRenderer.create(actual).toJSON()).toEqual(
TestRenderer.create(expected).toJSON()
);
}
function expectRenderToThrow(actual, error) {
const consoleError = console.error;
try {
console.error = () => {};
expect(() => TestRenderer.create(actual)).toThrow(error);
} finally {
console.error = consoleError;
}
}
describe("react-condition", () => {
describe("if", () => {
it("includes children if condition is truthy", () => {
expectRenderToEqual(<If test={"truthy"}>Truthy?</If>, "Truthy?");
});
it("excludes children if condition is falsey", () => {
expectRenderToEqual(<If test={0}>Truthy?</If>, null);
});
it("includes Else child if condition is falsey", () => {
expectRenderToEqual(
<If test={0}>
Truthy?
<Else>Falsey?</Else>
</If>,
"Falsey?"
);
});
it("excludes Else child if condition is truthy", () => {
expectRenderToEqual(
<If test={1}>
Truthy?
<Else>Falsey?</Else>
</If>,
"Truthy?"
);
});
it("evaluates ElseIf child if condition is falsey", () => {
expectRenderToEqual(
<If test={0}>
Truthy?
<ElseIf test={1}>Otherwise?</ElseIf>
</If>,
"Otherwise?"
);
});
it("does not evaluate If child if condition is falsey", () => {
expectRenderToEqual(
<If test={0}>
Truthy?
<If test={1}>Otherwise?</If>
</If>,
null
);
});
it("allows multiple nested ElseIf and Else cases", () => {
expectRenderToEqual(
<If test={0}>
Truthy?
<ElseIf test={0}>
Otherwise?
<Else>Not Otherwise?</Else>
</ElseIf>
<Else>Falsey?</Else>
</If>,
<>
{"Not Otherwise?"}
{"Falsey?"}
</>
);
});
it("supports then prop", () => {
expectRenderToEqual(<If test={1} then={"Truthy?"} />, "Truthy?");
expectRenderToEqual(<If test={0} then={"Truthy?"} />, null);
});
it("supports then else prop", () => {
expectRenderToEqual(
<If test={1} then={"Truthy?"} else={"Falsey?"} />,
"Truthy?"
);
expectRenderToEqual(
<If test={0} then={"Truthy?"} else={"Falsey?"} />,
"Falsey?"
);
});
it("supports legacy case prop", () => {
expectRenderToEqual(<If case={1} then={"Truthy?"} />, "Truthy?");
expectRenderToEqual(<If case={0} then={"Truthy?"} />, null);
});
describe("error cases", () => {
it("requires case", () => {
expectRenderToThrow(<If then={null} />, "<If> requires a `test` prop.");
});
it("requires either then or children", () => {
expectRenderToThrow(
<If test={1} />,
"<If> expects either a `then` prop or children."
);
});
it("requires then when using else", () => {
expectRenderToThrow(
<If test={1} else={null} />,
"<If> only use `else` prop alongside `then` prop."
);
});
});
});
describe("switch", () => {
it("includes child from matching expression", () => {
expectRenderToEqual(
<Switch expression="blue">
<Case value="red" then={"red"} />
<Case value="green" then={"green"} />
<Case value="blue" then={"blue"} />
</Switch>,
"blue"
);
});
it("includes single child from matching expression", () => {
expectRenderToEqual(
<Switch expression="blue">
<Case value="red" then={"red"} />
<Case value="green" then={"green"} />
<Case value="blue" then={"blue"} />
<Case value="blue" then={"blue?"} />
</Switch>,
"blue"
);
});
it("excludes all children from not matching expression", () => {
expectRenderToEqual(
<Switch expression="hot fucking pink">
<Case value="red" then={"red"} />
<Case value="green" then={"green"} />
<Case value="blue" then={"blue"} />
</Switch>,
null
);
});
it("includes child from matching expression but fallsback", () => {
expectRenderToEqual(
<Switch expression="hot fucking pink">
<Case value="red" then={"red"} />
<Case value="green" then={"green"} />
<Case value="blue" then={"blue"} />
<Default then={"no color"} />
</Switch>,
"no color"
);
});
describe("error cases", () => {
it("requires default at end", () => {
expectRenderToThrow(
<Switch expression="hot fucking pink">
<Case value="red" then={"red"} />
<Case value="green" then={"green"} />
<Default then={"no color"} />
<Case value="blue" then={"blue"} />
</Switch>,
"<Default> is required to be the last node if present."
);
});
it("requires case default or null", () => {
expectRenderToThrow(
<Switch expression="red">
<p>Uh oh 🙊🙉</p>
<Case value="red" then={"red"} />
<Case value="green" then={"green"} />
<Case value="blue" then={"blue"} />
</Switch>,
"<Switch> requires a child of type <Case>, <Default>, or null."
);
});
});
});
});