Skip to content

Commit e9901c2

Browse files
Added numeric categories tests to shapes_test
1 parent 0b48de5 commit e9901c2

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict';
2+
3+
var Plotly = require('../../../lib/index');
4+
var d3SelectAll = require('../../strict-d3').selectAll;
5+
var drag = require('../assets/drag');
6+
7+
module.exports.shapeColors = [
8+
'red',
9+
'green',
10+
'blue',
11+
'yellow',
12+
'orange',
13+
];
14+
15+
module.exports.shapeColorTab = {
16+
red: 'rgb(255, 0, 0)',
17+
green: 'rgb(0, 128, 0)',
18+
blue: 'rgb(0, 0, 255)',
19+
yellow: 'rgb(255, 255, 0)',
20+
orange: 'rgb(255, 165, 0)'
21+
};
22+
23+
function createDesc(xcats, ycats) {
24+
return 'x is specified as ' + xcats + ', y is specified as ' + ycats;
25+
}
26+
27+
module.exports.shapeColorTestDesc = {
28+
red: createDesc('indices', 'numeric strings'),
29+
green: createDesc('indices', 'numeric string categories, one of which is missing'),
30+
blue: createDesc('string categories', 'indices'),
31+
yellow: createDesc('numeric string categories, one of which is missing', 'indices'),
32+
orange: createDesc('(path) indices', '(path) indices'),
33+
};
34+
35+
function keepMthOfNShapes(shapes, nshapes, m) {
36+
var oneShape = shapes[m];
37+
var tailShapes = shapes.slice(nshapes);
38+
return [oneShape].concat(tailShapes);
39+
}
40+
41+
module.exports.plotMthShape = function(gd, m) {
42+
var mock;
43+
mock = require('../../image/mocks/numeric-string-categories.json');
44+
mock.config = {editable: true};
45+
mock.layout.shapes = keepMthOfNShapes(mock.layout.shapes, mock.layout.shapes.length, m);
46+
var promise = Plotly.newPlot(gd, mock);
47+
return promise;
48+
};
49+
50+
module.exports.plotColoredShape = function(gd, color) {
51+
var mock = require('../../image/mocks/numeric-string-categories.json');
52+
var m;
53+
mock.layout.shapes.forEach(function(e, i) {
54+
if(e && e.line && e.line.color === color) { m = i; }
55+
});
56+
return module.exports.plotMthShape(gd, m);
57+
};
58+
59+
module.exports.getShapeByColor = function(color) {
60+
var rgbColor = module.exports.shapeColorTab[color];
61+
var shapeNode = d3SelectAll('.shapelayer path[style*="stroke: ' + rgbColor + '"]').node();
62+
return shapeNode;
63+
};
64+
65+
module.exports.dragShapeByColor = function(color, dx, dy) {
66+
var shapeNode = module.exports.getShapeByColor(color);
67+
return drag({node: shapeNode, dpos: [dx, dy]});
68+
};
69+
70+
module.exports.bboxChange = function(fromBBox, toBBox) {
71+
var ret = {
72+
dx: toBBox.x - fromBBox.x,
73+
dy: toBBox.y - fromBBox.y,
74+
dwidth: toBBox.width - fromBBox.width,
75+
dheight: toBBox.height - fromBBox.height,
76+
};
77+
return ret;
78+
};
79+
80+
// Returns a promise that resolves with an object that looks like:
81+
// {
82+
// dx: ... // change in horizontal shape position
83+
// dy: ... // change in vertical shape position
84+
// dwidth: ... // change in shape width
85+
// dheight: ... // change in shape height
86+
// }
87+
module.exports.testDragShapeByColor = function(gd, shapeColor, dx, dy) {
88+
var startBBox, endBBox;
89+
// Plot the shape with the specified color
90+
var promise = module.exports.plotColoredShape(gd, shapeColor).then(function() {
91+
// get the bounding box of the shape with the specified color
92+
var shapeNode = module.exports.getShapeByColor(shapeColor);
93+
startBBox = shapeNode.getBoundingClientRect();
94+
// drag that shape
95+
return module.exports.dragShapeByColor(shapeColor, dx, dy);
96+
}).then(function() {
97+
// get the new bounding box of the shape
98+
var shapeNode = module.exports.getShapeByColor(shapeColor);
99+
endBBox = shapeNode.getBoundingClientRect();
100+
return module.exports.bboxChange(startBBox, endBBox);
101+
});
102+
return promise;
103+
};
104+
105+
module.exports.d3SelectAll = d3SelectAll;

test/jasmine/tests/shapes_test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var createGraphDiv = require('../assets/create_graph_div');
1212
var destroyGraphDiv = require('../assets/destroy_graph_div');
1313

1414
var drag = require('../assets/drag');
15+
var numericCategory = require('../assets/numeric_category');
16+
1517

1618
var customAssertions = require('../assets/custom_assertions');
1719
var assertElemRightTo = customAssertions.assertElemRightTo;
@@ -1649,3 +1651,28 @@ describe('Test shapes', function() {
16491651
return coordinates;
16501652
}
16511653
});
1654+
1655+
fdescribe('Numeric string categories', function() {
1656+
'use strict';
1657+
1658+
var gd; var dx = 100; var dy = 100;
1659+
1660+
beforeEach(function() {
1661+
gd = createGraphDiv();
1662+
});
1663+
1664+
afterEach(destroyGraphDiv);
1665+
1666+
numericCategory.shapeColors.forEach(function(color) {
1667+
it('should correctly drag shapes where ' + numericCategory.shapeColorTestDesc[color],
1668+
function(done) {
1669+
var promise = numericCategory.testDragShapeByColor(gd, color, dx, dy);
1670+
promise.then(function(dbbox) {
1671+
expect(dbbox.dx).toBeCloseTo(dx);
1672+
expect(dbbox.dy).toBeCloseTo(dy);
1673+
expect(dbbox.dwidth).toBeCloseTo(0);
1674+
expect(dbbox.dheight).toBeCloseTo(0);
1675+
}).then(done);
1676+
});
1677+
});
1678+
});

0 commit comments

Comments
 (0)