-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Effect.test.mjs
96 lines (81 loc) · 2.4 KB
/
Effect.test.mjs
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
// @ts-check
import { createElement as h } from "react";
import { act, create } from "react-test-renderer";
import { assertEquals, assertStrictEquals } from "std/testing/asserts.ts";
import Effect from "./Effect.mjs";
Deno.test("`Effect` functionality.", () => {
/** @type {Array<{ callback: Function, args: Array<unknown> }>} */
const callbackCalls = [];
/** @param {Array<unknown>} args */
function callbackOuterA(...args) {
callbackCalls.push({ callback: callbackOuterA, args });
}
/** @param {Array<unknown>} args */
function callbackOuterB(...args) {
callbackCalls.push({ callback: callbackOuterB, args });
}
/** @param {Array<unknown>} args */
function callbackInner(...args) {
callbackCalls.push({ callback: callbackInner, args });
}
const children = "abc";
/** @type {import("react-test-renderer").ReactTestRenderer | undefined} */
let testRenderer;
act(() => {
// When mounted, callbacks should be called in nested first order.
testRenderer = create(
h(Effect, {
callback: callbackOuterA,
children: h(Effect, {
callback: callbackInner,
children,
}),
}),
);
});
const tr =
/** @type {import("react-test-renderer").ReactTestRenderer} */
(testRenderer);
assertEquals(callbackCalls, [
{ callback: callbackInner, args: [] },
{ callback: callbackOuterA, args: [] },
]);
assertStrictEquals(tr.toJSON(), children);
act(() => {
// When mounted, re-rendering shouldn’t result in the callbacks being called
// again.
tr.update(
h(Effect, {
callback: callbackOuterA,
children: h(Effect, {
callback: callbackInner,
children,
}),
}),
);
});
assertEquals(callbackCalls, [
{ callback: callbackInner, args: [] },
{ callback: callbackOuterA, args: [] },
]);
assertStrictEquals(tr.toJSON(), children);
act(() => {
// When mounted, re-rendering with a different callback should result in it
// being called.
tr.update(
h(Effect, {
callback: callbackOuterB,
children: h(Effect, {
callback: callbackInner,
children,
}),
}),
);
});
assertEquals(callbackCalls, [
{ callback: callbackInner, args: [] },
{ callback: callbackOuterA, args: [] },
{ callback: callbackOuterB, args: [] },
]);
assertStrictEquals(tr.toJSON(), children);
});