Skip to content

Commit 5e62c08

Browse files
committed
test: add autorange test for annotations & shapes
1 parent c562b93 commit 5e62c08

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

test/jasmine/tests/annotations_test.js

+81
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Lib = require('@src/lib');
66
var Dates = require('@src/lib/dates');
77

88
var d3 = require('d3');
9+
var customMatchers = require('../assets/custom_matchers');
910
var createGraphDiv = require('../assets/create_graph_div');
1011
var destroyGraphDiv = require('../assets/destroy_graph_div');
1112

@@ -117,3 +118,83 @@ describe('annotations relayout', function() {
117118

118119
});
119120
});
121+
122+
describe('annotations autosize', function() {
123+
'use strict';
124+
125+
var mock = Lib.extendDeep({}, require('@mocks/annotations-autorange.json'));
126+
var gd;
127+
128+
beforeAll(function() {
129+
jasmine.addMatchers(customMatchers);
130+
});
131+
132+
afterEach(destroyGraphDiv);
133+
134+
it('should adapt to relayout calls', function(done) {
135+
gd = createGraphDiv();
136+
137+
function assertRanges(x, y, x2, y2, x3, y3) {
138+
var fullLayout = gd._fullLayout;
139+
var PREC = 1;
140+
141+
expect(fullLayout.xaxis.range).toBeCloseToArray(x, PREC, '- xaxis');
142+
expect(fullLayout.yaxis.range).toBeCloseToArray(y, PREC, '- yaxis');
143+
expect(fullLayout.xaxis2.range).toBeCloseToArray(x2, PREC, 'xaxis2');
144+
expect(fullLayout.yaxis2.range).toBeCloseToArray(y2, PREC, 'yaxis2');
145+
expect(fullLayout.xaxis3.range).toBeCloseToArray(x3, PREC, 'xaxis3');
146+
expect(fullLayout.yaxis3.range).toBeCloseToArray(y3, PREC, 'yaxis3');
147+
}
148+
149+
Plotly.plot(gd, mock).then(function() {
150+
assertRanges(
151+
[0.97, 2.03], [0.97, 2.03],
152+
[-0.32, 3.38], [0.42, 2.58],
153+
[0.9, 2.1], [0.86, 2.14]
154+
);
155+
156+
return Plotly.relayout(gd, {
157+
'annotations[0].visible': false,
158+
'annotations[4].visible': false,
159+
'annotations[8].visible': false
160+
});
161+
})
162+
.then(function() {
163+
assertRanges(
164+
[1.44, 2.02], [0.97, 2.03],
165+
[1.31, 2.41], [0.42, 2.58],
166+
[1.44, 2.1], [0.86, 2.14]
167+
);
168+
169+
return Plotly.relayout(gd, {
170+
'annotations[2].visible': false,
171+
'annotations[5].visible': false,
172+
'annotations[9].visible': false
173+
});
174+
})
175+
.then(function() {
176+
assertRanges(
177+
[1.44, 2.02], [0.99, 1.52],
178+
[0.5, 2.5], [0.42, 2.58],
179+
[0.5, 2.5], [0.86, 2.14]
180+
);
181+
182+
return Plotly.relayout(gd, {
183+
'annotations[0].visible': true,
184+
'annotations[2].visible': true,
185+
'annotations[4].visible': true,
186+
'annotations[5].visible': true,
187+
'annotations[8].visible': true,
188+
'annotations[9].visible': true
189+
});
190+
})
191+
.then(function() {
192+
assertRanges(
193+
[0.97, 2.03], [0.97, 2.03],
194+
[-0.32, 3.38], [0.42, 2.58],
195+
[0.9, 2.1], [0.86, 2.14]
196+
);
197+
})
198+
.then(done);
199+
});
200+
});

test/jasmine/tests/shapes_test.js

+64
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Lib = require('@src/lib');
77
var Axes = PlotlyInternal.Axes;
88

99
var d3 = require('d3');
10+
var customMatchers = require('../assets/custom_matchers');
1011
var createGraphDiv = require('../assets/create_graph_div');
1112
var destroyGraphDiv = require('../assets/destroy_graph_div');
1213

@@ -255,6 +256,69 @@ describe('Test shapes:', function() {
255256
});
256257
});
257258

259+
describe('shapes autosize', function() {
260+
'use strict';
261+
262+
var gd;
263+
264+
beforeAll(function() {
265+
jasmine.addMatchers(customMatchers);
266+
});
267+
268+
afterEach(destroyGraphDiv);
269+
270+
it('should adapt to relayout calls', function(done) {
271+
gd = createGraphDiv();
272+
273+
var mock = {
274+
data: [{}],
275+
layout: {
276+
shapes: [{
277+
type: 'line',
278+
x0: 0,
279+
y0: 0,
280+
x1: 1,
281+
y1: 1
282+
}, {
283+
type: 'line',
284+
x0: 0,
285+
y0: 0,
286+
x1: 2,
287+
y1: 2
288+
}]
289+
}
290+
};
291+
292+
function assertRanges(x, y) {
293+
var fullLayout = gd._fullLayout;
294+
var PREC = 1;
295+
296+
expect(fullLayout.xaxis.range).toBeCloseToArray(x, PREC, '- xaxis');
297+
expect(fullLayout.yaxis.range).toBeCloseToArray(y, PREC, '- yaxis');
298+
}
299+
300+
Plotly.plot(gd, mock).then(function() {
301+
assertRanges([0, 2], [0, 2]);
302+
303+
return Plotly.relayout(gd, { 'shapes[1].visible': false });
304+
})
305+
.then(function() {
306+
assertRanges([0, 1], [0, 1]);
307+
308+
return Plotly.relayout(gd, { 'shapes[1].visible': true });
309+
})
310+
.then(function() {
311+
assertRanges([0, 2], [0, 2]);
312+
313+
return Plotly.relayout(gd, { 'shapes[0].x1': 3 });
314+
})
315+
.then(function() {
316+
assertRanges([0, 3], [0, 2]);
317+
})
318+
.then(done);
319+
});
320+
});
321+
258322
describe('Test shapes: a plot with shapes and an overlaid axis', function() {
259323
'use strict';
260324

0 commit comments

Comments
 (0)