Skip to content

Commit 36c3a69

Browse files
committed
Add tests for scrolling
1 parent e66ab81 commit 36c3a69

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

test/image/mocks/legend_scroll.json

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"data": [
3+
{
4+
"x": [1,2,3],
5+
"y": [1,2,3],
6+
"type": "scatter"
7+
},
8+
{
9+
"x": [1,2,3],
10+
"y": [2,3,4],
11+
"type": "bar"
12+
},
13+
{
14+
"x": [1,2,3],
15+
"y": [3,4,5],
16+
"type": "scatter"
17+
},
18+
{
19+
"x": [1,2,3],
20+
"y": [4,5,6],
21+
"type": "bar"
22+
},
23+
{
24+
"x": [1,2,3],
25+
"y": [5,6,7],
26+
"type": "scatter"
27+
},
28+
{
29+
"x": [1,2,3],
30+
"y": [6,7,8],
31+
"type": "bar"
32+
},
33+
{
34+
"x": [1,2,3],
35+
"y": [7,8,9],
36+
"type": "scatter"
37+
},
38+
{
39+
"x": [1,2,3],
40+
"y": [8,9,10],
41+
"type": "bar"
42+
},
43+
{
44+
"x": [1,2,3],
45+
"y": [9,10,11],
46+
"type": "scatter"
47+
},
48+
{
49+
"x": [1,2,3],
50+
"y": [10,11,12],
51+
"type": "bar"
52+
},
53+
{
54+
"x": [1,2,3],
55+
"y": [11,12,13],
56+
"type": "scatter"
57+
},
58+
{
59+
"x": [1,2,3],
60+
"y": [12,13,14],
61+
"type": "bar"
62+
},
63+
{
64+
"x": [1,2,3],
65+
"y": [13,14,15],
66+
"type": "scatter"
67+
},
68+
{
69+
"x": [1,2,3],
70+
"y": [14,15,16],
71+
"type": "bar"
72+
},
73+
{
74+
"x": [1,2,3],
75+
"y": [15,16,17],
76+
"type": "scatter"
77+
},
78+
{
79+
"x": [1,2,3],
80+
"y": [16,17,18],
81+
"type": "bar"
82+
},
83+
{
84+
"x": [1,2,3],
85+
"y": [17,18,19],
86+
"type": "scatter"
87+
},
88+
{
89+
"x": [1,2,3],
90+
"y": [18,19,20],
91+
"type": "bar"
92+
},
93+
{
94+
"x": [1,2,3],
95+
"y": [19,20,21],
96+
"type": "scatter"
97+
},
98+
{
99+
"x": [1,2,3],
100+
"y": [20,21,22],
101+
"type": "bar"
102+
},
103+
{
104+
"x": [1,2,3],
105+
"y": [21,22,23],
106+
"type": "scatter"
107+
},
108+
{
109+
"x": [1,2,3],
110+
"y": [22,23,24],
111+
"type": "bar"
112+
},
113+
{
114+
"x": [1,2,3],
115+
"y": [23,24,25],
116+
"type": "scatter"
117+
}
118+
],
119+
"layout": {
120+
"legend": {
121+
"bordercolor": "#000000",
122+
"borderwidth": 1,
123+
"bgcolor": "#eeffee"
124+
}
125+
}
126+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var Plotly = require('@lib/index');
2+
var createGraph = require('../assets/create_graph_div');
3+
var destroyGraph = require('../assets/destroy_graph_div');
4+
var mock = require('../../image/mocks/legend_scroll.json');
5+
6+
describe('The legend', function() {
7+
var gd,
8+
legend;
9+
10+
describe('when plotted with many traces', function() {
11+
beforeEach(function() {
12+
gd = createGraph();
13+
Plotly.plot(gd, mock.data, mock.layout);
14+
legend = document.getElementsByClassName('legend')[0];
15+
});
16+
17+
afterEach(destroyGraph);
18+
19+
it('should not exceed plot height', function() {
20+
var legendHeight = legend.getAttribute('height'),
21+
plotHeight = gd._fullLayout.height - gd._fullLayout.margin.t - gd._fullLayout.margin.b;
22+
23+
expect(+legendHeight).toBe(plotHeight);
24+
});
25+
26+
it('should insert a scrollbar', function() {
27+
var scrollBar = legend.getElementsByClassName('scrollbar')[0];
28+
29+
expect(scrollBar).toBeDefined();
30+
expect(scrollBar.getAttribute('x')).not.toBe(null);
31+
});
32+
33+
it('should scroll when there\'s a wheel event', function() {
34+
var scrollBox = legend.getElementsByClassName('scrollbox')[0];
35+
36+
legend.dispatchEvent(scrollTo(100));
37+
38+
// Compare against -5 because of a scroll factor of 20
39+
// ( 100 / 20 === 5 )
40+
expect(scrollBox.getAttribute('transform')).toBe('translate(0, -5)');
41+
expect(scrollBox.getAttribute('data-scroll')).toBe('-5');
42+
});
43+
44+
it('should constrain scrolling to the contents', function() {
45+
var scrollBox = legend.getElementsByClassName('scrollbox')[0],
46+
scrollHeight = scrollBox.getBoundingClientRect().height,
47+
legendHeight = legend.getAttribute('height'),
48+
maxHeight = legendHeight - scrollHeight - 12;
49+
50+
legend.dispatchEvent(scrollTo(-100));
51+
expect(scrollBox.getAttribute('transform')).toBe('translate(0, 0)');
52+
53+
legend.dispatchEvent(scrollTo(10000));
54+
expect(scrollBox.getAttribute('transform')).toBe('translate(0, ' + maxHeight + ')');
55+
});
56+
57+
it('should scale the scrollbar movement from top to bottom', function() {
58+
var scrollBar = legend.getElementsByClassName('scrollbar')[0],
59+
legendHeight = legend.getAttribute('height');
60+
61+
// The scrollbar is 20px tall and has 4px margins
62+
63+
legend.dispatchEvent(scrollTo(-1000));
64+
expect(+scrollBar.getAttribute('y')).toBe(4);
65+
66+
legend.dispatchEvent(scrollTo(10000));
67+
expect(+scrollBar.getAttribute('y')).toBe(legendHeight - 4 - 20);
68+
});
69+
});
70+
71+
describe('when plotted with few traces', function() {
72+
var gd;
73+
74+
beforeEach(function() {
75+
gd = createGraph();
76+
Plotly.plot(gd, [{ x: [1,2,3], y: [2,3,4], name: 'Test' }], {});
77+
});
78+
79+
afterEach(destroyGraph);
80+
81+
it('should not display the scrollbar', function() {
82+
var scrollBar = document.getElementsByClassName('scrollbar')[0];
83+
84+
expect(scrollBar).toBeUndefined();
85+
});
86+
});
87+
});
88+
89+
90+
function scrollTo(delta) {
91+
return new WheelEvent('wheel', { deltaY: delta });
92+
}

0 commit comments

Comments
 (0)