-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConditional.test.tsx
More file actions
340 lines (304 loc) · 9.07 KB
/
Conditional.test.tsx
File metadata and controls
340 lines (304 loc) · 9.07 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* eslint import/first: 0 */
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const framework = jest.requireActual("@ensembleui/react-framework");
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
const unwrapWidgetSpy = jest.fn().mockImplementation(framework.unwrapWidget);
import { fireEvent, render, screen } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import type { ConditionalProps, ConditionalElement } from "../Conditional";
import {
Conditional,
hasProperStructure,
extractWidget,
extractCondition,
} from "../Conditional";
import "../index";
import { EnsembleScreen } from "../../runtime/screen";
jest.mock("react-markdown", jest.fn());
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
jest.mock("@ensembleui/react-framework", () => ({
...framework,
unwrapWidget: unwrapWidgetSpy,
}));
afterEach(() => {
jest.clearAllMocks();
});
describe("Conditional Component", () => {
test('renders the widget when "if" condition is met', () => {
const conditionalProps: ConditionalProps = {
conditions: [
{
if: "true",
Text: { text: "Widget A" },
},
{
else: null,
Text: { text: "Widget B" },
},
],
};
render(<Conditional conditions={conditionalProps.conditions} />);
expect(screen.getByText("Widget A")).not.toBeNull();
});
test('renders the widget when "elseif" condition is met', () => {
const conditionalProps: ConditionalProps = {
conditions: [
{
if: "false",
Text: { text: "Widget A" },
},
{
elseif: "true",
Text: { text: "Widget B" },
},
],
};
render(<Conditional conditions={conditionalProps.conditions} />);
expect(screen.getByText("Widget B")).not.toBeNull();
});
test('renders the widget when "else" condition is met', () => {
const conditionalProps: ConditionalProps = {
conditions: [
{
if: "false",
Text: { text: "Widget A" },
},
{
else: null,
Text: { text: "Widget C" },
},
],
};
render(<Conditional conditions={conditionalProps.conditions} />);
expect(screen.getByText("Widget C")).not.toBeNull();
});
test("renders nothing when no condition is met", () => {
const conditionalProps: ConditionalProps = {
conditions: [
{
if: "false",
Text: { text: "Widget A" },
},
{
if: "false",
Text: { text: "Widget AA" },
},
],
};
render(<Conditional conditions={conditionalProps.conditions} />);
expect(screen.queryByText("Widget A")).toBeNull();
});
});
describe("hasProperStructure Function", () => {
test("returns false for empty conditions", () => {
const [isValid] = hasProperStructure([]);
expect(isValid).toBe(false);
});
test("returns false for conditions without 'if' condition", () => {
const conditionalProps: ConditionalProps = {
conditions: [
{
elseif: "true",
Text: { text: "Widget A" },
},
{
else: null,
Text: { text: "Widget B" },
},
],
};
const [isValid] = hasProperStructure(conditionalProps.conditions);
expect(isValid).toBe(false);
});
test("returns false when a condition does not have exactly two properties", () => {
const conditionalProps: ConditionalProps = {
conditions: [
{
if: "true",
Text: { text: "Widget A" },
Text2: { text: "Widget B" },
},
{
else: null,
},
],
};
const [isValid] = hasProperStructure(conditionalProps.conditions);
expect(isValid).toBe(false);
});
it('returns false when "if" condition(s) is(are) not the initial condition(s) following "elseif" or "else"', () => {
const conditions = [
{
elseif: "false",
Text: { text: "Widget A" },
},
{
if: "true",
Text: { text: "Widget B" },
},
];
const [isValid] = hasProperStructure(conditions);
expect(isValid).toBe(false);
});
test("returns false for improper structure", () => {
const conditionalProps: ConditionalProps = {
conditions: [
{
if: "false",
Text: { text: "Widget A" },
},
{
elseif: "true",
Text: { text: "Widget B" },
},
{
else: null,
Text: { text: "Widget C" },
},
{
else: null,
Text: { text: "Widget D" },
},
],
};
const [isValid] = hasProperStructure(conditionalProps.conditions);
expect(isValid).toBe(false);
});
test("returns true for proper structure", () => {
const conditionalProps: ConditionalProps = {
conditions: [
{
if: "false",
Text: { text: "Widget A" },
},
{
elseif: "true",
Text: { text: "Widget B" },
},
{
else: null,
Text: { text: "Widget C" },
},
],
};
const [isValid] = hasProperStructure(conditionalProps.conditions);
expect(isValid).toBe(true);
});
});
describe("extractWidget Function", () => {
test("throws an error for improper structure", () => {
const condition = {
if: "true",
};
expect(() => extractWidget(condition)).toThrow();
});
test("extracts the correct widget", () => {
const condition = {
if: "true",
Text: { text: "Widget A" },
};
const widget = extractWidget(condition);
expect(widget).toEqual({ name: "Text", properties: { text: "Widget A" } });
});
});
describe("extractCondition Function", () => {
test("throws an error for improper structure", () => {
const condition = {
iff: "true",
Text: { text: "Widget A" },
};
expect(() =>
extractCondition(condition as unknown as ConditionalElement),
).toThrow();
});
test("extracts the correct condition", () => {
const condition = {
if: "1 === 1",
Text: { text: "Widget A" },
};
const extractedCondition = extractCondition(condition);
expect(extractedCondition).toBe("1 === 1");
});
});
describe("conditional widget memoization", () => {
it("should memoize branch widgets and prevent unnecessary re-renders", () => {
render(
<EnsembleScreen
screen={{
name: "test_conditional",
id: "test_conditional",
body: {
name: "Column",
properties: {
children: [
{
name: "Conditional",
properties: {
conditions: [
{
if: `\${ensemble.storage.get('number') < 0}`,
Text: {
text: "Less than 0",
},
},
{
if: `\${ensemble.storage.get('number') === 0}`,
Text: {
text: "Equals to 0",
},
},
{
if: `\${ensemble.storage.get('number') > 0}`,
Text: {
text: "Greater than 0",
},
},
],
},
},
{
name: "Button",
properties: {
label: "Increase",
onTap: {
executeCode:
"ensemble.storage.set('number', ensemble.storage.get('number') + 1)",
},
},
},
{
name: "Button",
properties: {
label: "Decrease",
onTap: {
executeCode:
"ensemble.storage.set('number', ensemble.storage.get('number') - 1)",
},
},
},
],
},
},
onLoad: { executeCode: 'ensemble.storage.set("number", -1)' },
}}
/>,
{
wrapper: BrowserRouter,
},
);
expect(unwrapWidgetSpy).toHaveBeenCalledTimes(1);
expect(screen.getByText("Less than 0")).not.toBeNull();
fireEvent.click(screen.getByText("Increase"));
expect(unwrapWidgetSpy).toHaveBeenCalledTimes(2);
expect(screen.getByText("Equals to 0")).not.toBeNull();
fireEvent.click(screen.getByText("Increase"));
expect(unwrapWidgetSpy).toHaveBeenCalledTimes(3);
expect(screen.getByText("Greater than 0")).not.toBeNull();
fireEvent.click(screen.getByText("Decrease"));
expect(unwrapWidgetSpy).toHaveBeenCalledTimes(3);
expect(screen.getByText("Equals to 0")).not.toBeNull();
fireEvent.click(screen.getByText("Decrease"));
expect(unwrapWidgetSpy).toHaveBeenCalledTimes(3);
expect(screen.getByText("Less than 0")).not.toBeNull();
});
});