1
1
var fs = require ( 'fs' ) ;
2
+ var sizeOf = require ( 'image-size' ) ;
2
3
3
4
var getMockList = require ( './assets/get_mock_list' ) ;
4
5
var getRequestOpts = require ( './assets/get_image_request_options' ) ;
@@ -9,14 +10,21 @@ var request = require('request');
9
10
var test = require ( 'tape' ) ;
10
11
11
12
// image formats to test
13
+ //
12
14
// 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
14
18
var FORMATS = [ 'svg' , 'pdf' , 'eps' ] ;
15
19
16
20
// non-exhaustive list of mocks to test
17
21
var DEFAULT_LIST = [ '0' , 'geo_first' , 'gl3d_z-range' , 'text_export' , 'layout_image' ] ;
18
22
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]
20
28
var MIN_SIZE = 100 ;
21
29
22
30
/**
@@ -69,18 +77,32 @@ function runInBatch(mockList) {
69
77
// The tests below determine whether the images are properly
70
78
// exported by (only) checking the file size of the generated images.
71
79
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 ) ,
73
88
imagePaths = getImagePaths ( mockName , format ) ,
74
89
saveImageStream = fs . createWriteStream ( imagePaths . test ) ;
75
90
76
91
function checkExport ( err ) {
77
92
if ( err ) throw err ;
78
93
79
- fs . stat ( imagePaths . test , function ( err , stats ) {
80
- var didExport = stats . size > MIN_SIZE ;
94
+ var didExport ;
81
95
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 ) ;
84
106
}
85
107
86
108
request ( requestOpts )
0 commit comments