-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathlegend_scroll_test.js
More file actions
131 lines (94 loc) · 4.36 KB
/
legend_scroll_test.js
File metadata and controls
131 lines (94 loc) · 4.36 KB
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
var Plotly = require('@lib/index');
var createGraph = require('../assets/create_graph_div');
var destroyGraph = require('../assets/destroy_graph_div');
var getBBox = require('../assets/get_bbox');
var mock = require('../../image/mocks/legend_scroll.json');
describe('The legend', function() {
'use strict';
var gd, legend;
function countLegendGroups(gd) {
return gd._fullLayout._toppaper.selectAll('g.legend').size();
}
function countLegendClipPaths(gd) {
var uid = gd._fullLayout._uid;
return gd._fullLayout._topdefs.selectAll('#legend' + uid).size();
}
describe('when plotted with many traces', function() {
beforeEach(function() {
gd = createGraph();
Plotly.plot(gd, mock.data, mock.layout);
legend = document.getElementsByClassName('legend')[0];
});
afterEach(destroyGraph);
it('should not exceed plot height', function() {
var legendHeight = getBBox(legend).height,
plotHeight = gd._fullLayout.height - gd._fullLayout.margin.t - gd._fullLayout.margin.b;
expect(+legendHeight).toBe(plotHeight);
});
it('should insert a scrollbar', function() {
var scrollBar = legend.getElementsByClassName('scrollbar')[0];
expect(scrollBar).toBeDefined();
expect(scrollBar.getAttribute('x')).not.toBe(null);
});
it('should scroll when there\'s a wheel event', function() {
var scrollBox = legend.getElementsByClassName('scrollbox')[0];
legend.dispatchEvent(scrollTo(100));
// Compare against -5 because of a scroll factor of 20
// ( 100 / 20 === 5 )
expect(scrollBox.getAttribute('transform')).toBe('translate(0, -5)');
expect(scrollBox.getAttribute('data-scroll')).toBe('-5');
});
it('should constrain scrolling to the contents', function() {
var scrollBox = legend.getElementsByClassName('scrollbox')[0];
legend.dispatchEvent(scrollTo(-100));
expect(scrollBox.getAttribute('transform')).toBe('translate(0, 0)');
legend.dispatchEvent(scrollTo(100000));
expect(scrollBox.getAttribute('transform')).toBe('translate(0, -179)');
});
it('should scale the scrollbar movement from top to bottom', function() {
var scrollBar = legend.getElementsByClassName('scrollbar')[0],
legendHeight = getBBox(legend).height;
// The scrollbar is 20px tall and has 4px margins
legend.dispatchEvent(scrollTo(-1000));
expect(+scrollBar.getAttribute('y')).toBe(4);
legend.dispatchEvent(scrollTo(10000));
expect(+scrollBar.getAttribute('y')).toBe(legendHeight - 4 - 20);
});
it('should be removed from DOM when \'showlegend\' is relayout\'ed to false', function(done) {
expect(countLegendGroups(gd)).toBe(1);
expect(countLegendClipPaths(gd)).toBe(1);
Plotly.relayout(gd, 'showlegend', false).then(function() {
expect(countLegendGroups(gd)).toBe(0);
expect(countLegendClipPaths(gd)).toBe(0);
done();
});
});
});
describe('when plotted with few traces', function() {
var gd;
beforeEach(function() {
gd = createGraph();
var data = [{ x: [1,2,3], y: [2,3,4], name: 'Test' }];
var layout = { showlegend: true };
Plotly.plot(gd, data, layout);
});
afterEach(destroyGraph);
it('should not display the scrollbar', function() {
var scrollBar = document.getElementsByClassName('scrollbar')[0];
expect(+scrollBar.getAttribute('width')).toBe(0);
expect(+scrollBar.getAttribute('height')).toBe(0);
});
it('should be removed from DOM when \'showlegend\' is relayout\'ed to false', function(done) {
expect(countLegendGroups(gd)).toBe(1);
expect(countLegendClipPaths(gd)).toBe(1);
Plotly.relayout(gd, 'showlegend', false).then(function() {
expect(countLegendGroups(gd)).toBe(0);
expect(countLegendClipPaths(gd)).toBe(0);
done();
});
});
});
});
function scrollTo(delta) {
return new WheelEvent('wheel', { deltaY: delta });
}