-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathgraphnode.spec.js
More file actions
177 lines (141 loc) · 6.35 KB
/
graphnode.spec.js
File metadata and controls
177 lines (141 loc) · 6.35 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
import Rendergraph from "../../src/rendergraph.js";
import GraphNode from "../../src/graphnode.js";
import { ConnectException } from "../../src/exceptions.js";
describe("GraphNode", () => {
describe("#inputNames", () => {
let node_a, node_b;
beforeEach(() => {
node_a = new GraphNode(undefined, undefined, ["input_a", "input_b"], true);
node_b = new GraphNode(undefined, undefined, [], true);
});
test("should return a list of the input names for the node", () => {
expect(node_a.inputNames).toEqual(["input_a", "input_b"]);
expect(node_b.inputNames).toEqual([]);
});
});
describe("#maximumConnections", () => {
let node_a, node_b, node_c;
beforeEach(() => {
node_a = new GraphNode(undefined, undefined, ["input_a", "input_b"], false);
node_b = new GraphNode(undefined, undefined, [], true);
node_c = new GraphNode(undefined, undefined, ["input_a", "input_b"], true);
});
test("should return the maximum number of connections that can be made to the node (Infinity if number of connections is not limited)", () => {
expect(node_a.maximumConnections).toBe(Infinity);
expect(node_b.maximumConnections).toBe(0);
expect(node_c.maximumConnections).toBe(2);
});
});
describe("#inputs", () => {
let rendergraph, node_a, node_b, node_c;
beforeEach(() => {
rendergraph = new Rendergraph();
node_a = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_b = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_c = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
});
test("should return an array of the nodes connected as inputs", () => {
expect(node_a.inputs).toEqual([]);
node_b.connect(node_a);
expect(node_a.inputs).toEqual([node_b]);
node_c.connect(node_a);
expect(node_a.inputs).toEqual([node_b, node_c]);
});
});
describe("#outputs", () => {
let rendergraph, node_a, node_b, node_c;
beforeEach(() => {
rendergraph = new Rendergraph();
node_a = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_b = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_c = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
});
test("should return the nodes this node connects to as outputs", () => {
expect(node_a.outputs).toEqual([]);
node_a.connect(node_b);
expect(node_a.outputs).toEqual([node_b]);
node_a.connect(node_c);
expect(node_a.outputs).toEqual([node_b, node_c]);
});
});
describe("#connect()", () => {
let rendergraph, node_a, node_b, node_c, node_d;
beforeEach(() => {
rendergraph = new Rendergraph();
node_a = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_b = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_c = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_d = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
});
test("should return true if connection was successful", () => {
expect(node_a.connect(node_b)).toBe(true);
});
test("should allow connecting via a specified input port name", () => {
expect(
node_a.connect(
node_b,
"input_b"
)
).toBe(true);
const expected_result = [
{
destination: node_b,
source: node_a,
type: "name",
name: "input_b"
}
];
expect(rendergraph.getNamedInputsForNode(node_b)).toEqual(expected_result);
});
test("should throw ConnectException if target input is taken", () => {
node_c.connect(
node_b,
"input_a"
);
expect(node_a.connect.bind(node_a, node_b, "input_a")).toThrow(ConnectException);
});
test("should throw ConnectException if target inputs are all in use", () => {
node_c.connect(node_b);
node_d.connect(node_b);
expect(node_a.connect.bind(node_a, node_b)).toThrow(ConnectException);
});
test("should throw ConnectException if named input dosen't exist", () => {
expect(node_a.connect.bind(node_a, node_b, "non-existant port")).toThrow(
ConnectException
);
});
});
describe("#disconnect()", () => {
let rendergraph, node_a, node_b, node_c, node_d;
beforeEach(() => {
rendergraph = new Rendergraph();
node_a = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_b = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_c = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
node_d = new GraphNode(undefined, rendergraph, ["input_a", "input_b"], true);
});
test("should remove node from input list of target node", () => {
node_a.connect(node_b);
expect(node_b.inputs).toEqual([node_a]);
node_a.disconnect(node_b);
expect(node_b.inputs).toEqual([]);
});
test("should remove node from output list of this node", () => {
node_a.connect(node_b);
expect(node_a.outputs).toEqual([node_b]);
node_a.disconnect(node_b);
expect(node_a.outputs).toEqual([]);
});
test("should return true if disconnection was successful", () => {
node_a.connect(node_b);
expect(node_a.disconnect(node_b)).toBe(true);
});
test("should remove all outputs if no connection is specified", () => {
node_a.connect(node_b);
node_a.connect(node_c);
node_a.connect(node_d);
node_a.disconnect();
expect(node_a.outputs).toEqual([]);
});
});
});