This repository has been archived by the owner on Jul 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathNetwork.test.js
1358 lines (1115 loc) · 44.4 KB
/
Network.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* Useful during debugging
* =======================
*
* console.log(JSON.stringify(output, null, 2));
*
* for (let i in network.body.edges) {
* let edge = network.body.edges[i];
* console.log("" + i + ": from: " + edge.fromId + ", to: " + edge.toId);
* }
*/
var fs = require('fs');
var assert = require('assert');
var DataSet = require('../lib/DataSet');
var Network = require('../lib/network/Network');
var stdout = require('test-console').stdout;
var Validator = require("./../lib/shared/Validator").default;
var canvasMockify = require('./canvas-mock');
var {allOptions, configureOptions} = require('./../lib/network/options.js');
//var {printStyle} = require('./../lib/shared/Validator');
/**
* Merge all options of object b into object b
* @param {Object} a
* @param {Object} b
* @return {Object} a
*
* Adapted merge() in dotparser.js
*/
function merge (a, b) {
if (!a) {
a = {};
}
if (b) {
for (var name in b) {
if (b.hasOwnProperty(name)) {
if (typeof b[name] === 'object') {
a[name] = merge(a[name], b[name]);
} else {
a[name] = b[name];
}
}
}
}
return a;
}
/**
* Load legacy-style (i.e. not module) javascript files into the given context.
*/
function include(list, context) {
if (!(list instanceof Array)) {
list = [list];
}
for (var n in list) {
var path = list[n];
var arr = [fs.readFileSync(path) + ''];
eval.apply(context, arr);
}
}
/**
* Defined network consists of two sub-networks:
*
* - 1-2-3-4
* - 11-12-13-14
*
* For reference, this is the sample network of issue #1218
*/
function createSampleNetwork(options) {
var NumInitialNodes = 8;
var NumInitialEdges = 6;
var nodes = new DataSet([
{id: 1, label: '1'},
{id: 2, label: '2'},
{id: 3, label: '3'},
{id: 4, label: '4'},
{id: 11, label: '11'},
{id: 12, label: '12'},
{id: 13, label: '13'},
{id: 14, label: '14'},
]);
var edges = new DataSet([
{from: 1, to: 2},
{from: 2, to: 3},
{from: 3, to: 4},
{from: 11, to: 12},
{from: 12, to: 13},
{from: 13, to: 14},
]);
// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var defaultOptions = {
layout: {
randomSeed: 8
},
edges: {
smooth: {
type: 'continuous' // avoid dynamic here, it adds extra hidden nodes
}
}
};
options = merge(defaultOptions, options);
var network = new Network(container, data, options);
assertNumNodes(network, NumInitialNodes);
assertNumEdges(network, NumInitialEdges);
return [network, data, NumInitialNodes, NumInitialEdges];
};
/**
* Create a cluster for the dynamic data change cases.
*
* Works on the network created by createSampleNetwork().
*
* This is actually a pathological case; there are two separate sub-networks and
* a cluster is made of two nodes, each from one of the sub-networks.
*/
function createCluster(network) {
var clusterOptionsByData = {
joinCondition: function(node) {
if (node.id == 1 || node.id == 11) return true;
return false;
},
clusterNodeProperties: {id:"c1", label:'c1'}
}
network.cluster(clusterOptionsByData);
}
/**
* Display node/edge state, useful during debugging
*/
function log(network) {
console.log(Object.keys(network.body.nodes));
console.log(network.body.nodeIndices);
console.log(Object.keys(network.body.edges));
console.log(network.body.edgeIndices);
};
/**
* Note that only the node and edges counts are asserted.
* This might be done more thoroughly by explicitly checking the id's
*/
function assertNumNodes(network, expectedPresent, expectedVisible) {
if (expectedVisible === undefined) expectedVisible = expectedPresent;
assert.equal(Object.keys(network.body.nodes).length, expectedPresent, "Total number of nodes does not match");
assert.equal(network.body.nodeIndices.length, expectedVisible, "Number of visible nodes does not match");
};
/**
* Comment at assertNumNodes() also applies.
*/
function assertNumEdges(network, expectedPresent, expectedVisible) {
if (expectedVisible === undefined) expectedVisible = expectedPresent;
assert.equal(Object.keys(network.body.edges).length, expectedPresent, "Total number of edges does not match");
assert.equal(network.body.edgeIndices.length, expectedVisible, "Number of visible edges does not match");
};
/**
* Check if the font options haven't changed.
*
* This is to guard against future code changes; a lot of the code deals with particular properties of
* the font options.
* If any assertion fails here, all code in Network handling fonts should be checked.
*/
function checkFontProperties(fontItem, checkStrict = true) {
var knownProperties = [
'color',
'size',
'face',
'background',
'strokeWidth',
'strokeColor',
'align',
'multi',
'vadjust',
'bold',
'boldital',
'ital',
'mono',
];
// All properties in fontItem should be known
for (var prop in fontItem) {
if (prop === '__type__') continue; // Skip special field in options definition
if (!fontItem.hasOwnProperty(prop)) continue;
assert(knownProperties.indexOf(prop) !== -1, "Unknown font option '" + prop + "'");
}
if (!checkStrict) return;
// All known properties should be present
var keys = Object.keys(fontItem);
for (var n in knownProperties) {
var prop = knownProperties[n];
assert(keys.indexOf(prop) !== -1, "Missing known font option '" + prop + "'");
}
}
describe('Network', function () {
before(function() {
this.jsdom_global = canvasMockify("<div id='mynetwork'></div>");
this.container = document.getElementById('mynetwork');
});
after(function() {
this.jsdom_global();
});
/////////////////////////////////////////////////////
// Local helper methods for Edge and Node testing
/////////////////////////////////////////////////////
/**
* Simplify network creation for local tests
*/
function createNetwork(options) {
var [network, data, numNodes, numEdges] = createSampleNetwork(options);
return network;
}
function firstNode(network) {
for (var id in network.body.nodes) {
return network.body.nodes[id];
}
return undefined;
}
function firstEdge(network) {
for (var id in network.body.edges) {
return network.body.edges[id];
}
return undefined;
}
function checkChooserValues(item, chooser, labelChooser) {
if (chooser === 'function') {
assert.equal(typeof item.chooser, 'function');
} else {
assert.equal(item.chooser, chooser);
}
if (labelChooser === 'function') {
assert.equal(typeof item.labelModule.fontOptions.chooser, 'function');
} else {
assert.equal(item.labelModule.fontOptions.chooser, labelChooser);
}
}
/////////////////////////////////////////////////////
// End Local helper methods for Edge and Node testing
/////////////////////////////////////////////////////
/**
* Helper function for clustering
*/
function clusterTo(network, clusterId, nodeList, allowSingle) {
var options = {
joinCondition: function(node) {
return nodeList.indexOf(node.id) !== -1;
},
clusterNodeProperties: {
id: clusterId,
label: clusterId
}
}
if (allowSingle === true) {
options.clusterNodeProperties.allowSingleNodeCluster = true
}
network.cluster(options);
}
/**
* At time of writing, this test detected 22 out of 33 'illegal' loops.
* The real deterrent is eslint rule 'guard-for-in`.
*/
it('can deal with added fields in Array.prototype', function (done) {
var canvas = window.document.createElement('canvas');
Array.prototype.foo = 1; // Just add anything to the prototype
Object.prototype.bar = 2; // Let's screw up hashes as well
// The network should just run without throwing errors
try {
var [network, data, numNodes, numEdges] = createSampleNetwork({});
// Do some stuff to trigger more errors
clusterTo(network, 'c1', [1,2,3]);
data.nodes.remove(1);
network.openCluster('c1');
clusterTo(network, 'c1', [4], true);
clusterTo(network, 'c2', ['c1'], true);
clusterTo(network, 'c3', ['c2'], true);
data.nodes.remove(4);
} catch(e) {
delete Array.prototype.foo; // Remove it again so as not to confuse other tests.
delete Object.prototype.bar;
assert(false, "Got exception:\n" + e.stack);
}
delete Array.prototype.foo; // Remove it again so as not to confuse other tests.
delete Object.prototype.bar;
done();
});
/**
* This is a fix on one issue (#3543), but in fact **all* options for all API calls should
* remain unchanged.
* TODO: extend test for all API calls with options, see #3548
*/
it('does not change the options object passed to fit()', function() {
var [network, data, numNodes, numEdges] = createSampleNetwork({});
var options = {};
network.fit(options);
// options should still be empty
for (var prop in options) {
assert(!options.hasOwnProperty(prop), 'No properties should be present in options, detected property: ' + prop);
}
});
it('does not crash when dataChanged is triggered when setting options on first initialization ', function() {
// The init should succeed without an error thrown.
var options = {
nodes: {
physics: false // any value here triggered the error
}
};
createSampleNetwork(options);
// Do the other values as well that can cause this./
// 'any values' applies here as well, expecting no throw
options = {edges: {physics: false}};
createSampleNetwork(options);
options = {nodes: {hidden: false}};
createSampleNetwork(options);
options = {edges: {hidden: false}};
createSampleNetwork(options);
});
it('can deal with null data', function() {
// While we're at it, try out other silly values as well
// All the following are wrong, but none should lead to a crash
var awkwardData = [
null,
[1,2,3],
42,
'meow'
];
var container = document.getElementById('mynetwork');
for (var n = 0; n < awkwardData.length; ++n) {
var network = new Network(container, awkwardData[n], {}); // Should not throw
}
});
describe('Node', function () {
it('has known font options', function () {
var network = createNetwork({});
checkFontProperties(network.nodesHandler.defaultOptions.font);
checkFontProperties(allOptions.nodes.font);
checkFontProperties(configureOptions.nodes.font, false);
});
/**
* NOTE: choosify tests of Node and Edge are parallel
* TODO: consolidate this is necessary
*/
it('properly handles choosify input', function () {
// check defaults
var options = {};
var network = createNetwork(options);
checkChooserValues(firstNode(network), true, true);
// There's no point in checking invalid values here; these are detected by the options parser
// and subsequently handled as missing input, thus assigned defaults
// check various combinations of valid input
options = {nodes: {chosen: false}};
network = createNetwork(options);
checkChooserValues(firstNode(network), false, false);
options = {nodes: {chosen: { node:true, label:false}}};
network = createNetwork(options);
checkChooserValues(firstNode(network), true, false);
options = {nodes: {chosen: {
node:true,
label: function(value, id, selected, hovering) {}
}}};
network = createNetwork(options);
checkChooserValues(firstNode(network), true, 'function');
options = {nodes: {chosen: {
node: function(value, id, selected, hovering) {},
label:false,
}}};
network = createNetwork(options);
checkChooserValues(firstNode(network), 'function', false);
});
}); // Node
describe('Edge', function () {
it('has known font options', function () {
var network = createNetwork({});
checkFontProperties(network.edgesHandler.defaultOptions.font);
checkFontProperties(allOptions.edges.font);
checkFontProperties(configureOptions.edges.font, false);
});
/**
* NOTE: choosify tests of Node and Edge are parallel
* TODO: consolidate this is necessary
*/
it('properly handles choosify input', function () {
// check defaults
var options = {};
var network = createNetwork(options);
checkChooserValues(firstEdge(network), true, true);
// There's no point in checking invalid values here; these are detected by the options parser
// and subsequently handled as missing input, thus assigned defaults
// check various combinations of valid input
options = {edges: {chosen: false}};
network = createNetwork(options);
checkChooserValues(firstEdge(network), false, false);
options = {edges: {chosen: { edge:true, label:false}}};
network = createNetwork(options);
checkChooserValues(firstEdge(network), true, false);
options = {edges: {chosen: {
edge:true,
label: function(value, id, selected, hovering) {}
}}};
network = createNetwork(options);
checkChooserValues(firstEdge(network), true, 'function');
options = {edges: {chosen: {
edge: function(value, id, selected, hovering) {},
label:false,
}}};
network = createNetwork(options);
checkChooserValues(firstEdge(network), 'function', false);
});
/**
* Support routine for next unit test
*/
function createDataforColorChange() {
var nodes = new DataSet([
{id: 1, label: 'Node 1' }, // group:'Group1'},
{id: 2, label: 'Node 2', group:'Group2'},
{id: 3, label: 'Node 3'},
]);
// create an array with edges
var edges = new DataSet([
{id: 1, from: 1, to: 2},
{id: 2, from: 1, to: 3, color: { inherit: 'to'}},
{id: 3, from: 3, to: 3, color: { color: '#00FF00'}},
{id: 4, from: 2, to: 3, color: { inherit: 'from'}},
]);
var data = {
nodes: nodes,
edges: edges
};
return data;
}
/**
* Unit test for fix of #3350
*
* The issue is that changing color options is not registered in the nodes.
* We test the updates the color options in the general edges options here.
*/
it('sets inherit color option for edges on call to Network.setOptions()', function () {
var container = document.getElementById('mynetwork');
var data = createDataforColorChange();
var options = {
"edges" : { "color" : { "inherit" : "to" } },
};
// Test passing options on init.
var network = new Network(container, data, options);
var edges = network.body.edges;
assert.equal(edges[1].options.color.inherit, 'to'); // new default
assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
assert.equal(edges[3].options.color.inherit, false); // has explicit color
assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
// Sanity check: colors should still be defaults
assert.equal(edges[1].options.color.color, network.edgesHandler.options.color.color);
// Override the color value - inherit returns to default
network.setOptions({ edges:{color: {}}});
assert.equal(edges[1].options.color.inherit, 'from'); // default
assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
assert.equal(edges[3].options.color.inherit, false); // has explicit color
assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
// Check no options
network = new Network(container, data, {});
edges = network.body.edges;
assert.equal(edges[1].options.color.inherit, 'from'); // default
assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
assert.equal(edges[3].options.color.inherit, false); // has explicit color
assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
// Set new value
network.setOptions(options);
assert.equal(edges[1].options.color.inherit, 'to');
assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
assert.equal(edges[3].options.color.inherit, false); // has explicit color
assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
/*
// Useful for debugging
console.log('===================================');
console.log(edges[1].options.color);
console.log(edges[1].options.color.__proto__);
console.log(edges[1].options);
console.log(edges[1].options.__proto__);
console.log(edges[1].edgeOptions);
*/
});
it('sets inherit color option for specific edge', function () {
var container = document.getElementById('mynetwork');
var data = createDataforColorChange();
// Check no options
var network = new Network(container, data, {});
var edges = network.body.edges;
assert.equal(edges[1].options.color.inherit, 'from'); // default
assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
assert.equal(edges[3].options.color.inherit, false); // has explicit color
assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
// Set new value
data.edges.update({id: 1, color: { inherit: 'to'}});
assert.equal(edges[1].options.color.inherit, 'to'); // Only this changed
assert.equal(edges[2].options.color.inherit, 'to');
assert.equal(edges[3].options.color.inherit, false); // has explicit color
assert.equal(edges[4].options.color.inherit, 'from');
});
/**
* Perhaps TODO: add unit test for passing string value for color option
*/
it('sets color value for edges on call to Network.setOptions()', function () {
var container = document.getElementById('mynetwork');
var data = createDataforColorChange();
var defaultColor = '#848484'; // From defaults
var color = '#FF0000';
var options = {
"edges" : { "color" : { "color" : color } },
};
// Test passing options on init.
var network = new Network(container, data, options);
var edges = network.body.edges;
assert.equal(edges[1].options.color.color, color);
assert.equal(edges[1].options.color.inherit, false); // Explicit color, so no inherit
assert.equal(edges[2].options.color.color, color);
assert.equal(edges[2].options.color.inherit, 'to'); // Local value overrides! (bug according to docs)
assert.notEqual(edges[3].options.color.color, color); // Has own value
assert.equal(edges[3].options.color.inherit, false); // Explicit color, so no inherit
assert.equal(edges[4].options.color.color, color);
// Override the color value - all should return to default
network.setOptions({ edges:{color: {}}});
assert.equal(edges[1].options.color.color, defaultColor);
assert.equal(edges[1].options.color.inherit, 'from');
assert.equal(edges[2].options.color.color, defaultColor);
assert.notEqual(edges[3].options.color.color, color); // Has own value
assert.equal(edges[4].options.color.color, defaultColor);
// Check no options
network = new Network(container, data, {});
edges = network.body.edges;
// At this point, color has not changed yet
assert.equal(edges[1].options.color.color, defaultColor);
assert.equal(edges[1].options.color.highlight, defaultColor);
assert.equal(edges[1].options.color.inherit, 'from');
assert.notEqual(edges[3].options.color.color, color); // Has own value
// Set new Value
network.setOptions(options);
assert.equal(edges[1].options.color.color, color);
assert.equal(edges[1].options.color.highlight, defaultColor); // Should not be changed
assert.equal(edges[1].options.color.inherit, false); // Explicit color, so no inherit
assert.equal(edges[2].options.color.color, color);
assert.notEqual(edges[3].options.color.color, color); // Has own value
assert.equal(edges[4].options.color.color, color);
});
/**
* Unit test for fix of #3500
* Checking to make sure edges that become unconnected due to node removal get reconnected
*/
it('has reconnected edges', function () {
var node1 = {id:1, label:"test1"};
var node2 = {id:2, label:"test2"};
var nodes = new DataSet([node1, node2]);
var edge = {id:1, from: 1, to:2};
var edges = new DataSet([edge]);
var data = {
nodes: nodes,
edges: edges
};
var container = document.getElementById('mynetwork');
var network = new Network(container, data);
//remove node causing edge to become disconnected
nodes.remove(node2.id);
var foundEdge = network.body.edges[edge.id];
assert.ok(foundEdge===undefined, "edge is still in state cache");
//add node back reconnecting edge
nodes.add(node2);
foundEdge = network.body.edges[edge.id];
assert.ok(foundEdge!==undefined, "edge is missing from state cache");
});
}); // Edge
describe('Clustering', function () {
it('properly handles options allowSingleNodeCluster', function() {
var [network, data, numNodes, numEdges] = createSampleNetwork();
data.edges.update({from: 1, to: 11,});
numEdges += 1;
assertNumNodes(network, numNodes);
assertNumEdges(network, numEdges);
clusterTo(network, 'c1', [3,4]);
numNodes += 1; // A clustering node is now hiding two nodes
numEdges += 1; // One clustering edges now hiding two edges
assertNumNodes(network, numNodes, numNodes - 2);
assertNumEdges(network, numEdges, numEdges - 2);
// Cluster of single node should fail, because by default allowSingleNodeCluster == false
clusterTo(network, 'c2', [14]);
assertNumNodes(network, numNodes, numNodes - 2); // Nothing changed
assertNumEdges(network, numEdges, numEdges - 2);
assert(network.body.nodes['c2'] === undefined); // Cluster not created
// Redo with allowSingleNodeCluster == true
clusterTo(network, 'c2', [14], true);
numNodes += 1;
numEdges += 1;
assertNumNodes(network, numNodes, numNodes - 3);
assertNumEdges(network, numEdges, numEdges - 3);
assert(network.body.nodes['c2'] !== undefined); // Cluster created
// allowSingleNodeCluster: true with two nodes
// removing one clustered node should retain cluster
clusterTo(network, 'c3', [11, 12], true);
numNodes += 1; // Added cluster
numEdges += 2;
assertNumNodes(network, numNodes, 6);
assertNumEdges(network, numEdges, 5);
data.nodes.remove(12);
assert(network.body.nodes['c3'] !== undefined); // Cluster should still be present
numNodes -= 1; // removed node
numEdges -= 3; // cluster edge C3-13 should be removed
assertNumNodes(network, numNodes, 6);
assertNumEdges(network, numEdges, 4);
});
it('removes nested clusters with allowSingleNodeCluster === true', function() {
var [network, data, numNodes, numEdges] = createSampleNetwork();
// Create a chain of nested clusters, three deep
clusterTo(network, 'c1', [4], true);
clusterTo(network, 'c2', ['c1'], true);
clusterTo(network, 'c3', ['c2'], true);
numNodes += 3;
numEdges += 3;
assertNumNodes(network, numNodes, numNodes - 3);
assertNumEdges(network, numEdges, numEdges - 3);
assert(network.body.nodes['c1'] !== undefined);
assert(network.body.nodes['c2'] !== undefined);
assert(network.body.nodes['c3'] !== undefined);
// The whole chain should be removed when the bottom-most node is deleted
data.nodes.remove(4);
numNodes -= 4;
numEdges -= 4;
assertNumNodes(network, numNodes);
assertNumEdges(network, numEdges);
assert(network.body.nodes['c1'] === undefined);
assert(network.body.nodes['c2'] === undefined);
assert(network.body.nodes['c3'] === undefined);
});
/**
* Check on fix for #1218
*/
it('connects a new edge to a clustering node instead of the clustered node', function () {
var [network, data, numNodes, numEdges] = createSampleNetwork();
createCluster(network);
numNodes += 1; // A clustering node is now hiding two nodes
numEdges += 2; // Two clustering edges now hide two edges
assertNumNodes(network, numNodes, numNodes - 2);
assertNumEdges(network, numEdges, numEdges - 2);
//console.log("Creating node 21")
data.nodes.update([{id: 21, label: '21'}]);
numNodes += 1; // New unconnected node added
assertNumNodes(network, numNodes, numNodes - 2);
assertNumEdges(network, numEdges, numEdges - 2); // edges unchanged
//console.log("Creating edge 21 pointing to 1");
// '1' is part of the cluster so should
// connect to cluster instead
data.edges.update([{from: 21, to: 1}]);
numEdges += 2; // A new clustering edge is hiding a new edge
assertNumNodes(network, numNodes, numNodes - 2); // nodes unchanged
assertNumEdges(network, numEdges, numEdges - 3);
});
/**
* Check on fix for #1315
*/
it('can uncluster a clustered node when a node is removed that has an edge to that cluster', function () {
// NOTE: this block is same as previous test
var [network, data, numNodes, numEdges] = createSampleNetwork();
createCluster(network);
numNodes += 1; // A clustering node is now hiding two nodes
numEdges += 2; // Two clustering edges now hide two edges
assertNumNodes(network, numNodes, numNodes - 2);
assertNumEdges(network, numEdges, numEdges - 2);
// End block same as previous test
//console.log("removing 12");
data.nodes.remove(12);
// NOTE:
// At this particular point, there are still the two edges for node 12 in the edges DataSet.
// If you want to do the delete correctly, these should also be deleted explictly from
// the edges DataSet. In the Network instance, however, this.body.nodes and this.body.edges
// should be correct, with the edges of 12 all cleared out.
// 12 was connected to 11, which is clustered
numNodes -= 1; // 12 removed, one less node
numEdges -= 3; // clustering edge c1-12 and 2 edges of 12 gone
assertNumNodes(network, numNodes, numNodes - 2);
assertNumEdges(network, numEdges, numEdges - 1);
//console.log("Unclustering c1");
network.openCluster("c1");
numNodes -= 1; // cluster node removed, one less node
numEdges -= 1; // clustering edge gone, regular edge visible
assertNumNodes(network, numNodes, numNodes); // all are visible again
assertNumEdges(network, numEdges, numEdges); // all are visible again
});
/**
* Check on fix for #1291
*/
it('can remove a node inside a cluster and then open that cluster', function () {
var [network, data, numNodes, numEdges] = createSampleNetwork();
var clusterOptionsByData = {
joinCondition: function(node) {
if (node.id == 1 || node.id == 2 || node.id == 3) return true;
return false;
},
clusterNodeProperties: {id:"c1", label:'c1'}
}
network.cluster(clusterOptionsByData);
numNodes += 1; // new cluster node
numEdges += 1; // 1 cluster edge expected
assertNumNodes(network, numNodes, numNodes - 3); // 3 clustered nodes
assertNumEdges(network, numEdges, numEdges - 3); // 3 edges hidden
//console.log("removing node 2, which is inside the cluster");
data.nodes.remove(2);
numNodes -= 1; // clustered node removed
numEdges -= 2; // edges removed hidden in cluster
assertNumNodes(network, numNodes, numNodes - 2); // view doesn't change
assertNumEdges(network, numEdges, numEdges - 1); // view doesn't change
//console.log("Unclustering c1");
network.openCluster("c1")
numNodes -= 1; // cluster node gone
numEdges -= 1; // cluster edge gone
assertNumNodes(network, numNodes, numNodes); // all visible
assertNumEdges(network, numEdges, numEdges); // all visible
//log(network);
});
/**
* Helper function for setting up a graph for testing clusterByEdgeCount()
*/
function createOutlierGraph() {
// create an array with nodes
var nodes = new DataSet([
{id: 1, label: '1', group:'Group1'},
{id: 2, label: '2', group:'Group2'},
{id: 3, label: '3', group:'Group3'},
{id: 4, label: '4', group:'Group4'},
{id: 5, label: '5', group:'Group4'}
]);
// create an array with edges
var edges = new DataSet([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);
// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {
"groups" : {
"Group1" : { level:1 },
"Group2" : { level:2 },
"Group3" : { level:3 },
"Group4" : { level:4 }
}
};
var network = new Network (container, data, options);
return network;
}
/**
* Check on fix for #3367
*/
it('correctly handles edge cases of clusterByEdgeCount()', function () {
/**
* Collect clustered id's
*
* All node id's in clustering nodes are collected into an array;
* The results for all clusters are returned as an array.
*
* Ordering of output depends on the order in which they are defined
* within nodes.clustering; strictly, speaking, the array and its items
* are collections, so order should not matter.
*/
var collectClusters = function(network) {
var clusters = [];
for(var n in network.body.nodes) {
var node = network.body.nodes[n];
if (node.containedNodes === undefined) continue; // clusters only
// Collect id's of nodes in the cluster
var nodes = [];
for(var m in node.containedNodes) {
nodes.push(m);
}
clusters.push(nodes);
}
return clusters;
}
/**
* Compare cluster data
*
* params are arrays of arrays of id's, e.g:
*
* [[1,3],[2,4]]
*
* Item arrays are the id's of nodes in a given cluster
*
* This comparison depends on the ordering; better
* would be to treat the items and values as collections.
*/
var compareClusterInfo = function(recieved, expected) {
if (recieved.length !== expected.length) return false;
for (var n = 0; n < recieved.length; ++n) {
var itema = recieved[n];
var itemb = expected[n];
if (itema.length !== itemb.length) return false;
for (var m = 0; m < itema.length; ++m) {
if (itema[m] != itemb[m]) return false; // != because values can be string or number
}
}
return true;
}
var assertJoinCondition = function(joinCondition, expected) {
var network = createOutlierGraph();
network.clusterOutliers({joinCondition: joinCondition});
var recieved = collectClusters(network);
//console.log(recieved);
assert(compareClusterInfo(recieved, expected),
'recieved:' + JSON.stringify(recieved) + '; '
+ 'expected: ' + JSON.stringify(expected));
};
// Should cluster 3,4,5:
var joinAll_ = function(n) { return true ; }
// Should cluster none:
var joinNone_ = function(n) { return false ; }
// Should cluster 4 & 5:
var joinLevel_ = function(n) { return n.level > 3 ; }
assertJoinCondition(undefined , [[1,3],[2,4,5]]);
assertJoinCondition(null , [[1,3],[2,4,5]]);