-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathconfig_test.js
637 lines (519 loc) · 21.7 KB
/
config_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
var Plotly = require('@lib/index');
var Plots = Plotly.Plots;
var Lib = require('@src/lib');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var click = require('../assets/click');
var mouseEvent = require('../assets/mouse_event');
var failTest = require('../assets/fail_test');
var delay = require('../assets/delay');
describe('config argument', function() {
describe('attribute layout.autosize', function() {
var layoutWidth = 1111;
var relayoutWidth = 555;
var containerWidthBeforePlot = 888;
var containerWidthBeforeRelayout = 666;
var containerHeightBeforePlot = 543;
var containerHeightBeforeRelayout = 321;
var data = [];
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
function checkLayoutSize(width, height) {
expect(gd._fullLayout.width).toBe(width);
expect(gd._fullLayout.height).toBe(height);
var svg = document.getElementsByClassName('main-svg')[0];
expect(+svg.getAttribute('width')).toBe(width);
expect(+svg.getAttribute('height')).toBe(height);
}
function compareLayoutAndFullLayout(gd) {
expect(gd.layout.width).toBe(gd._fullLayout.width);
expect(gd.layout.height).toBe(gd._fullLayout.height);
}
function testAutosize(autosize, config, layoutHeight, relayoutHeight, done) {
var layout = {
autosize: autosize,
width: layoutWidth
};
var relayout = {
width: relayoutWidth
};
var layout2 = Lib.extendDeep({}, layout);
gd.style.width = containerWidthBeforePlot + 'px';
gd.style.height = containerHeightBeforePlot + 'px';
function beforeResize() {
checkLayoutSize(layoutWidth, layoutHeight);
if(!autosize) compareLayoutAndFullLayout(gd);
gd.style.width = containerWidthBeforeRelayout + 'px';
gd.style.height = containerHeightBeforeRelayout + 'px';
}
function afterResize() {
checkLayoutSize(relayoutWidth, relayoutHeight);
if(!autosize) compareLayoutAndFullLayout(gd);
}
Plotly.plot(gd, data, layout, config).then(function() {
beforeResize();
return Plotly.relayout(gd, relayout);
})
.then(afterResize)
// now redo with Plotly.react
.then(function() {
gd.style.width = containerWidthBeforePlot + 'px';
gd.style.height = containerHeightBeforePlot + 'px';
return Plotly.newPlot(gd, data, layout2, config);
})
.then(function() {
beforeResize();
layout2.width = relayoutWidth;
return Plotly.react(gd, data, layout2, config);
})
.then(afterResize)
.catch(failTest)
.then(done);
}
it('should fill the frame when autosize: false, fillFrame: true, frameMargins: undefined', function(done) {
var autosize = false;
var config = {
autosizable: true,
fillFrame: true
};
var layoutHeight = window.innerHeight;
var relayoutHeight = layoutHeight;
testAutosize(autosize, config, layoutHeight, relayoutHeight, done);
});
it('should fill the frame when autosize: true, fillFrame: true and frameMargins: undefined', function(done) {
var autosize = true;
var config = {
fillFrame: true
};
var layoutHeight = window.innerHeight;
var relayoutHeight = window.innerHeight;
testAutosize(autosize, config, layoutHeight, relayoutHeight, done);
});
it('should fill the container when autosize: false, fillFrame: false and frameMargins: undefined', function(done) {
var autosize = false;
var config = {
autosizable: true,
fillFrame: false
};
var layoutHeight = containerHeightBeforePlot;
var relayoutHeight = layoutHeight;
testAutosize(autosize, config, layoutHeight, relayoutHeight, done);
});
it('should fill the container when autosize: true, fillFrame: false and frameMargins: undefined', function(done) {
var autosize = true;
var config = {
fillFrame: false
};
var layoutHeight = containerHeightBeforePlot;
var relayoutHeight = containerHeightBeforeRelayout;
testAutosize(autosize, config, layoutHeight, relayoutHeight, done);
});
it('should fill the container when autosize: false, fillFrame: false and frameMargins: 0.1', function(done) {
var autosize = false;
var config = {
autosizable: true,
fillFrame: false,
frameMargins: 0.1
};
var layoutHeight = 360;
var relayoutHeight = layoutHeight;
testAutosize(autosize, config, layoutHeight, relayoutHeight, done);
});
it('should fill the container when autosize: true, fillFrame: false and frameMargins: 0.1', function(done) {
var autosize = true;
var config = {
fillFrame: false,
frameMargins: 0.1
};
var layoutHeight = 360;
var relayoutHeight = 288;
testAutosize(autosize, config, layoutHeight, relayoutHeight, done);
});
it('should respect attribute autosizable: false', function(done) {
var autosize = false;
var config = {
autosizable: false,
fillFrame: true
};
var layoutHeight = Plots.layoutAttributes.height.dflt;
var relayoutHeight = layoutHeight;
testAutosize(autosize, config, layoutHeight, relayoutHeight, done);
});
});
describe('showLink attribute', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should not display the edit link by default', function() {
Plotly.plot(gd, [], {});
var link = document.getElementsByClassName('js-plot-link-container')[0];
expect(link).toBeUndefined();
});
it('should display a link when true', function() {
Plotly.plot(gd, [], {}, { showLink: true });
var link = document.getElementsByClassName('js-plot-link-container')[0];
expect(link.textContent).toBe('Edit chart »');
var bBox = link.getBoundingClientRect();
expect(bBox.width).toBeGreaterThan(0);
expect(bBox.height).toBeGreaterThan(0);
});
});
describe('editable attribute', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
function initPlot(editFlag) {
var edits = {};
edits[editFlag] = true;
return Plotly.plot(gd, [
{ x: [1, 2, 3], y: [1, 2, 3] },
{ x: [1, 2, 3], y: [3, 2, 1] }
], {
width: 600,
height: 400,
annotations: [
{ text: 'testing', x: 1, y: 1, showarrow: true }
]
}, { editable: false, edits: edits });
}
function checkIfEditable(elClass, text) {
return function() {
var label = document.getElementsByClassName(elClass)[0];
expect(label.textContent).toBe(text);
var labelBox = label.getBoundingClientRect();
var labelX = labelBox.left + labelBox.width / 2;
var labelY = labelBox.top + labelBox.height / 2;
mouseEvent('click', labelX, labelY);
var editBox = document.getElementsByClassName('plugin-editable editable')[0];
expect(editBox).toBeDefined();
expect(editBox.textContent).toBe(text);
expect(editBox.getAttribute('contenteditable')).toBe('true');
};
}
function checkIfDraggable(elClass) {
return function() {
var el = document.getElementsByClassName(elClass)[0];
var elBox = el.getBoundingClientRect();
var elX = elBox.left + elBox.width / 2;
var elY = elBox.top + elBox.height / 2;
mouseEvent('mousedown', elX, elY);
mouseEvent('mousemove', elX - 20, elY + 20);
var movedBox = el.getBoundingClientRect();
expect(movedBox.left).toBe(elBox.left - 20);
expect(movedBox.top).toBe(elBox.top + 20);
mouseEvent('mouseup', elX - 20, elY + 20);
};
}
it('should let edits override editable', function(done) {
var data = [{y: [1, 2, 3]}];
var layout = {width: 600, height: 400};
Plotly.newPlot(gd, data, layout, {editable: true})
.then(function() {
expect(gd._context.edits).toEqual({
annotationPosition: true,
annotationTail: true,
annotationText: true,
axisTitleText: true,
colorbarPosition: true,
colorbarTitleText: true,
legendPosition: true,
legendText: true,
shapePosition: true,
titleText: true
});
})
.then(function() {
return Plotly.newPlot(gd, data, layout, {
edits: {annotationPosition: false, annotationTail: false},
editable: true
});
})
.then(function() {
expect(gd._context.edits).toEqual({
annotationPosition: false,
annotationTail: false,
annotationText: true,
axisTitleText: true,
colorbarPosition: true,
colorbarTitleText: true,
legendPosition: true,
legendText: true,
shapePosition: true,
titleText: true
});
})
.then(function() {
return Plotly.newPlot(gd, data, layout, {
edits: {annotationText: true, axisTitleText: true},
editable: false
});
})
.then(function() {
expect(gd._context.edits).toEqual({
annotationPosition: false,
annotationTail: false,
annotationText: true,
axisTitleText: true,
colorbarPosition: false,
colorbarTitleText: false,
legendPosition: false,
legendText: false,
shapePosition: false,
titleText: false
});
})
.catch(failTest)
.then(done);
});
it('should make titles editable', function(done) {
initPlot('titleText')
.then(checkIfEditable('gtitle', 'Click to enter Plot title'))
.catch(failTest)
.then(done);
});
it('should make x axes labels editable', function(done) {
initPlot('axisTitleText')
.then(checkIfEditable('g-xtitle', 'Click to enter X axis title'))
.catch(failTest)
.then(done);
});
it('should make y axes labels editable', function(done) {
initPlot('axisTitleText')
.then(checkIfEditable('g-ytitle', 'Click to enter Y axis title'))
.catch(failTest)
.then(done);
});
it('should make legend labels editable', function(done) {
initPlot('legendText')
.then(checkIfEditable('legendtext', 'trace 0'))
.catch(failTest)
.then(done);
});
it('should make annotation labels editable', function(done) {
initPlot('annotationText')
.then(checkIfEditable('annotation-text-g', 'testing'))
.catch(failTest)
.then(done);
});
it('should make annotation labels draggable', function(done) {
initPlot('annotationTail')
.then(checkIfDraggable('annotation-text-g'))
.catch(failTest)
.then(done);
});
it('should make annotation arrows draggable', function(done) {
initPlot('annotationPosition')
.then(checkIfDraggable('annotation-arrow-g'))
.catch(failTest)
.then(done);
});
it('should make legends draggable', function(done) {
initPlot('legendPosition')
.then(checkIfDraggable('legend'))
.catch(failTest)
.then(done);
});
});
describe('axis drag handles attribute', function() {
var mock = require('@mocks/14.json');
var gd;
var mockCopy;
beforeEach(function(done) {
gd = createGraphDiv();
mockCopy = Lib.extendDeep({}, mock);
done();
});
afterEach(destroyGraphDiv);
function testDraggers(len) {
[
'nw', 'ne', 'sw', 'se', 'ew', 'w', 'e', 'ns', 'n', 's'
].forEach(function(dir) {
var draggers = document.getElementsByClassName('drag ' + dir + 'drag');
expect(draggers.length).toBe(len, dir);
});
}
it('should have drag rectangles cursors by default', function() {
Plotly.plot(gd, mockCopy.data, {});
testDraggers(1);
});
it('should not have drag rectangles when disabled', function() {
Plotly.plot(gd, mockCopy.data, {}, { showAxisDragHandles: false });
testDraggers(0);
});
});
describe('axis range entry attribute', function() {
var mock = require('@mocks/14.json');
var gd, mockCopy;
beforeEach(function(done) {
gd = createGraphDiv();
mockCopy = Lib.extendDeep({}, mock);
done();
});
afterEach(destroyGraphDiv);
it('allows axis range entry by default', function() {
Plotly.plot(gd, mockCopy.data, {});
var corner = document.getElementsByClassName('edrag')[0];
var cornerBox = corner.getBoundingClientRect();
var cornerX = cornerBox.left + cornerBox.width / 2;
var cornerY = cornerBox.top + cornerBox.height / 2;
click(cornerX, cornerY);
var editBox = document.getElementsByClassName('plugin-editable editable')[0];
expect(editBox).toBeDefined();
expect(editBox.getAttribute('contenteditable')).toBe('true');
});
it('disallows axis range entry when disabled', function() {
Plotly.plot(gd, mockCopy.data, {}, { showAxisRangeEntryBoxes: false });
var corner = document.getElementsByClassName('edrag')[0];
var cornerBox = corner.getBoundingClientRect();
var cornerX = cornerBox.left + cornerBox.width / 2;
var cornerY = cornerBox.top + cornerBox.height / 2;
click(cornerX, cornerY);
var editBox = document.getElementsByClassName('plugin-editable editable')[0];
expect(editBox).toBeUndefined();
});
});
describe('plotlyServerURL:', function() {
var gd;
var form;
beforeEach(function() {
gd = createGraphDiv();
spyOn(HTMLFormElement.prototype, 'submit').and.callFake(function() {
form = this;
});
});
afterEach(destroyGraphDiv);
it('should default to plotly cloud', function(done) {
Plotly.plot(gd, [], {})
.then(function() {
expect(gd._context.plotlyServerURL).toBe('https://plot.ly');
Plotly.Plots.sendDataToCloud(gd);
expect(form.action).toBe('https://plot.ly/external');
expect(form.method).toBe('post');
})
.catch(failTest)
.then(done);
});
it('can be set to other base urls', function(done) {
Plotly.plot(gd, [], {}, {plotlyServerURL: 'dummy'})
.then(function() {
expect(gd._context.plotlyServerURL).toBe('dummy');
Plotly.Plots.sendDataToCloud(gd);
expect(form.action).toContain('/dummy/external');
expect(form.method).toBe('post');
})
.catch(failTest)
.then(done);
});
it('has lesser priotiy then window env', function(done) {
window.PLOTLYENV = {BASE_URL: 'yo'};
Plotly.plot(gd, [], {}, {plotlyServerURL: 'dummy'})
.then(function() {
expect(gd._context.plotlyServerURL).toBe('dummy');
Plotly.Plots.sendDataToCloud(gd);
expect(form.action).toContain('/yo/external');
expect(form.method).toBe('post');
})
.catch(failTest)
.then(function() {
delete window.PLOTLY_ENV;
done();
});
});
});
describe('responsive figure', function() {
var gd;
var startWidth = 960, startHeight = 400;
var newWidth = 400, newHeight = 700;
var data = [{x: [1, 2, 3, 4], y: [5, 10, 2, 8]}];
beforeEach(function() {
viewport.set(startWidth, startHeight);
gd = createGraphDiv();
// Make the graph fill the parent
gd.style.width = '100%';
gd.style.height = '100%';
});
afterEach(function() {
Plotly.purge(gd); // Needed to remove all event listeners
destroyGraphDiv();
viewport.reset();
});
function checkLayoutSize(width, height) {
expect(gd._fullLayout.width).toBe(width);
expect(gd._fullLayout.height).toBe(height);
var svg = document.getElementsByClassName('main-svg')[0];
expect(+svg.getAttribute('width')).toBe(width);
expect(+svg.getAttribute('height')).toBe(height);
}
function testResponsive() {
checkLayoutSize(startWidth, startHeight);
viewport.set(newWidth, newHeight);
return Promise.resolve()
.then(delay(200))
.then(function() {
checkLayoutSize(newWidth, newHeight);
})
.catch(failTest);
}
it('should resize when the viewport width/height changes', function(done) {
Plotly.plot(gd, data, {}, {responsive: true})
.then(testResponsive)
.then(done);
});
it('should still be responsive if the plot is edited', function(done) {
Plotly.plot(gd, data, {}, {responsive: true})
.then(function() {return Plotly.restyle(gd, 'y[0]', data[0].y[0] + 2);})
.then(testResponsive)
.then(done);
});
it('should still be responsive if the plot is purged and replotted', function(done) {
Plotly.plot(gd, data, {}, {responsive: true})
.then(function() {return Plotly.newPlot(gd, data, {}, {responsive: true});})
.then(testResponsive)
.then(done);
});
it('should only have one resize handler when plotted more than once', function(done) {
var cntWindowResize = 0;
window.addEventListener('resize', function() {cntWindowResize++;});
spyOn(Plotly.Plots, 'resize').and.callThrough();
Plotly.plot(gd, data, {}, {responsive: true})
.then(function() {return Plotly.restyle(gd, 'y[0]', data[0].y[0] + 2);})
.then(function() {viewport.set(newWidth, newHeight);})
.then(delay(200))
// .then(function() {viewport.set(newWidth, 2 * newHeight);}).then(delay(200))
.then(function() {
expect(cntWindowResize).toBe(1);
expect(Plotly.Plots.resize.calls.count()).toBe(1);
})
.catch(failTest)
.then(done);
});
it('should become responsive if configured as such via Plotly.react', function(done) {
Plotly.plot(gd, data, {}, {responsive: false})
.then(function() {return Plotly.react(gd, data, {}, {responsive: true});})
.then(testResponsive)
.then(done);
});
it('should stop being responsive if configured as such via Plotly.react', function(done) {
Plotly.plot(gd, data, {}, {responsive: true})
// Check initial size
.then(function() {checkLayoutSize(startWidth, startHeight);})
// Turn off responsiveness
.then(function() {return Plotly.react(gd, data, {}, {responsive: false});})
// Resize viewport
.then(function() {viewport.set(newWidth, newHeight);})
// Wait for resize to happen (Plotly.resize has an internal timeout)
.then(delay(200))
// Check that final figure's size hasn't changed
.then(function() {checkLayoutSize(startWidth, startHeight);})
.catch(failTest)
.then(done);
});
});
});