-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathway-projection-magic.test.js
45 lines (38 loc) · 1.21 KB
/
way-projection-magic.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
const { addReaction, connectMutable } = require("./way-projection-magic");
function scheduleAction(fn) {
return new Promise(resolve => {
setTimeout(() => {
resolve(fn());
}, 0);
});
}
describe("mutable way: no, no", () => {
test("Reaction should be called only when watched value is changed", async () => {
const state = {
customerHappiness: { today: 50, inTheFuture: 0 },
techDebs: { value: 25 }
};
const rewardRender = jest.fn(
customerHappiness => `<p>Reward: ${customerHappiness}</p>`
);
const projection = state => state.customerHappiness;
const rewardMaybeRender = connectMutable(state, projection, rewardRender);
// Initial rendering
rewardMaybeRender();
rewardMaybeRender();
rewardMaybeRender();
expect(rewardRender).toHaveBeenCalledTimes(1);
// Subscribe to changes
addReaction(rewardMaybeRender);
// Change non-observable value
await scheduleAction(() => {
state.techDebs.value = 40;
});
expect(rewardRender).toHaveBeenCalledTimes(1);
// Change observable value
await scheduleAction(() => {
state.customerHappiness.today = 70;
});
expect(rewardRender).toHaveBeenCalledTimes(2);
});
});