-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathbox_test.js
402 lines (359 loc) · 12.4 KB
/
box_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
var Plotly = require('@lib');
var Lib = require('@src/lib');
var Box = require('@src/traces/box');
var d3 = require('d3');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var failTest = require('../assets/fail_test');
var mouseEvent = require('../assets/mouse_event');
var customAssertions = require('../assets/custom_assertions');
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
describe('Test boxes supplyDefaults', function() {
var traceIn;
var traceOut;
var defaultColor = '#444';
var supplyDefaults = Box.supplyDefaults;
beforeEach(function() {
traceOut = {};
});
it('should set visible to false when x and y are empty', function() {
traceIn = {};
supplyDefaults(traceIn, traceOut, defaultColor);
expect(traceOut.visible).toBe(false);
traceIn = {
x: [],
y: []
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.visible).toBe(false);
});
it('should set visible to false when x or y is empty', function() {
traceIn = {
x: []
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.visible).toBe(false);
traceIn = {
x: [],
y: [1, 2, 3]
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.visible).toBe(false);
traceIn = {
y: []
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.visible).toBe(false);
traceIn = {
x: [1, 2, 3],
y: []
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.visible).toBe(false);
});
it('should set orientation to v by default', function() {
traceIn = {
y: [1, 2, 3]
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.orientation).toBe('v');
traceIn = {
x: [1, 1, 1],
y: [1, 2, 3]
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.orientation).toBe('v');
});
it('should set orientation to h when only x is supplied', function() {
traceIn = {
x: [1, 2, 3]
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.orientation).toBe('h');
});
it('should inherit layout.calendar', function() {
traceIn = {
y: [1, 2, 3]
};
supplyDefaults(traceIn, traceOut, defaultColor, {calendar: 'islamic'});
// we always fill calendar attributes, because it's hard to tell if
// we're on a date axis at this point.
expect(traceOut.xcalendar).toBe('islamic');
expect(traceOut.ycalendar).toBe('islamic');
});
it('should take its own calendars', function() {
traceIn = {
y: [1, 2, 3],
xcalendar: 'coptic',
ycalendar: 'ethiopian'
};
supplyDefaults(traceIn, traceOut, defaultColor, {calendar: 'islamic'});
// we always fill calendar attributes, because it's hard to tell if
// we're on a date axis at this point.
expect(traceOut.xcalendar).toBe('coptic');
expect(traceOut.ycalendar).toBe('ethiopian');
});
it('should not coerce point attributes when boxpoints is false', function() {
traceIn = {
y: [1, 1, 2],
boxpoints: false
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.boxpoints).toBe(false);
expect(traceOut.jitter).toBeUndefined();
expect(traceOut.pointpos).toBeUndefined();
expect(traceOut.marker).toBeUndefined();
expect(traceOut.text).toBeUndefined();
});
it('should default boxpoints to suspectedoutliers when marker.outliercolor is set & valid', function() {
traceIn = {
y: [1, 1, 2],
marker: {
outliercolor: 'blue'
}
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.boxpoints).toBe('suspectedoutliers');
});
it('should default boxpoints to suspectedoutliers when marker.line.outliercolor is set & valid', function() {
traceIn = {
y: [1, 1, 2],
marker: {
line: {outliercolor: 'blue'}
}
};
supplyDefaults(traceIn, traceOut, defaultColor, {});
expect(traceOut.boxpoints).toBe('suspectedoutliers');
expect(traceOut.marker).toBeDefined();
expect(traceOut.text).toBeDefined();
});
});
describe('Test box hover:', function() {
var gd;
afterEach(destroyGraphDiv);
function run(specs) {
gd = createGraphDiv();
var fig = Lib.extendDeep(
{width: 700, height: 500},
specs.mock || require('@mocks/box_grouped.json')
);
if(specs.patch) {
fig = specs.patch(fig);
}
var pos = specs.pos || [200, 200];
return Plotly.plot(gd, fig).then(function() {
mouseEvent('mousemove', pos[0], pos[1]);
assertHoverLabelContent(specs, specs.desc);
});
}
[{
desc: 'base',
nums: ['median: 0.55', 'min: 0', 'q1: 0.3', 'q3: 0.6', 'max: 0.7'],
name: ['radishes', '', '', '', ''],
axis: 'day 1'
}, {
desc: 'with mean',
patch: function(fig) {
fig.data.forEach(function(trace) {
trace.boxmean = true;
});
return fig;
},
nums: ['median: 0.55', 'min: 0', 'q1: 0.3', 'q3: 0.6', 'max: 0.7', 'mean: 0.45'],
name: ['radishes', '', '', '', '', ''],
axis: 'day 1'
}, {
desc: 'with sd',
patch: function(fig) {
fig.data.forEach(function(trace) {
trace.boxmean = 'sd';
});
return fig;
},
nums: [
'median: 0.55', 'min: 0', 'q1: 0.3', 'q3: 0.6', 'max: 0.7',
'mean ± σ: 0.45 ± 0.2362908'
],
name: ['radishes', '', '', '', '', ''],
axis: 'day 1'
}, {
desc: 'with boxpoints fences',
mock: require('@mocks/boxplots_outliercolordflt.json'),
pos: [350, 200],
nums: [
'median: 8.15', 'min: 0.75', 'q1: 6.8',
'q3: 10.25', 'max: 23.25', 'lower fence: 5.25', 'upper fence: 12'
],
name: ['', '', '', '', '', '', ''],
axis: 'trace 0'
}, {
desc: 'with overlaid boxes',
patch: function(fig) {
fig.layout.boxmode = 'overlay';
return fig;
},
nums: [
'q1: 0.3', 'median: 0.45', 'q3: 0.6', 'max: 1', 'median: 0.55', 'min: 0', 'min: 0.2',
'q3: 0.6', 'max: 0.7', 'median: 0.45', 'min: 0.1', 'q3: 0.6', 'max: 0.9'
],
name: [
'', 'kale', '', '', 'radishes', '', '',
'', '', 'carrots', '', '', ''
],
axis: 'day 1'
}, {
desc: 'hoveron points | hovermode closest',
patch: function(fig) {
fig.data.forEach(function(trace) {
trace.boxpoints = 'all';
trace.hoveron = 'points';
});
fig.layout.hovermode = 'closest';
return fig;
},
nums: '(day 1, 0.7)',
name: 'radishes'
}, {
desc: 'hoveron points | hovermode x',
patch: function(fig) {
fig.data.forEach(function(trace) {
trace.boxpoints = 'all';
trace.hoveron = 'points';
});
fig.layout.hovermode = 'x';
return fig;
},
nums: '0.7',
name: 'radishes',
axis: 'day 1'
}, {
desc: 'hoveron boxes+points | hovermode x (hover on box only - same result as base)',
patch: function(fig) {
fig.data.forEach(function(trace) {
trace.boxpoints = 'all';
trace.hoveron = 'points+boxes';
});
fig.layout.hovermode = 'x';
return fig;
},
pos: [215, 200],
nums: ['median: 0.55', 'min: 0', 'q1: 0.3', 'q3: 0.6', 'max: 0.7'],
name: ['radishes', '', '', '', ''],
axis: 'day 1'
}, {
desc: 'hoveron boxes+points | hovermode x (box AND closest point)',
patch: function(fig) {
fig.data.forEach(function(trace) {
trace.boxpoints = 'all';
trace.hoveron = 'points+boxes';
trace.pointpos = 0;
});
fig.layout.hovermode = 'x';
return fig;
},
nums: ['0.6', 'median: 0.55', 'min: 0', 'q1: 0.3', 'q3: 0.6', 'max: 0.7'],
name: ['radishes', 'radishes', '', '', '', ''],
axis: 'day 1'
}, {
desc: 'text items on hover',
patch: function(fig) {
fig.data.forEach(function(trace) {
trace.boxpoints = 'all';
trace.hoveron = 'points';
trace.text = trace.y.map(function(v) { return 'look:' + v; });
});
fig.layout.hovermode = 'closest';
return fig;
},
nums: '(day 1, 0.7)\nlook:0.7',
name: 'radishes'
}, {
desc: 'only text items on hover',
patch: function(fig) {
fig.data.forEach(function(trace) {
trace.boxpoints = 'all';
trace.hoveron = 'points';
trace.text = trace.y.map(function(v) { return 'look:' + v; });
trace.hoverinfo = 'text';
});
fig.layout.hovermode = 'closest';
return fig;
},
nums: 'look:0.7',
name: ''
}].forEach(function(specs) {
it('should generate correct hover labels ' + specs.desc, function(done) {
run(specs).catch(failTest).then(done);
});
});
});
describe('Box edge cases', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('does not barf on a single outlier with jitter', function(done) {
var trace = {
boxpoints: 'outliers',
jitter: 0.7,
type: 'box',
y: [46.505, 0.143, 0.649, 0.059, 513, 90, 234]
};
Plotly.newPlot(gd, [trace])
.then(function() {
var outliers = [];
gd.calcdata[0][0].pts.forEach(function(pt) {
if(pt.x !== undefined) outliers.push(pt);
});
expect(outliers.length).toBe(1);
expect(outliers[0].x).toBe(0);
})
.catch(failTest)
.then(done);
});
});
describe('Test box restyle:', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should be able to add/remove innner parts', function(done) {
var fig = Lib.extendDeep({}, require('@mocks/box_plot_jitter.json'));
// start with just 1 box
delete fig.data[0].boxpoints;
function _assertOne(msg, exp, trace3, k, query) {
expect(trace3.selectAll(query).size())
.toBe(exp[k] || 0, k + ' - ' + msg);
}
function _assert(msg, exp) {
var trace3 = d3.select(gd).select('.boxlayer > .trace');
_assertOne(msg, exp, trace3, 'boxCnt', 'path.box');
_assertOne(msg, exp, trace3, 'meanlineCnt', 'path.mean');
_assertOne(msg, exp, trace3, 'ptsCnt', 'path.point');
}
Plotly.plot(gd, fig)
.then(function() {
_assert('base', {boxCnt: 1});
})
.then(function() { return Plotly.restyle(gd, 'boxmean', true); })
.then(function() {
_assert('with meanline', {boxCnt: 1, meanlineCnt: 1});
})
.then(function() { return Plotly.restyle(gd, 'boxmean', 'sd'); })
.then(function() {
_assert('with mean+sd line', {boxCnt: 1, meanlineCnt: 1});
})
.then(function() { return Plotly.restyle(gd, 'boxpoints', 'all'); })
.then(function() {
_assert('with mean+sd line + pts', {boxCnt: 1, meanlineCnt: 1, ptsCnt: 9});
})
.then(function() { return Plotly.restyle(gd, 'boxmean', false); })
.then(function() {
_assert('with pts', {boxCnt: 1, ptsCnt: 9});
})
.catch(failTest)
.then(done);
});
});