-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtable_test.js
526 lines (433 loc) · 21.7 KB
/
table_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
var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var Table = require('@src/traces/table');
var attributes = require('@src/traces/table/attributes');
var cn = require('@src/traces/table/constants').cn;
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 supplyAllDefaults = require('../assets/supply_defaults');
var mouseEvent = require('../assets/mouse_event');
var mockMulti = require('@mocks/table_latex_multitrace_scatter.json');
// mock with two columns; lowest column count of general case
var mock2 = Lib.extendDeep({}, mockMulti);
mock2.data = [mock2.data[2]]; // keep the subplot with two columns
// mock with one column as special case
var mock1 = Lib.extendDeep({}, mock2);
mock1.data[0].header.values = mock1.data[0].header.values.slice(0, 1);
mock1.data[0].cells.values = mock1.data[0].cells.values.slice(0, 1);
// mock with zero columns; special case, as no column can be rendered
var mock0 = Lib.extendDeep({}, mock1);
mock0.data[0].header.values = [];
mock0.data[0].cells.values = [];
var mock = require('@mocks/table_plain_birds.json');
describe('table initialization tests', function() {
'use strict';
describe('table global defaults', function() {
it('should not coerce trace opacity', function() {
var gd = Lib.extendDeep({}, mock1);
supplyAllDefaults(gd);
expect(gd._fullData[0].opacity).toBeUndefined();
});
it('should use global font as label, tick and range font defaults', function() {
var gd = Lib.extendDeep({}, mock1);
delete gd.data[0].header.font;
delete gd.data[0].cells.font;
gd.layout.font = {
family: 'Gravitas',
size: 20,
color: 'blue'
};
supplyAllDefaults(gd);
var expected = {
family: 'Gravitas',
size: 20,
color: 'blue'
};
expect(gd._fullData[0].header.font).toEqual(expected);
expect(gd._fullData[0].cells.font).toEqual(expected);
});
});
describe('table defaults', function() {
function _supply(traceIn) {
var traceOut = { visible: true },
defaultColor = '#777',
layout = { font: {family: '"Open Sans", verdana, arial, sans-serif', size: 12, color: '#444'} };
Table.supplyDefaults(traceIn, traceOut, defaultColor, layout);
return traceOut;
}
it('\'line\' specification should yield a default color', function() {
var fullTrace = _supply({});
expect(fullTrace.header.fill.color).toEqual(attributes.header.fill.color.dflt);
expect(fullTrace.cells.fill.color).toEqual(attributes.cells.fill.color.dflt);
});
it('\'domain\' specification should have a default', function() {
var fullTrace = _supply({});
expect(fullTrace.domain).toEqual({x: [0, 1], y: [0, 1]});
});
it('\'*.values\' specification should have a default of an empty array', function() {
var fullTrace = _supply({});
expect(fullTrace.header.values).toEqual([]);
expect(fullTrace.cells.values).toEqual([]);
});
it('\'columnwidth\' specification should accept a numerical array', function() {
var fullTrace = _supply({
columnwidth: [1, 2, 3]
});
expect(fullTrace.columnwidth).toEqual([1, 2, 3]);
});
it('\'columnwidth\' specification should accept a string array (converted downstream)', function() {
var fullTrace = _supply({
columnwidth: ['1', '2', '3']
});
expect(fullTrace.columnwidth).toEqual(['1', '2', '3']);
});
it('\'header\' should be used with default values where attributes are not provided', function() {
var fullTrace = _supply({
header: {
values: ['A'],
alienProperty: 'Alpha Centauri'
},
cells: {
values: [1, 2], // otherwise header.values will become []
alienProperty: 'Betelgeuse'
}
});
expect(fullTrace.header).toEqual({
values: ['A'], // only one column remains
format: [],
align: 'center',
height: 28,
line: { width: 1, color: 'grey' },
fill: { color: attributes.header.fill.color.dflt },
font: {family: '"Open Sans", verdana, arial, sans-serif', size: 12, color: '#444'}
});
expect(fullTrace.cells).toEqual({
values: [1, 2],
format: [],
align: 'center',
height: 20,
line: { width: 1, color: 'grey' },
fill: { color: attributes.cells.fill.color.dflt },
font: {family: '"Open Sans", verdana, arial, sans-serif', size: 12, color: '#444'}
});
});
});
});
describe('table', function() {
afterEach(destroyGraphDiv);
describe('edge cases', function() {
it('Works with more than one column', function(done) {
var mockCopy = Lib.extendDeep({}, mock2);
var gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].header.values.length).toEqual(2);
expect(gd.data[0].cells.values.length).toEqual(2);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(2);
done();
});
});
it('Works with one column', function(done) {
var mockCopy = Lib.extendDeep({}, mock1);
var gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].header.values.length).toEqual(1);
expect(gd.data[0].cells.values.length).toEqual(1);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(1);
done();
});
});
it('Does not error with zero columns', function(done) {
var mockCopy = Lib.extendDeep({}, mock0);
var gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].header.values.length).toEqual(0);
expect(gd.data[0].cells.values.length).toEqual(0);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(0);
done();
});
});
it('Does not raise an error with zero lines', function(done) {
var mockCopy = Lib.extendDeep({}, mock2);
mockCopy.layout.width = 320;
mockCopy.data[0].header.values = [[], []];
mockCopy.data[0].cells.values = [[], []];
var gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].header.values.length).toEqual(2);
expect(gd.data[0].cells.values.length).toEqual(2);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(2);
done();
});
});
it('should remove scroll glyph and capture zone when *staticPlot:true*', function(done) {
var mockCopy = Lib.extendDeep({}, require('@mocks/table_plain_birds.json'));
var gd = createGraphDiv();
function _assert(msg, exp) {
expect(d3.selectAll('.' + cn.scrollbarCaptureZone).size()).toBe(exp.captureZone, msg);
expect(d3.selectAll('.' + cn.scrollbarGlyph).size()).toBe(exp.glyph, msg);
}
// more info in: https://github.com/plotly/streambed/issues/11618
Plotly.plot(gd, mockCopy).then(function() {
_assert('staticPlot:false (base)', {
captureZone: 1,
glyph: 1
});
})
.then(function() { return Plotly.purge(gd); })
.then(function() { return Plotly.plot(gd, mockCopy.data, mockCopy.layout, {staticPlot: true}); })
.then(function() {
_assert('staticPlot:true', {
captureZone: 0,
glyph: 0
});
})
.catch(failTest)
.then(done);
});
});
describe('Rendering with partial attribute support', function() {
var mockCopy,
gd;
afterEach(destroyGraphDiv);
it('`Plotly.plot` should render all the columns even if no cell contents were supplied yet', function(done) {
gd = createGraphDiv();
mockCopy = Lib.extendDeep({}, mock);
delete mockCopy.data[0].cells;
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].header.values.length).toEqual(7);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(7);
expect(document.querySelectorAll('.' + cn.columnCell).length).toEqual(7 * 2); // both column rows to render
done();
});
});
it('`Plotly.plot` should render all columns even if no header contents were supplied yet', function(done) {
gd = createGraphDiv();
mockCopy = Lib.extendDeep({}, mock);
delete mockCopy.data[0].header;
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].cells.values.length).toEqual(7);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(7);
expect(document.querySelectorAll('.' + cn.columnCell).length).toEqual(7 * 29);
expect(document.querySelectorAll('#header').length).toEqual(7);
expect(document.querySelectorAll('#header .' + cn.columnCell).length).toEqual(7);
expect(document.querySelector('#header .' + cn.columnCell + ' text').textContent).toEqual('');
done();
});
});
it('`Plotly.plot` should render all the column headers even if not all header values were supplied', function(done) {
gd = createGraphDiv();
mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].header.values = ['A', 'S', 'D']; // doesn't cover all 7 columns
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].cells.values.length).toEqual(7);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(7);
expect(document.querySelectorAll('#header').length).toEqual(7);
expect(document.querySelectorAll('#header .' + cn.columnCell).length).toEqual(7);
expect(document.querySelector('#header .' + cn.columnCell + ' text').textContent).toEqual('A');
done();
});
});
});
describe('basic use and basic data restyling', function() {
var mockCopy,
gd;
beforeEach(function(done) {
mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].domain = {
x: [0.1, 0.9],
y: [0.05, 0.85]
};
gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
});
it('`Plotly.plot` should have proper fields on `gd.data` on initial rendering', function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].header.values.length).toEqual(7);
expect(gd.data[0].cells.values.length).toEqual(7);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(7);
});
it('Calling `Plotly.plot` again should add the new table trace', function(done) {
var reversedMockCopy = Lib.extendDeep({}, mockCopy);
reversedMockCopy.data[0].header.values = reversedMockCopy.data[0].header.values.slice().reverse();
reversedMockCopy.data[0].cells.values = reversedMockCopy.data[0].cells.values.slice().reverse();
reversedMockCopy.data[0].domain.y = [0, 0.3];
Plotly.plot(gd, reversedMockCopy.data, reversedMockCopy.layout).then(function() {
expect(gd.data.length).toEqual(2);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(7 * 2);
done();
});
});
it('Calling `Plotly.restyle` with a string path should amend the preexisting table', function(done) {
expect(gd.data.length).toEqual(1);
Plotly.restyle(gd, 'header.fill.color', 'magenta').then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.data[0].header.fill.color).toEqual('magenta');
expect(gd.data[0].header.values.length).toEqual(7);
expect(gd.data[0].cells.values.length).toEqual(7);
expect(gd.data[0].header.line.color).toEqual(['dimgray', 'grey']); // no change relative to original mock value
expect(gd.data[0].cells.line.color).toEqual(['grey']); // no change relative to original mock value
done();
});
});
it('Calling `Plotly.restyle` for a `header.values` change should amend the preexisting one', function(done) {
function restyleValues(what, key, setterValue) {
// array values need to be wrapped in an array; unwrapping here for value comparison
var value = Array.isArray(setterValue) ? setterValue[0] : setterValue;
return function() {
return Plotly.restyle(gd, what + '.values[' + key + ']', setterValue).then(function() {
expect(gd.data[0][what].values[key]).toEqual(value, 'for column \'' + key + '\'');
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(7);
});
};
}
restyleValues('cells', 0, [['new cell content 1', 'new cell content 2']])()
.then(restyleValues('cells', 2, [[0, 0.1]]))
.then(restyleValues('header', 1, [['Species top', 'Species bottom']]))
.then(done);
});
it('Calling `Plotly.relayout` with string should amend the preexisting table', function(done) {
expect(gd.layout.width).toEqual(1000);
Plotly.relayout(gd, 'width', 500).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.layout.width).toEqual(500);
done();
});
});
it('Calling `Plotly.relayout` with object should amend the preexisting table', function(done) {
expect(gd.layout.width).toEqual(1000);
Plotly.relayout(gd, {width: 500}).then(function() {
expect(gd.data.length).toEqual(1);
expect(gd.layout.width).toEqual(500);
done();
});
});
});
describe('more restyling tests with scenegraph queries', function() {
var mockCopy, gd;
beforeEach(function(done) {
mockCopy = Lib.extendDeep({}, mock2);
gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
});
it('Calling `Plotly.restyle` for a `header.values` change should amend the preexisting one', function(done) {
function restyleValues(what, fun, setterValue) {
var value = Array.isArray(setterValue) ? setterValue[0] : setterValue;
return function() {
return Plotly.restyle(gd, what, setterValue).then(function() {
expect(fun(gd)).toEqual(value, what + ':::' + value);
expect(document.querySelectorAll('.' + cn.yColumn).length).toEqual(2);
expect(document.querySelectorAll('.' + cn.columnBlock).length).toEqual(6);
expect(document.querySelectorAll('.' + cn.columnCell).length).toEqual(6);
expect(document.querySelectorAll('.' + cn.cellRect).length).toEqual(6);
expect(document.querySelectorAll('.' + cn.cellTextHolder).length).toEqual(6);
});
};
}
restyleValues('cells.fill.color', function(gd) {return gd.data[0].cells.fill.color;}, [['green', 'red']])()
.then(restyleValues('cells.line.color', function(gd) {return gd.data[0].cells.line.color;}, [['magenta', 'blue']]))
.then(restyleValues('cells.line.width', function(gd) {return gd.data[0].cells.line.width;}, [[5, 3]]))
.then(restyleValues('cells.format', function(gd) {return gd.data[0].cells.format;}, [['', '']]))
.then(restyleValues('cells.prefix', function(gd) {return gd.data[0].cells.prefix;}, [['p1']]))
.then(restyleValues('cells.suffix', function(gd) {return gd.data[0].cells.suffix;}, [['s1']]))
.then(restyleValues('header.fill.color', function(gd) {return gd.data[0].header.fill.color;}, [['yellow', 'purple']]))
.then(restyleValues('header.line.color', function(gd) {return gd.data[0].header.line.color;}, [['green', 'red']]))
.then(restyleValues('header.line.width', function(gd) {return gd.data[0].header.line.width;}, [[2, 6]]))
.then(restyleValues('header.format', function(gd) {return gd.data[0].header.format;}, [['', '']]))
.then(done);
});
});
describe('scroll effects', function() {
var mockCopy, gd, gdWheelEventCount;
beforeEach(function(done) {
mockCopy = Lib.extendDeep({}, mockMulti);
gd = createGraphDiv();
gdWheelEventCount = 0;
Plotly.plot(gd, mockCopy.data, mockCopy.layout)
.then(function() {
gd.addEventListener('mousewheel', function(evt) {
gdWheelEventCount++;
// make sure we don't *really* scroll the page.
// that would be annoying!
evt.stopPropagation();
evt.preventDefault();
});
})
.then(done);
});
function assertBubbledEvents(cnt) {
expect(gdWheelEventCount).toBe(cnt);
gdWheelEventCount = 0;
}
function getCenter(el) {
var bBox = el.getBoundingClientRect();
// actually above center, since these bboxes are bigger than the
// visible table area
return {x: bBox.left + bBox.width / 2, y: bBox.top + bBox.height / 4};
}
function scroll(pos, dy) {
mouseEvent('mousemove', pos.x, pos.y);
mouseEvent('mousewheel', pos.x, pos.y, {deltaX: 0, deltaY: dy});
}
it('bubbles scroll events iff they did not scroll a table', function() {
var allTableControls = document.querySelectorAll('.' + cn.tableControlView);
var smallCenter = getCenter(allTableControls[0]);
var bigCenter = getCenter(allTableControls[1]);
// table with no scroll bars - events always bubble
scroll(smallCenter, -20);
assertBubbledEvents(1);
scroll(smallCenter, 20);
assertBubbledEvents(1);
// table with scrollbars - events bubble if we don't use them
scroll(bigCenter, -20); // up from the top - bubbles
assertBubbledEvents(1);
scroll(bigCenter, 20); // down from the top - scrolled, so no bubble
assertBubbledEvents(0);
scroll(bigCenter, -40); // back to the top, with extra dy discarded
assertBubbledEvents(0);
scroll(bigCenter, -20); // now it bubbles
assertBubbledEvents(1);
scroll(bigCenter, 200000); // all the way to the bottom
assertBubbledEvents(0);
scroll(bigCenter, 20); // now it bubbles from the bottom..
assertBubbledEvents(1);
});
it('does not scroll any tables with staticPlot', function(done) {
var allTableControls = document.querySelectorAll('.' + cn.tableControlView);
var bigCenter = getCenter(allTableControls[1]);
var mock = Lib.extendDeep({}, mockMulti);
// make sure initially with staticPlot: false, scrolling works
// (copied from previous test)
scroll(bigCenter, 20);
assertBubbledEvents(0);
scroll(bigCenter, -40);
assertBubbledEvents(0);
Plotly.react(gd, mock.data, mock.layout, {staticPlot: true})
.then(function() {
// now the same scrolls bubble
scroll(bigCenter, 20);
assertBubbledEvents(1);
scroll(bigCenter, -40);
assertBubbledEvents(1);
return Plotly.react(gd, mock.data, mock.layout, {staticPlot: false});
})
.then(function() {
// scroll works again!
scroll(bigCenter, 20);
assertBubbledEvents(0);
scroll(bigCenter, -40);
assertBubbledEvents(0);
})
.catch(failTest)
.then(done);
});
});
});