forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowGraphDataNodes.test.ts
103 lines (81 loc) · 3.5 KB
/
flowGraphDataNodes.test.ts
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
import type { Engine } from "core/Engines";
import { NullEngine } from "core/Engines";
import type { FlowGraph, FlowGraphContext } from "core/FlowGraph";
import {
FlowGraphCoordinator,
FlowGraphGetVariableBlock,
FlowGraphSceneReadyEventBlock,
FlowGraphLogBlock,
FlowGraphAddNumberBlock,
FlowGraphRandomNumberBlock,
} from "core/FlowGraph";
import { Scene } from "core/scene";
describe("Flow Graph Data Nodes", () => {
let engine: Engine;
let scene: Scene;
let flowGraphCoordinator: FlowGraphCoordinator;
let flowGraph: FlowGraph;
let flowGraphContext: FlowGraphContext;
beforeEach(() => {
console.log = jest.fn();
engine = new NullEngine({
renderHeight: 256,
renderWidth: 256,
textureSize: 256,
deterministicLockstep: false,
lockstepMaxSteps: 1,
});
scene = new Scene(engine);
flowGraphCoordinator = new FlowGraphCoordinator({ scene });
flowGraph = flowGraphCoordinator.createGraph();
flowGraphContext = flowGraph.createContext();
});
it("Variable Block", () => {
const sceneReady = new FlowGraphSceneReadyEventBlock({ name: "SceneReady" });
flowGraph.addEventBlock(sceneReady);
const runCustomFunction = new FlowGraphLogBlock({ name: "Log" });
sceneReady.onDone.connectTo(runCustomFunction.onStart);
const getVariable = new FlowGraphGetVariableBlock();
getVariable.variableName.setValue("testVariable", flowGraphContext);
flowGraphContext.setVariable("testVariable", 42);
runCustomFunction.message.connectTo(getVariable.output);
// Test in a different context
const flowGraphContext2 = flowGraph.createContext();
getVariable.variableName.setValue("testVariable", flowGraphContext2);
flowGraphContext2.setVariable("testVariable", 43);
flowGraph.start();
scene.onReadyObservable.notifyObservers(scene);
expect(console.log).toHaveBeenCalledWith(42);
expect(console.log).toHaveBeenCalledWith(43);
});
it("Values are cached for the same execution id", () => {
const sceneReady = new FlowGraphSceneReadyEventBlock({ name: "SceneReady" });
flowGraph.addEventBlock(sceneReady);
const add = new FlowGraphAddNumberBlock();
const rnd = new FlowGraphRandomNumberBlock();
rnd.leftInput.setValue(0, flowGraphContext);
rnd.rightInput.setValue(1, flowGraphContext);
// add a number to itself, which should only trigger the random number block once and cache the result
add.leftInput.connectTo(rnd.output);
add.rightInput.connectTo(rnd.output);
// log ther result
const log = new FlowGraphLogBlock();
log.message.connectTo(add.output);
sceneReady.onDone.connectTo(log.onStart);
flowGraph.start();
let mockRandomIndex = 1;
const mockedRandom = (): number => {
return mockRandomIndex++;
};
// clear the random mock before calling
const random = jest.spyOn(global.Math, "random").mockImplementation(mockedRandom);
scene.onReadyObservable.notifyObservers(scene);
expect(random).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(2); // 1 + 1
random.mockRestore();
});
afterEach(() => {
scene.dispose();
engine.dispose();
});
});