-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsankey_test.js
341 lines (266 loc) · 11.3 KB
/
sankey_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
var Plotly = require('@lib/index');
var attributes = require('@src/traces/sankey/attributes');
var Lib = require('@src/lib');
var d3 = require('d3');
var mock = require('@mocks/sankey_energy.json');
var mockDark = require('@mocks/sankey_energy_dark.json');
var Plots = require('@src/plots/plots');
var Sankey = require('@src/traces/sankey');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var mouseEvent = require('../assets/mouse_event');
describe('sankey tests', function() {
'use strict';
function _supply(traceIn) {
var traceOut = { visible: true },
defaultColor = '#444',
layout = { };
Sankey.supplyDefaults(traceIn, traceOut, defaultColor, layout);
return traceOut;
}
function _supplyWithLayout(traceIn, layout) {
var traceOut = { visible: true },
defaultColor = '#444';
Sankey.supplyDefaults(traceIn, traceOut, defaultColor, layout);
return traceOut;
}
describe('don\'t remove nodes if encountering no circularity', function() {
it('removing a single self-pointing node', function() {
var fullTrace = _supply({
node: {
label: ['a', 'b']
},
link: {
value: [1],
source: [1],
target: [0]
}
});
expect(fullTrace.node.label).toEqual(['a', 'b'], 'node labels retained');
expect(fullTrace.link.value).toEqual([1], 'link value(s) retained');
expect(fullTrace.link.source).toEqual([1], 'link source(s) retained');
expect(fullTrace.link.target).toEqual([0], 'link target(s) retained');
});
});
describe('log warning if issue is encountered with graph structure',
function() {
it('some nodes are not linked', function() {
var warnings = [];
spyOn(Lib, 'warn').and.callFake(function(msg) {
warnings.push(msg);
});
_supply({
node: {
label: ['a', 'b', 'c']
},
link: {
value: [1],
source: [0],
target: [1]
}
});
expect(warnings.length).toEqual(1);
});
});
describe('sankey global defaults', function() {
it('should not coerce trace opacity', function() {
var gd = Lib.extendDeep({}, mock);
Plots.supplyDefaults(gd);
expect(gd._fullData[0].opacity).toBeUndefined();
});
});
describe('sankey defaults', function() {
it('\'Sankey\' specification should have proper arrays where mandatory',
function() {
var fullTrace = _supply({});
expect(fullTrace.node.label)
.toEqual([], 'presence of node label array is guaranteed');
expect(fullTrace.link.value)
.toEqual([], 'presence of link value array is guaranteed');
expect(fullTrace.link.source)
.toEqual([], 'presence of link source array is guaranteed');
expect(fullTrace.link.target)
.toEqual([], 'presence of link target array is guaranteed');
});
it('\'Sankey\' specification should have proper types',
function() {
var fullTrace = _supply({});
expect(fullTrace.orientation)
.toEqual(attributes.orientation.dflt, 'use orientation by default');
expect(fullTrace.valueformat)
.toEqual(attributes.valueformat.dflt, 'valueformat by default');
expect(fullTrace.valuesuffix)
.toEqual(attributes.valuesuffix.dflt, 'valuesuffix by default');
expect(fullTrace.arrangement)
.toEqual(attributes.arrangement.dflt, 'arrangement by default');
expect(fullTrace.domain.x)
.toEqual(attributes.domain.x.dflt, 'x domain by default');
expect(fullTrace.domain.y)
.toEqual(attributes.domain.y.dflt, 'y domain by default');
});
it('\'Sankey\' layout dependent specification should have proper types',
function() {
var fullTrace = _supplyWithLayout({}, {font: {family: 'Arial'}});
expect(fullTrace.textfont)
.toEqual({family: 'Arial'}, 'textfont is defined');
});
it('\'line\' specifications should yield the default values',
function() {
var fullTrace = _supply({});
expect(fullTrace.node.line.color)
.toEqual('#444', 'default node line color');
expect(fullTrace.node.line.width)
.toEqual(0.5, 'default node line thickness');
expect(fullTrace.link.line.color)
.toEqual('#444', 'default link line color');
expect(fullTrace.link.line.width)
.toEqual(0, 'default link line thickness');
});
it('fills \'node\' colors if not specified', function() {
var fullTrace = _supply({
node: {
label: ['a', 'b']
},
link: {
source: [0],
target: [1],
value: [1]
}
});
expect(Lib.isArray(fullTrace.node.color)).toBe(true, 'set up color array');
});
});
describe('sankey calc', function() {
function _calc(trace) {
var gd = { data: [trace] };
Plots.supplyDefaults(gd);
var fullTrace = gd._fullData[0];
Sankey.calc(gd, fullTrace);
return fullTrace;
}
var base = { type: 'sankey' };
it('circularity is detected', function() {
var errors = [];
spyOn(Lib, 'error').and.callFake(function(msg) {
errors.push(msg);
});
_calc(Lib.extendDeep({}, base, {
node: {
label: ['a', 'b', 'c']
},
link: {
value: [1, 1, 1],
source: [0, 1, 2],
target: [1, 2, 0]
}
}));
expect(errors.length).toEqual(1);
});
describe('remove nodes if encountering circularity', function() {
it('removing a single self-pointing node', function() {
var fullTrace = _calc(Lib.extendDeep({}, base, {
node: {
label: ['a']
},
link: {
value: [1],
source: [0],
target: [0]
}
}));
expect(fullTrace.node.label).toEqual([], 'node label(s) removed');
expect(fullTrace.link.value).toEqual([], 'link value(s) removed');
expect(fullTrace.link.source).toEqual([], 'link source(s) removed');
expect(fullTrace.link.target).toEqual([], 'link target(s) removed');
});
it('removing everything if detecting a circle', function() {
var fullTrace = _calc(Lib.extendDeep({}, base, {
node: {
label: ['a', 'b', 'c', 'd', 'e']
},
link: {
value: [1, 1, 1, 1, 1, 1, 1, 1],
source: [0, 1, 2, 3],
target: [1, 2, 0, 4]
}
}));
expect(fullTrace.node.label).toEqual([], 'node label(s) removed');
expect(fullTrace.link.value).toEqual([], 'link value(s) removed');
expect(fullTrace.link.source).toEqual([], 'link source(s) removed');
expect(fullTrace.link.target).toEqual([], 'link target(s) removed');
});
});
});
describe('lifecycle methods', function() {
afterEach(destroyGraphDiv);
function wait() {
return new Promise(function(resolve) {
setTimeout(resolve, 60);
});
}
it('Plotly.deleteTraces with two traces removes the deleted plot', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
var mockCopy2 = Lib.extendDeep({}, mockDark);
Plotly.plot(gd, mockCopy)
.then(function() {
expect(gd.data.length).toEqual(1);
expect(d3.selectAll('.sankey').size()).toEqual(1);
return Plotly.addTraces(gd, mockCopy2.data[0]);
})
.then(function() {
expect(gd.data.length).toEqual(2);
expect(d3.selectAll('.sankey').size()).toEqual(2);
return Plotly.deleteTraces(gd, [0]);
})
.then(function() {
expect(gd.data.length).toEqual(1);
expect(d3.selectAll('.sankey').size()).toEqual(1);
return Plotly.deleteTraces(gd, 0);
})
.then(function() {
expect(gd.data.length).toEqual(0);
expect(d3.selectAll('.sankey').size()).toEqual(0);
done();
});
});
it('Plotly.plot does not show Sankey if \'visible\' is false', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
Plotly.plot(gd, mockCopy)
.then(function() {
expect(gd.data.length).toEqual(1);
expect(d3.selectAll('.sankey').size()).toEqual(1);
return Plotly.restyle(gd, 'visible', false);
})
.then(function() {
expect(gd.data.length).toEqual(1);
expect(d3.selectAll('.sankey').size()).toEqual(0);
return Plotly.restyle(gd, 'visible', true);
})
.then(function() {
expect(gd.data.length).toEqual(1);
expect(d3.selectAll('.sankey').size()).toEqual(1);
done();
});
});
it('Plotly.plot shows and removes tooltip on node, link', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
Plotly.plot(gd, mockCopy).then(function() {
mouseEvent('mousemove', 400, 300);
mouseEvent('mouseover', 400, 300);
})
.then(wait)
.then(function() {
expect(d3.select('.hoverlayer>.hovertext>text').node().innerHTML)
.toEqual('447TWh', 'tooltip present');
})
.then(function() {
mouseEvent('mousemove', 450, 300);
mouseEvent('mouseover', 450, 300);
})
.then(done);
});
});
});