3
3
4
4
var d3sankey = require ( 'd3-sankey' ) ;
5
5
6
- var graph = {
6
+ var data = {
7
7
'nodes' : [ {
8
8
'node' : 0 ,
9
9
'name' : 'node0'
@@ -53,6 +53,8 @@ var graph = {
53
53
54
54
55
55
describe ( 'd3-sankey' , function ( ) {
56
+ var sankey ;
57
+ var graph ;
56
58
var margin = {
57
59
top : 10 ,
58
60
right : 10 ,
@@ -62,53 +64,67 @@ describe('d3-sankey', function() {
62
64
var width = 1200 - margin . left - margin . right ;
63
65
var height = 740 - margin . top - margin . bottom ;
64
66
65
- var s ;
66
-
67
67
beforeEach ( function ( ) {
68
- s = d3sankey
68
+ sankey = d3sankey
69
69
. sankey ( )
70
70
. nodeWidth ( 36 )
71
71
. nodePadding ( 10 )
72
- . nodes ( graph . nodes )
73
- . links ( graph . links )
72
+ . nodes ( data . nodes )
73
+ . links ( data . links )
74
74
. size ( [ width , height ] )
75
75
. iterations ( 32 ) ;
76
+
77
+ graph = sankey ( ) ;
76
78
} ) ;
77
79
80
+ function checkArray ( key , result ) {
81
+ var value = graph . nodes . map ( function ( obj ) {
82
+ return obj [ key ] ;
83
+ } ) ;
84
+ expect ( value ) . toEqual ( result , 'invalid property named ' + key ) ;
85
+ }
86
+
87
+ function checkRoundedArray ( key , result ) {
88
+ var value = graph . nodes . map ( function ( obj ) {
89
+ return Math . round ( obj [ key ] ) ;
90
+ } ) ;
91
+ expect ( value ) . toEqual ( result , 'invalid property named ' + key ) ;
92
+ }
93
+
78
94
it ( 'controls the width of nodes' , function ( ) {
79
- expect ( s . nodeWidth ( ) ) . toEqual ( 36 , 'incorrect nodeWidth' ) ;
95
+ expect ( sankey . nodeWidth ( ) ) . toEqual ( 36 , 'incorrect nodeWidth' ) ;
80
96
} ) ;
81
97
82
98
it ( 'controls the padding between nodes' , function ( ) {
83
- expect ( s . nodePadding ( ) ) . toEqual ( 10 , 'incorrect nodePadding' ) ;
99
+ expect ( sankey . nodePadding ( ) ) . toEqual ( 10 , 'incorrect nodePadding' ) ;
84
100
} ) ;
85
101
86
102
it ( 'controls the padding between nodes' , function ( ) {
87
- expect ( s . nodePadding ( ) ) . toEqual ( 10 , 'incorrect nodePadding' ) ;
103
+ expect ( sankey . nodePadding ( ) ) . toEqual ( 10 , 'incorrect nodePadding' ) ;
88
104
} ) ;
89
105
90
106
it ( 'keep a list of nodes' , function ( ) {
91
- var nodeNames = s ( ) . nodes . map ( function ( obj ) {
92
- return obj . name ;
93
- } ) ;
94
- expect ( nodeNames ) . toEqual ( [ 'node0' , 'node1' , 'node2' , 'node3' , 'node4' ] ) ;
107
+ checkArray ( 'name' , [ 'node0' , 'node1' , 'node2' , 'node3' , 'node4' ] ) ;
95
108
} ) ;
96
109
97
- it ( 'keep a list of nodes with x values' , function ( ) {
98
- var nodeNames = s ( ) . nodes . map ( function ( obj ) {
99
- return Math . floor ( obj . x0 ) ;
100
- } ) ;
101
- expect ( nodeNames ) . toEqual ( [ 0 , 0 , 381 , 762 , 1144 ] ) ;
110
+ it ( 'keep a list of nodes with x and y values' , function ( ) {
111
+ checkRoundedArray ( 'x0' , [ 0 , 0 , 381 , 763 , 1144 ] ) ;
112
+ checkRoundedArray ( 'y0' , [ 0 , 365 , 184 , 253 , 0 ] ) ;
113
+ } ) ;
114
+
115
+ it ( 'keep a list of nodes with positions in integer (depth, height)' , function ( ) {
116
+ checkArray ( 'depth' , [ 0 , 0 , 1 , 2 , 3 ] ) ;
117
+ checkArray ( 'height' , [ 3 , 3 , 2 , 1 , 0 ] ) ;
102
118
} ) ;
103
119
104
120
it ( 'keep a list of links' , function ( ) {
105
- var linkWidths = s ( ) . links . map ( function ( obj ) {
121
+ var linkWidths = sankey ( ) . links . map ( function ( obj ) {
106
122
return ( obj . width ) ;
107
123
} ) ;
108
124
expect ( linkWidths ) . toEqual ( [ 177.5 , 177.5 , 177.5 , 177.5 , 177.5 , 177.5 , 355 ] ) ;
109
125
} ) ;
110
126
111
127
it ( 'controls the size of the figure' , function ( ) {
112
- expect ( s . size ( ) ) . toEqual ( [ 1180 , 720 ] , 'incorrect size' ) ;
128
+ expect ( sankey . size ( ) ) . toEqual ( [ 1180 , 720 ] , 'incorrect size' ) ;
113
129
} ) ;
114
130
} ) ;
0 commit comments