Skip to content

Commit 9b249c2

Browse files
authored
Merge pull request #944 from plotly/image-size-test
Test SVG image size in test-export
2 parents fa07513 + 1e61ed6 commit 9b249c2

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"fuse.js": "^2.2.0",
101101
"glob": "^7.0.0",
102102
"gzip-size": "^3.0.0",
103+
"image-size": "^0.5.0",
103104
"jasmine-core": "^2.4.1",
104105
"karma": "^1.1.0",
105106
"karma-browserify": "^5.0.1",

test/image/assets/get_image_request_options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module.exports = function getRequestOpts(specs) {
2323
scale: specs.scale || DEFAULT_SCALE
2424
};
2525

26+
if(specs.width) body.width = specs.width;
27+
if(specs.height) body.height = specs.height;
28+
2629
return {
2730
method: 'POST',
2831
url: constants.testContainerUrl,

test/image/export_test.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var fs = require('fs');
2+
var sizeOf = require('image-size');
23

34
var getMockList = require('./assets/get_mock_list');
45
var getRequestOpts = require('./assets/get_image_request_options');
@@ -9,14 +10,21 @@ var request = require('request');
910
var test = require('tape');
1011

1112
// image formats to test
13+
//
1214
// N.B. 'png' is tested in `npm run test-image, no need to duplicate here
13-
// TODO figure why 'jpeg' and 'webp' lead to errors
15+
//
16+
// N.B. 'jpeg' and 'webp' lead to errors because of the image server code
17+
// is looking for Plotly.Color which isn't exposed anymore
1418
var FORMATS = ['svg', 'pdf', 'eps'];
1519

1620
// non-exhaustive list of mocks to test
1721
var DEFAULT_LIST = ['0', 'geo_first', 'gl3d_z-range', 'text_export', 'layout_image'];
1822

19-
// minimum satisfactory file size
23+
// return dimensions [in px]
24+
var WIDTH = 700;
25+
var HEIGHT = 500;
26+
27+
// minimum satisfactory file size [in bytes]
2028
var MIN_SIZE = 100;
2129

2230
/**
@@ -69,18 +77,32 @@ function runInBatch(mockList) {
6977
// The tests below determine whether the images are properly
7078
// exported by (only) checking the file size of the generated images.
7179
function testExport(mockName, format, t) {
72-
var requestOpts = getRequestOpts({ mockName: mockName, format: format }),
80+
var specs = {
81+
mockName: mockName,
82+
format: format,
83+
width: WIDTH,
84+
height: HEIGHT
85+
};
86+
87+
var requestOpts = getRequestOpts(specs),
7388
imagePaths = getImagePaths(mockName, format),
7489
saveImageStream = fs.createWriteStream(imagePaths.test);
7590

7691
function checkExport(err) {
7792
if(err) throw err;
7893

79-
fs.stat(imagePaths.test, function(err, stats) {
80-
var didExport = stats.size > MIN_SIZE;
94+
var didExport;
8195

82-
t.ok(didExport, mockName + ' should be properly exported as a ' + format);
83-
});
96+
if(format === 'svg') {
97+
var dims = sizeOf(imagePaths.test);
98+
didExport = (dims.width === WIDTH) && (dims.height === HEIGHT);
99+
}
100+
else {
101+
var stats = fs.statSync(imagePaths.test);
102+
didExport = stats.size > MIN_SIZE;
103+
}
104+
105+
t.ok(didExport, mockName + ' should be properly exported as a ' + format);
84106
}
85107

86108
request(requestOpts)

0 commit comments

Comments
 (0)