-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathReactP5Wrapper.test.tsx
347 lines (270 loc) · 11.7 KB
/
ReactP5Wrapper.test.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
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
341
342
343
344
345
346
347
import { render, RenderResult, waitFor } from "@testing-library/react";
import { renderToStaticMarkup, renderToString } from "react-dom/server";
import { assert, describe, expect, it, vi } from "vitest";
import { ReactP5Wrapper } from "../../src/components/ReactP5Wrapper";
import { P5WrapperClassName } from "../../src/constants/P5WrapperClassName";
import { type P5CanvasInstance } from "../../src/contracts/P5CanvasInstance";
import { type Sketch } from "../../src/contracts/Sketch";
function createSketch(
updateFunction?: P5CanvasInstance["updateWithProps"]
): Sketch {
return vi.fn(p5 => {
p5.setup = () => p5.createCanvas(720, 400);
p5.updateWithProps = updateFunction;
});
}
async function waitForCanvas(findByTestId: RenderResult["findByTestId"]) {
return await waitFor(async () => {
const wrapper = await findByTestId("wrapper");
const canvas = wrapper.querySelector("canvas");
assert(canvas instanceof HTMLCanvasElement);
return canvas;
});
}
async function waitForLoading(findByTestId: RenderResult["findByTestId"]) {
return await waitFor(async () => {
const loading = await findByTestId("loading");
assert(loading instanceof HTMLParagraphElement);
return loading;
});
}
describe("ReactP5Wrapper", () => {
describe("Rendering", () => {
describe("Client", () => {
it("Renders the canvas into the wrapping element", async () => {
const sketch = createSketch();
const { findByTestId } = render(<ReactP5Wrapper sketch={sketch} />);
const canvas = await waitForCanvas(findByTestId);
expect(canvas).toBeInstanceOf(HTMLCanvasElement);
});
it("Recreates the P5 instance when the sketch is changed", async () => {
const sketch = createSketch();
const { rerender, findByTestId } = render(
<ReactP5Wrapper sketch={sketch} />
);
rerender(<ReactP5Wrapper sketch={sketch} />);
const canvas = await waitForCanvas(findByTestId);
expect(canvas).toBeInstanceOf(HTMLCanvasElement);
});
it("Adds a utility css class to the wrapping element", async () => {
const sketch = createSketch();
const { findByTestId } = render(<ReactP5Wrapper sketch={sketch} />);
const wrapper = await findByTestId("wrapper");
expect(wrapper).toBeInstanceOf(HTMLDivElement);
expect(wrapper.className).toBe(P5WrapperClassName);
});
it("Unmounts the canvas when the element is removed from the DOM", async () => {
const sketch = createSketch();
const { container, unmount, findByTestId } = render(
<ReactP5Wrapper sketch={sketch} />
);
await waitForCanvas(findByTestId);
expect(container.innerHTML).not.toBe("");
unmount();
expect(container.innerHTML).toBe("");
});
it("Should not render anything when the `sketch` and `fallback` props are not provided", () => {
const { container } = render(<ReactP5Wrapper />);
expect(container.innerHTML).toBe("");
});
it("Should log an error to the console when the `sketch` prop is not provided", async () => {
const errorLogger = vi.fn();
const errorLoggerSpy = vi
.spyOn(console, "error")
.mockImplementation(errorLogger);
render(<ReactP5Wrapper />);
await waitFor(() => {
expect(errorLoggerSpy).toHaveBeenCalledOnce();
expect(errorLoggerSpy).toHaveBeenCalledWith(
"[ReactP5Wrapper] The `sketch` prop is required."
);
errorLoggerSpy.mockReset();
errorLoggerSpy.mockRestore();
});
});
it("Should use the fallback UI if the sketch is undefined on initial render", async () => {
const fallbackView = vi.fn(() => <div data-testid="fallback" />);
const { findByTestId } = render(
<ReactP5Wrapper fallback={fallbackView} />
);
const fallback = await findByTestId("fallback");
expect(fallbackView).toHaveBeenCalledOnce();
expect(fallback).toBeInTheDocument();
});
it("Should use the fallback UI if the sketch is undefined on future renders", async () => {
const sketch = createSketch();
const fallbackView = vi.fn(() => <div data-testid="fallback" />);
const { rerender, findByTestId } = render(
<ReactP5Wrapper fallback={() => <h1>Oh no</h1>} sketch={sketch} />
);
await waitForCanvas(findByTestId);
rerender(<ReactP5Wrapper fallback={fallbackView} sketch={undefined} />);
const fallback = await findByTestId("fallback");
expect(fallbackView).toHaveBeenCalledOnce();
expect(fallback).toBeInTheDocument();
});
it.skip("Should show the default loading UI when the `loading` prop is not set and the sketch is not yet loaded", async () => {
const sketch = createSketch();
const { findByTestId } = render(<ReactP5Wrapper sketch={sketch} />);
const loading = await waitForLoading(findByTestId);
expect(loading).toBeInstanceOf(HTMLParagraphElement);
expect(loading.innerHTML).toBe("🚀 Loading...");
});
it.skip("Should show the provided loading UI while the canvas has not yet rendered into the page", async () => {
const sketch = createSketch();
const LoadingView = vi.fn(() => (
<p data-testid="loading">Loading test...</p>
));
const { findByTestId } = render(
<ReactP5Wrapper loading={LoadingView} sketch={sketch} />
);
const loading = await waitForLoading(findByTestId);
expect(LoadingView).toHaveBeenCalledOnce();
expect(loading).toBeInstanceOf(HTMLParagraphElement);
expect(loading.innerHTML).toBe("Loading test...");
});
it("Should show the default error UI when the `error` prop is not set an error is thrown within the subtree of the wrapper", async () => {
const sketch = createSketch();
const ErrorChild = () => {
throw new Error("oops");
};
const { findByTestId } = render(
<ReactP5Wrapper sketch={sketch}>
<ErrorChild />
</ReactP5Wrapper>
);
const error = await findByTestId("error");
expect(error).toBeInstanceOf(HTMLParagraphElement);
expect(error.textContent).toBe("❌ - Something went wrong");
});
it("Should show the error UI when the `error` prop is set an error is thrown within the subtree of the wrapper", async () => {
const sketch = createSketch();
const ErrorView = vi.fn(error => {
assert(error instanceof Error);
return <div data-testid="error">Error: {error.message}</div>;
});
const ErrorChild = () => {
throw new Error("oops");
};
const { findByTestId } = render(
<ReactP5Wrapper error={ErrorView} sketch={sketch}>
<ErrorChild />
</ReactP5Wrapper>
);
const error = await findByTestId("error");
expect(ErrorView).toHaveBeenCalledTimes(2); // Seems there is a double-render issue with the error-boundary, not sure why since we are not in strict-mode during testing and so I assume it is a library issue instead.
expect(error).toBeInstanceOf(HTMLDivElement);
expect(error.innerHTML).toBe("Error: oops");
});
it("Should log the error when an error is thrown within the subtree of the wrapper", async () => {
const sketch = createSketch();
const ErrorView = vi.fn(() => <div data-testid="error" />);
const errorLogger = vi.fn();
const errorLoggerSpy = vi
.spyOn(console, "error")
.mockImplementation(errorLogger);
const ErrorChild = () => {
throw new Error("oops");
};
const { findByTestId } = render(
<ReactP5Wrapper error={ErrorView} sketch={sketch}>
<ErrorChild />
</ReactP5Wrapper>
);
await findByTestId("error");
expect(errorLoggerSpy).toHaveBeenCalledTimes(2);
expect(errorLoggerSpy).toHaveBeenCalledWith(
expect.stringContaining(
"[ReactP5Wrapper] The error boundary was triggered. The error message was:"
)
);
expect(errorLoggerSpy).toHaveBeenCalledWith(
expect.stringContaining("oops")
);
errorLoggerSpy.mockReset();
errorLoggerSpy.mockRestore();
});
});
describe("Server", () => {
it("Renders as expected when using `renderToString`", () => {
const sketch = createSketch();
const StringComponent = renderToString(
<ReactP5Wrapper sketch={sketch} />
);
expect(StringComponent).toBe(
`<!--$--><div class="${P5WrapperClassName}" data-testid="wrapper"></div><!--/$-->`
);
});
it("Renders as expected when using `renderToStaticMarkup`", () => {
const sketch = createSketch();
const StaticComponent = renderToStaticMarkup(
<ReactP5Wrapper sketch={sketch} />
);
expect(StaticComponent).toBe(
`<div class="${P5WrapperClassName}" data-testid="wrapper"></div>`
);
});
});
});
describe("Updates", () => {
it("Calls `updateWithProps` when the component is mounted", async () => {
const updateFunction = vi.fn();
const sketch = createSketch(updateFunction);
const { findByTestId } = render(
<ReactP5Wrapper sketch={sketch} x={100} />
);
await waitForCanvas(findByTestId);
expect(sketch).toHaveBeenCalledOnce();
expect(updateFunction).toHaveBeenCalledOnce();
expect(updateFunction).toHaveBeenCalledWith({ x: 100 });
});
it("Calls `updateWithProps` when a prop value changes", async () => {
const updateFunction = vi.fn();
const sketch = createSketch(updateFunction);
const { rerender, findByTestId } = render(
<ReactP5Wrapper sketch={sketch} x={100} />
);
await waitForCanvas(findByTestId);
rerender(<ReactP5Wrapper sketch={sketch} x={200} />);
expect(sketch).toHaveBeenCalledOnce();
expect(updateFunction).toHaveBeenCalledTimes(2);
expect(updateFunction).toHaveBeenCalledWith({ x: 200 });
});
it("Calls `updateWithProps` when a prop is removed", async () => {
const updateFunction = vi.fn();
const sketch = createSketch(updateFunction);
const { rerender, findByTestId } = render(
<ReactP5Wrapper sketch={sketch} x={100} />
);
await waitForCanvas(findByTestId);
rerender(<ReactP5Wrapper sketch={sketch} />);
expect(sketch).toHaveBeenCalledOnce();
expect(updateFunction).toHaveBeenCalledTimes(2);
expect(updateFunction).toHaveBeenCalledWith({});
});
it("Calls `updateWithProps` when new props are added", async () => {
const updateFunction = vi.fn();
const sketch = createSketch(updateFunction);
const { rerender, findByTestId } = render(
<ReactP5Wrapper sketch={sketch} x={100} />
);
await waitForCanvas(findByTestId);
rerender(<ReactP5Wrapper sketch={sketch} x={200} y={50} />);
expect(sketch).toHaveBeenCalledOnce();
expect(updateFunction).toHaveBeenCalledTimes(2);
expect(updateFunction).toHaveBeenCalledWith({ x: 200, y: 50 });
});
it("Calls `updateWithProps` when props are traded", async () => {
const updateFunction = vi.fn();
const sketch = createSketch(updateFunction);
const { rerender, findByTestId } = render(
<ReactP5Wrapper sketch={sketch} x={100} />
);
await waitForCanvas(findByTestId);
rerender(<ReactP5Wrapper sketch={sketch} y={100} />);
expect(sketch).toHaveBeenCalledOnce();
expect(updateFunction).toHaveBeenCalledTimes(2);
expect(updateFunction).toHaveBeenCalledWith({ y: 100 });
});
});
});