forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes_test.js
192 lines (154 loc) · 6.08 KB
/
shapes_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
var d3 = require('d3');
var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
describe('Test shapes:', function() {
'use strict';
var mock = require('@mocks/shapes.json');
var gd;
beforeEach(function(done) {
gd = createGraphDiv();
var mockData = Lib.extendDeep([], mock.data),
mockLayout = Lib.extendDeep({}, mock.layout);
Plotly.plot(gd, mockData, mockLayout).then(done);
});
afterEach(destroyGraphDiv);
function countShapesInLowerLayer() {
return gd._fullLayout.shapes.filter(isShapeInLowerLayer).length;
}
function countShapesInUpperLayer() {
return gd._fullLayout.shapes.filter(isShapeInUpperLayer).length;
}
function countShapesInSubplots() {
return gd._fullLayout.shapes.filter(isShapeInSubplot).length;
}
function isShapeInUpperLayer(shape) {
return shape.layer !== 'below';
}
function isShapeInLowerLayer(shape) {
return (shape.xref === 'paper' && shape.yref === 'paper') &&
!isShapeInUpperLayer(shape);
}
function isShapeInSubplot(shape) {
return !isShapeInUpperLayer(shape) && !isShapeInLowerLayer(shape);
}
function countShapeLowerLayerNodes() {
return d3.selectAll('.shapelayer-below').size();
}
function countShapeUpperLayerNodes() {
return d3.selectAll('.shapelayer-above').size();
}
function countShapeLayerNodesInSubplots() {
return d3.selectAll('.shapelayer-subplot').size();
}
function countSubplots(gd) {
return Object.getOwnPropertyNames(gd._fullLayout._plots || {}).length;
}
function countShapePathsInLowerLayer() {
return d3.selectAll('.shapelayer-below > path').size();
}
function countShapePathsInUpperLayer() {
return d3.selectAll('.shapelayer-above > path').size();
}
function countShapePathsInSubplots() {
return d3.selectAll('.shapelayer-subplot > path').size();
}
describe('*shapeLowerLayer*', function() {
it('has one node', function() {
expect(countShapeLowerLayerNodes()).toEqual(1);
});
it('has as many *path* nodes as shapes in the lower layer', function() {
expect(countShapePathsInLowerLayer())
.toEqual(countShapesInLowerLayer());
});
it('should be able to get relayout', function(done) {
Plotly.relayout(gd, {height: 200, width: 400}).then(function() {
expect(countShapeLowerLayerNodes()).toEqual(1);
expect(countShapePathsInLowerLayer())
.toEqual(countShapesInLowerLayer());
}).then(done);
});
});
describe('*shapeUpperLayer*', function() {
it('has one node', function() {
expect(countShapeUpperLayerNodes()).toEqual(1);
});
it('has as many *path* nodes as shapes in the upper layer', function() {
expect(countShapePathsInUpperLayer())
.toEqual(countShapesInUpperLayer());
});
it('should be able to get relayout', function(done) {
Plotly.relayout(gd, {height: 200, width: 400}).then(function() {
expect(countShapeUpperLayerNodes()).toEqual(1);
expect(countShapePathsInUpperLayer())
.toEqual(countShapesInUpperLayer());
}).then(done);
});
});
describe('each *subplot*', function() {
it('has one *shapelayer*', function() {
expect(countShapeLayerNodesInSubplots())
.toEqual(countSubplots(gd));
});
it('has as many *path* nodes as shapes in the subplot', function() {
expect(countShapePathsInSubplots())
.toEqual(countShapesInSubplots());
});
it('should be able to get relayout', function(done) {
Plotly.relayout(gd, {height: 200, width: 400}).then(function() {
expect(countShapeLayerNodesInSubplots())
.toEqual(countSubplots(gd));
expect(countShapePathsInSubplots())
.toEqual(countShapesInSubplots());
}).then(done);
});
});
function countShapes(gd) {
return gd.layout.shapes ?
gd.layout.shapes.length :
0;
}
function getLastShape(gd) {
return gd.layout.shapes ?
gd.layout.shapes[gd.layout.shapes.length - 1] :
null;
}
function getRandomShape() {
return {
x0: Math.random(),
y0: Math.random(),
x1: Math.random(),
y1: Math.random()
};
}
describe('Plotly.relayout', function() {
it('should be able to add a shape', function(done) {
var pathCount = countShapePathsInUpperLayer();
var index = countShapes(gd);
var shape = getRandomShape();
Plotly.relayout(gd, 'shapes[' + index + ']', shape).then(function()
{
expect(countShapePathsInUpperLayer()).toEqual(pathCount + 1);
expect(getLastShape(gd)).toEqual(shape);
expect(countShapes(gd)).toEqual(index + 1);
}).then(done);
});
it('should be able to remove a shape', function(done) {
var pathCount = countShapePathsInUpperLayer();
var index = countShapes(gd);
var shape = getRandomShape();
Plotly.relayout(gd, 'shapes[' + index + ']', shape).then(function()
{
expect(countShapePathsInUpperLayer()).toEqual(pathCount + 1);
expect(getLastShape(gd)).toEqual(shape);
expect(countShapes(gd)).toEqual(index + 1);
}).then(function() {
Plotly.relayout(gd, 'shapes[' + index + ']', 'remove');
}).then(function() {
expect(countShapePathsInUpperLayer()).toEqual(pathCount);
expect(countShapes(gd)).toEqual(index);
}).then(done);
});
});
});