diff --git a/package.json b/package.json index 199d5c48f90..8456f224cf5 100644 --- a/package.json +++ b/package.json @@ -100,6 +100,7 @@ "fuse.js": "^2.2.0", "glob": "^7.0.0", "gzip-size": "^3.0.0", + "image-size": "^0.5.0", "jasmine-core": "^2.4.1", "karma": "^1.1.0", "karma-browserify": "^5.0.1", diff --git a/test/image/assets/get_image_request_options.js b/test/image/assets/get_image_request_options.js index 02b1f84347f..82589f76d16 100644 --- a/test/image/assets/get_image_request_options.js +++ b/test/image/assets/get_image_request_options.js @@ -23,6 +23,9 @@ module.exports = function getRequestOpts(specs) { scale: specs.scale || DEFAULT_SCALE }; + if(specs.width) body.width = specs.width; + if(specs.height) body.height = specs.height; + return { method: 'POST', url: constants.testContainerUrl, diff --git a/test/image/export_test.js b/test/image/export_test.js index 6d0e7d2557f..a29106da5de 100644 --- a/test/image/export_test.js +++ b/test/image/export_test.js @@ -1,4 +1,5 @@ var fs = require('fs'); +var sizeOf = require('image-size'); var getMockList = require('./assets/get_mock_list'); var getRequestOpts = require('./assets/get_image_request_options'); @@ -9,14 +10,21 @@ var request = require('request'); var test = require('tape'); // image formats to test +// // N.B. 'png' is tested in `npm run test-image, no need to duplicate here -// TODO figure why 'jpeg' and 'webp' lead to errors +// +// N.B. 'jpeg' and 'webp' lead to errors because of the image server code +// is looking for Plotly.Color which isn't exposed anymore var FORMATS = ['svg', 'pdf', 'eps']; // non-exhaustive list of mocks to test var DEFAULT_LIST = ['0', 'geo_first', 'gl3d_z-range', 'text_export', 'layout_image']; -// minimum satisfactory file size +// return dimensions [in px] +var WIDTH = 700; +var HEIGHT = 500; + +// minimum satisfactory file size [in bytes] var MIN_SIZE = 100; /** @@ -69,18 +77,32 @@ function runInBatch(mockList) { // The tests below determine whether the images are properly // exported by (only) checking the file size of the generated images. function testExport(mockName, format, t) { - var requestOpts = getRequestOpts({ mockName: mockName, format: format }), + var specs = { + mockName: mockName, + format: format, + width: WIDTH, + height: HEIGHT + }; + + var requestOpts = getRequestOpts(specs), imagePaths = getImagePaths(mockName, format), saveImageStream = fs.createWriteStream(imagePaths.test); function checkExport(err) { if(err) throw err; - fs.stat(imagePaths.test, function(err, stats) { - var didExport = stats.size > MIN_SIZE; + var didExport; - t.ok(didExport, mockName + ' should be properly exported as a ' + format); - }); + if(format === 'svg') { + var dims = sizeOf(imagePaths.test); + didExport = (dims.width === WIDTH) && (dims.height === HEIGHT); + } + else { + var stats = fs.statSync(imagePaths.test); + didExport = stats.size > MIN_SIZE; + } + + t.ok(didExport, mockName + ' should be properly exported as a ' + format); } request(requestOpts)