Skip to content

Commit bd07df7

Browse files
committed
simplify inner plot size tests with custom_assertions
1 parent 222f396 commit bd07df7

File tree

5 files changed

+46
-32
lines changed

5 files changed

+46
-32
lines changed

test/jasmine/assets/custom_assertions.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,29 @@ exports.assertElemInside = function(elem, container, msg) {
216216
contBB.top < elemBB.top &&
217217
contBB.bottom > elemBB.bottom).toBe(true, msg);
218218
};
219+
220+
/*
221+
* quick plot area dimension check: test width and/or height of the inner
222+
* plot area (single subplot) to verify that the margins are as expected
223+
* opts can have keys (all optional):
224+
* width (exact width match)
225+
* height (exact height match)
226+
* widthLessThan (width must be less than this)
227+
* heightLessThan (height must be less than this)
228+
*/
229+
exports.assertPlotSize = function(opts, msg) {
230+
var width = opts.width;
231+
var height = opts.height;
232+
var widthLessThan = opts.widthLessThan;
233+
var heightLessThan = opts.heightLessThan;
234+
235+
var actualWidth = d3.select('.ygrid').node().getBoundingClientRect().width;
236+
var actualHeight = d3.select('.xgrid').node().getBoundingClientRect().height;
237+
238+
var msgPlus = msg ? ': ' + msg : '';
239+
240+
if(width) expect(actualWidth).toBeWithin(width, 1, 'width' + msgPlus);
241+
if(height) expect(actualHeight).toBeWithin(height, 1, 'height' + msgPlus);
242+
if(widthLessThan) expect(actualWidth).toBeLessThan(widthLessThan - 1, 'widthLessThan' + msgPlus);
243+
if(heightLessThan) expect(actualHeight).toBeLessThan(heightLessThan - 1, 'heightLessThan' + msgPlus);
244+
};

test/jasmine/tests/legend_test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var failTest = require('../assets/fail_test');
1313
var delay = require('../assets/delay');
1414
var createGraphDiv = require('../assets/create_graph_div');
1515
var destroyGraphDiv = require('../assets/destroy_graph_div');
16+
var assertPlotSize = require('../assets/custom_assertions').assertPlotSize;
1617

1718
describe('legend defaults', function() {
1819
'use strict';
@@ -530,30 +531,26 @@ describe('legend relayout update', function() {
530531
width: 500
531532
}});
532533

533-
function plotWidth() {
534-
return d3.select('.ygrid').node().getBoundingClientRect().width;
535-
}
536-
537534
Plotly.newPlot(gd, mockCopy.data, mockCopy.layout)
538535
.then(function() {
539536
expect(d3.selectAll('g.legend').size()).toBe(1);
540537
// check that the margins changed
541-
expect(plotWidth()).toBeLessThan(400);
538+
assertPlotSize({widthLessThan: 400});
542539
return Plotly.relayout(gd, {showlegend: false});
543540
})
544541
.then(function() {
545542
expect(d3.selectAll('g.legend').size()).toBe(0);
546-
expect(plotWidth()).toBe(400);
543+
assertPlotSize({width: 400});
547544
return Plotly.relayout(gd, {showlegend: true});
548545
})
549546
.then(function() {
550547
expect(d3.selectAll('g.legend').size()).toBe(1);
551-
expect(plotWidth()).toBeLessThan(400);
548+
assertPlotSize({widthLessThan: 400});
552549
return Plotly.relayout(gd, {'legend.x': 0.7});
553550
})
554551
.then(function() {
555552
expect(d3.selectAll('g.legend').size()).toBe(1);
556-
expect(plotWidth()).toBe(400);
553+
assertPlotSize({width: 400});
557554
})
558555
.catch(failTest)
559556
.then(done);

test/jasmine/tests/range_selector_test.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var failTest = require('../assets/fail_test');
1111
var getRectCenter = require('../assets/get_rect_center');
1212
var mouseEvent = require('../assets/mouse_event');
1313
var setConvert = require('@src/plots/cartesian/set_convert');
14+
var assertPlotSize = require('../assets/custom_assertions').assertPlotSize;
1415

1516

1617
describe('range selector defaults:', function() {
@@ -620,37 +621,25 @@ describe('range selector automargin', function() {
620621

621622
afterEach(destroyGraphDiv);
622623

623-
function plotWidth() {
624-
return d3.selectAll('.ygrid').node().getBoundingClientRect().width;
625-
}
626-
627-
function plotHeight() {
628-
return d3.selectAll('.xgrid').node().getBoundingClientRect().height;
629-
}
630-
631624
it('updates automargin when hiding, showing, or moving', function(done) {
632-
expect(plotWidth()).toBe(400);
633-
expect(plotHeight()).toBe(300);
625+
assertPlotSize({width: 400, height: 300}, 'initial');
634626

635627
Plotly.relayout(gd, {
636628
'xaxis.rangeselector.y': 1.3,
637629
'xaxis.rangeselector.xanchor': 'center'
638630
})
639631
.then(function() {
640-
expect(plotWidth()).toBeLessThan(400);
641-
expect(plotHeight()).toBeLessThan(300);
632+
assertPlotSize({widthLessThan: 400, heightLessThan: 300}, 'moved');
642633

643634
return Plotly.relayout(gd, {'xaxis.rangeselector.visible': false});
644635
})
645636
.then(function() {
646-
expect(plotWidth()).toBe(400);
647-
expect(plotHeight()).toBe(300);
637+
assertPlotSize({width: 400, height: 300}, 'hidden');
648638

649639
return Plotly.relayout(gd, {'xaxis.rangeselector.visible': true});
650640
})
651641
.then(function() {
652-
expect(plotWidth()).toBeLessThan(400);
653-
expect(plotHeight()).toBeLessThan(300);
642+
assertPlotSize({widthLessThan: 400, heightLessThan: 300}, 'reshow');
654643
})
655644
.catch(failTest)
656645
.then(done);

test/jasmine/tests/range_slider_test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var destroyGraphDiv = require('../assets/destroy_graph_div');
1313
var mouseEvent = require('../assets/mouse_event');
1414
var supplyAllDefaults = require('../assets/supply_defaults');
1515
var failTest = require('../assets/fail_test');
16+
var assertPlotSize = require('../assets/custom_assertions').assertPlotSize;
1617

1718
var TOL = 6;
1819

@@ -363,10 +364,6 @@ describe('Rangeslider visibility property', function() {
363364

364365
afterEach(destroyGraphDiv);
365366

366-
function plotHeight() {
367-
return d3.select('.xgrid').node().getBoundingClientRect().height;
368-
}
369-
370367
function defaultLayout(opts) {
371368
return Lib.extendDeep({
372369
width: 500,
@@ -380,7 +377,7 @@ describe('Rangeslider visibility property', function() {
380377
.then(function() {
381378
var rangeSlider = getRangeSlider();
382379
expect(rangeSlider).not.toBeDefined();
383-
expect(plotHeight()).toBe(400);
380+
assertPlotSize({height: 400});
384381
})
385382
.catch(failTest)
386383
.then(done);
@@ -394,7 +391,7 @@ describe('Rangeslider visibility property', function() {
394391
.then(function() {
395392
var rangeSlider = getRangeSlider();
396393
expect(rangeSlider).toBeDefined();
397-
expect(plotHeight()).toBeLessThan(400);
394+
assertPlotSize({heightLessThan: 400});
398395
})
399396
.catch(failTest)
400397
.then(done);
@@ -409,7 +406,7 @@ describe('Rangeslider visibility property', function() {
409406
var rangeSlider = getRangeSlider();
410407
expect(rangeSlider).toBeDefined();
411408
expect(countRangeSliderClipPaths()).toEqual(1);
412-
expect(plotHeight()).toBeLessThan(400);
409+
assertPlotSize({heightLessThan: 400});
413410
})
414411
.catch(failTest)
415412
.then(done);
@@ -431,7 +428,7 @@ describe('Rangeslider visibility property', function() {
431428
var rangeSlider = getRangeSlider();
432429
expect(rangeSlider).not.toBeDefined();
433430
expect(countRangeSliderClipPaths()).toEqual(0);
434-
expect(plotHeight()).toBe(400);
431+
assertPlotSize({height: 400});
435432
})
436433
.catch(failTest)
437434
.then(done);

test/jasmine/tests/sliders_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var failTest = require('../assets/fail_test');
1010
var delay = require('../assets/delay');
11+
var assertPlotSize = require('../assets/custom_assertions').assertPlotSize;
1112

1213
describe('sliders defaults', function() {
1314
'use strict';
@@ -324,12 +325,14 @@ describe('sliders interactions', function() {
324325
it('should draw only visible sliders', function(done) {
325326
expect(gd._fullLayout._pushmargin['slider-0']).toBeDefined();
326327
expect(gd._fullLayout._pushmargin['slider-1']).toBeDefined();
328+
assertPlotSize({heightLessThan: 270}, 'initial');
327329

328330
Plotly.relayout(gd, 'sliders[0].visible', false).then(function() {
329331
assertNodeCount('.' + constants.groupClassName, 1);
330332
expect(gd._fullLayout._pushmargin['slider-0']).toBeUndefined();
331333
expect(gd._fullLayout._pushmargin['slider-1']).toBeDefined();
332334
expect(gd.layout.sliders.length).toEqual(2);
335+
assertPlotSize({heightLessThan: 270}, 'hide 0');
333336

334337
return Plotly.relayout(gd, 'sliders[1]', null);
335338
})
@@ -338,6 +341,7 @@ describe('sliders interactions', function() {
338341
expect(gd._fullLayout._pushmargin['slider-0']).toBeUndefined();
339342
expect(gd._fullLayout._pushmargin['slider-1']).toBeUndefined();
340343
expect(gd.layout.sliders.length).toEqual(1);
344+
assertPlotSize({height: 270}, 'delete 1');
341345

342346
return Plotly.relayout(gd, {
343347
'sliders[0].visible': true,
@@ -347,6 +351,7 @@ describe('sliders interactions', function() {
347351
assertNodeCount('.' + constants.groupClassName, 1);
348352
expect(gd._fullLayout._pushmargin['slider-0']).toBeDefined();
349353
expect(gd._fullLayout._pushmargin['slider-1']).toBeUndefined();
354+
assertPlotSize({heightLessThan: 270}, 'reshow 0');
350355

351356
return Plotly.relayout(gd, {
352357
'sliders[1]': {

0 commit comments

Comments
 (0)