aboutsummaryrefslogtreecommitdiff
path: root/node_modules/dependency-graph/specs/dep_graph_spec.js
blob: 46d222e5a63b0267b2d796b84760dfae3697ac1e (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
var DepGraph = require('../lib/dep_graph').DepGraph;

describe('DepGraph', function () {

  it('should be able to add/remove nodes', function () {
    var graph = new DepGraph();

    graph.addNode('Foo');
    graph.addNode('Bar');

    expect(graph.hasNode('Foo')).toBe(true);
    expect(graph.hasNode('Bar')).toBe(true);
    expect(graph.hasNode('NotThere')).toBe(false);

    graph.removeNode('Bar');

    expect(graph.hasNode('Bar')).toBe(false);
  });

  it('should calculate its size', function () {
    var graph = new DepGraph();

    expect(graph.size()).toEqual(0);

    graph.addNode('Foo');
    graph.addNode('Bar');

    expect(graph.size()).toEqual(2);

    graph.removeNode('Bar');

    expect(graph.size()).toEqual(1);
  });

  it('should treat the node data parameter as optional and use the node name as data if node data was not given', function () {
    var graph = new DepGraph();

    graph.addNode('Foo');

    expect(graph.getNodeData('Foo')).toBe('Foo');
  });

  it('should be able to associate a node name with data on node add', function () {
    var graph = new DepGraph();

    graph.addNode('Foo', 'data');

    expect(graph.getNodeData('Foo')).toBe('data');
  });

  it('should be able to add undefined as node data', function () {
    var graph = new DepGraph();

    graph.addNode('Foo', undefined);

    expect(graph.getNodeData('Foo')).toBe(undefined);
  });

  it('should return true when using hasNode with a node which has falsy data', function () {
    var graph = new DepGraph();

    var falsyData = ['', 0, null, undefined, false];
    graph.addNode('Foo');

    falsyData.forEach(function(data) {
      graph.setNodeData('Foo', data);

      expect(graph.hasNode('Foo')).toBe(true);

      // Just an extra check to make sure that the saved data is correct
      expect(graph.getNodeData('Foo')).toBe(data);
    });
  });

  it('should be able to set data after a node was added', function () {
    var graph = new DepGraph();

    graph.addNode('Foo', 'data');
    graph.setNodeData('Foo', 'data2');

    expect(graph.getNodeData('Foo')).toBe('data2');
  });

  it('should throw an error if we try to set data for a non-existing node', function () {
    var graph = new DepGraph();

    expect(function () {
      graph.setNodeData('Foo', 'data');
    }).toThrow(new Error('Node does not exist: Foo'));
  });

  it('should throw an error if the node does not exists and we try to get data', function () {
    var graph = new DepGraph();

    expect(function () {
      graph.getNodeData('Foo');
    }).toThrow(new Error('Node does not exist: Foo'));
  });

  it('should do nothing if creating a node that already exists', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');

    graph.addDependency('a','b');

    graph.addNode('a');

    expect(graph.dependenciesOf('a')).toEqual(['b']);
  });

  it('should do nothing if removing a node that does not exist', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    expect(graph.hasNode('a')).toBe(true);

    graph.removeNode('a');
    expect(graph.hasNode('Foo')).toBe(false);

    graph.removeNode('a');
    expect(graph.hasNode('Foo')).toBe(false);
  });

  it('should be able to add dependencies between nodes', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');

    graph.addDependency('a','b');
    graph.addDependency('a','c');

    expect(graph.dependenciesOf('a')).toEqual(['b', 'c']);
  });

  it('should throw an error if a node does not exist and a dependency is added', function () {
    var graph = new DepGraph();

    graph.addNode('a');

    expect(function () {
      graph.addDependency('a','b');
    }).toThrow(new Error('Node does not exist: b'));
  });

  it('should detect cycles', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');
    graph.addNode('d');

    graph.addDependency('a', 'b');
    graph.addDependency('b', 'c');
    graph.addDependency('c', 'a');
    graph.addDependency('d', 'a');

    expect(function () {
      graph.dependenciesOf('b');
    }).toThrow(new Error('Dependency Cycle Found: b -> c -> a -> b'));
  });

  it('should allow cycles when configured', function () {
    var graph = new DepGraph({ circular: true });

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');
    graph.addNode('d');

    graph.addDependency('a', 'b');
    graph.addDependency('b', 'c');
    graph.addDependency('c', 'a');
    graph.addDependency('d', 'a');

    expect(graph.dependenciesOf('b')).toEqual(['a', 'c']);
    expect(graph.overallOrder()).toEqual(['c', 'b', 'a', 'd']);
  });

  it('should detect cycles in overall order', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');
    graph.addNode('d');

    graph.addDependency('a', 'b');
    graph.addDependency('b', 'c');
    graph.addDependency('c', 'a');
    graph.addDependency('d', 'a');

    expect(function () {
      graph.overallOrder();
    }).toThrow(new Error('Dependency Cycle Found: a -> b -> c -> a'));
  });

  it('should detect cycles in overall order when all nodes have dependants (incoming edges)', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');

    graph.addDependency('a', 'b');
    graph.addDependency('b', 'c');
    graph.addDependency('c', 'a');

    expect(function () {
      graph.overallOrder();
    }).toThrow(new Error('Dependency Cycle Found: a -> b -> c -> a'));
  });

  it('should detect cycles in overall order when there are several ' +
     'disconnected subgraphs (with one that does not have a cycle', function () {
    var graph = new DepGraph();

    graph.addNode('a_1');
    graph.addNode('a_2');
    graph.addNode('b_1');
    graph.addNode('b_2');
    graph.addNode('b_3');

    graph.addDependency('a_1', 'a_2');
    graph.addDependency('b_1', 'b_2');
    graph.addDependency('b_2', 'b_3');
    graph.addDependency('b_3', 'b_1');

    expect(function () {
      graph.overallOrder();
    }).toThrow(new Error('Dependency Cycle Found: b_1 -> b_2 -> b_3 -> b_1'));
  });

  it('should retrieve dependencies and dependants in the correct order', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');
    graph.addNode('d');

    graph.addDependency('a', 'd');
    graph.addDependency('a', 'b');
    graph.addDependency('b', 'c');
    graph.addDependency('d', 'b');

    expect(graph.dependenciesOf('a')).toEqual(['c', 'b', 'd']);
    expect(graph.dependenciesOf('b')).toEqual(['c']);
    expect(graph.dependenciesOf('c')).toEqual([]);
    expect(graph.dependenciesOf('d')).toEqual(['c', 'b']);

    expect(graph.dependantsOf('a')).toEqual([]);
    expect(graph.dependantsOf('b')).toEqual(['a','d']);
    expect(graph.dependantsOf('c')).toEqual(['a','d','b']);
    expect(graph.dependantsOf('d')).toEqual(['a']);
  });

  it('should be able to resolve the overall order of things', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');
    graph.addNode('d');
    graph.addNode('e');

    graph.addDependency('a', 'b');
    graph.addDependency('a', 'c');
    graph.addDependency('b', 'c');
    graph.addDependency('c', 'd');

    expect(graph.overallOrder()).toEqual(['d', 'c', 'b', 'a', 'e']);
  });

  it('should be able to only retrieve the "leaves" in the overall order', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');
    graph.addNode('d');
    graph.addNode('e');

    graph.addDependency('a', 'b');
    graph.addDependency('a', 'c');
    graph.addDependency('b', 'c');
    graph.addDependency('c', 'd');

    expect(graph.overallOrder(true)).toEqual(['d', 'e']);
  });

  it('should be able to give the overall order for a graph with several disconnected subgraphs', function () {
    var graph = new DepGraph();

    graph.addNode('a_1');
    graph.addNode('a_2');
    graph.addNode('b_1');
    graph.addNode('b_2');
    graph.addNode('b_3');

    graph.addDependency('a_1', 'a_2');
    graph.addDependency('b_1', 'b_2');
    graph.addDependency('b_2', 'b_3');

    expect(graph.overallOrder()).toEqual(['a_2', 'a_1', 'b_3', 'b_2', 'b_1']);
  });

  it('should give an empty overall order for an empty graph', function () {
    var graph = new DepGraph();

    expect(graph.overallOrder()).toEqual([]);
  });

  it('should still work after nodes are removed', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');
    graph.addDependency('a', 'b');
    graph.addDependency('b', 'c');

    expect(graph.dependenciesOf('a')).toEqual(['c', 'b']);

    graph.removeNode('c');

    expect(graph.dependenciesOf('a')).toEqual(['b']);
  });

  it('should clone an empty graph', function () {
    var graph = new DepGraph();
    expect(graph.size()).toEqual(0);
    var cloned = graph.clone();
    expect(cloned.size()).toEqual(0);

    expect(graph === cloned).toBe(false);
  });

  it('should clone a non-empty graph', function () {
    var graph = new DepGraph();

    graph.addNode('a');
    graph.addNode('b');
    graph.addNode('c');
    graph.addDependency('a', 'b');
    graph.addDependency('b', 'c');

    var cloned = graph.clone();

    expect(graph === cloned).toBe(false);
    expect(cloned.hasNode('a')).toBe(true);
    expect(cloned.hasNode('b')).toBe(true);
    expect(cloned.hasNode('c')).toBe(true);
    expect(cloned.dependenciesOf('a')).toEqual(['c', 'b']);
    expect(cloned.dependantsOf('c')).toEqual(['a', 'b']);

    // Changes to the original graph shouldn't affect the clone
    graph.removeNode('c');
    expect(graph.dependenciesOf('a')).toEqual(['b']);
    expect(cloned.dependenciesOf('a')).toEqual(['c', 'b']);

    graph.addNode('d');
    graph.addDependency('b', 'd');
    expect(graph.dependenciesOf('a')).toEqual(['d', 'b']);
    expect(cloned.dependenciesOf('a')).toEqual(['c', 'b']);
  });

  it('should only be a shallow clone', function () {
    var graph = new DepGraph();

    var data = {a: 42};
    graph.addNode('a', data);

    var cloned = graph.clone();
    expect(graph === cloned).toBe(false);
    expect(graph.getNodeData('a') === cloned.getNodeData('a')).toBe(true);

    graph.getNodeData('a').a = 43;
    expect(cloned.getNodeData('a').a).toEqual(43);

    cloned.setNodeData('a', {a: 42});
    expect(cloned.getNodeData('a').a).toEqual(42);
    expect(graph.getNodeData('a') === cloned.getNodeData('a')).toBe(false);
  });
});