Skip to content

Commit 335bb40

Browse files
committed
add in-house getBBox test asset:
- needed to determine bounding box of clipped element, as the stock SVG getBBox method does not take clip path into consideration - use it legend scroll test to determine the height of the legend
1 parent 0007807 commit 335bb40

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

test/jasmine/assets/get_bbox.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
var d3 = require('d3');
4+
5+
6+
// In-house implementation of SVG getBBox that takes clip paths into account
7+
module.exports = function getBBox(element) {
8+
var elementBBox = element.getBBox();
9+
10+
var s = d3.select(element);
11+
var clipPathAttr = s.attr('clip-path');
12+
13+
if(!clipPathAttr) return elementBBox;
14+
15+
// only supports 'url(#<id>)' at the moment
16+
var clipPathId = clipPathAttr.substring(5, clipPathAttr.length-1);
17+
var clipPath = d3.select('#' + clipPathId).node();
18+
19+
return minBBox(elementBBox, clipPath.getBBox());
20+
};
21+
22+
function minBBox(bbox1, bbox2) {
23+
var keys = ['x', 'y', 'width', 'height'];
24+
var out = {};
25+
26+
function min(attr) {
27+
return Math.min(bbox1[attr], bbox2[attr]);
28+
}
29+
30+
keys.forEach(function(key) {
31+
out[key] = min(key);
32+
});
33+
34+
return out;
35+
}

test/jasmine/tests/legend_scroll_test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
var Plotly = require('@lib/index');
22
var createGraph = require('../assets/create_graph_div');
33
var destroyGraph = require('../assets/destroy_graph_div');
4+
var getBBox = require('../assets/get_bbox');
45
var mock = require('../../image/mocks/legend_scroll.json');
56

7+
68
describe('The legend', function() {
7-
var gd,
8-
legend;
9+
'use strict';
10+
11+
var gd, legend;
912

1013
describe('when plotted with many traces', function() {
1114
beforeEach(function() {
@@ -17,7 +20,7 @@ describe('The legend', function() {
1720
afterEach(destroyGraph);
1821

1922
it('should not exceed plot height', function() {
20-
var legendHeight = legend.getAttribute('height'),
23+
var legendHeight = getBBox(legend).height,
2124
plotHeight = gd._fullLayout.height - gd._fullLayout.margin.t - gd._fullLayout.margin.b;
2225

2326
expect(+legendHeight).toBe(plotHeight);
@@ -53,7 +56,7 @@ describe('The legend', function() {
5356

5457
it('should scale the scrollbar movement from top to bottom', function() {
5558
var scrollBar = legend.getElementsByClassName('scrollbar')[0],
56-
legendHeight = legend.getAttribute('height');
59+
legendHeight = getBBox(legend).height;
5760

5861
// The scrollbar is 20px tall and has 4px margins
5962

0 commit comments

Comments
 (0)