@@ -7,6 +7,8 @@ var fail = require('../assets/fail_test');
7
7
var customMatchers = require ( '../assets/custom_matchers' ) ;
8
8
var subplotMock = require ( '@mocks/multiple_subplots.json' ) ;
9
9
10
+ var FORMATS = [ 'png' , 'jpeg' , 'webp' , 'svg' ] ;
11
+
10
12
describe ( 'Plotly.toImage' , function ( ) {
11
13
'use strict' ;
12
14
@@ -31,6 +33,19 @@ describe('Plotly.toImage', function() {
31
33
} ) ;
32
34
}
33
35
36
+ function assertSize ( url , width , height ) {
37
+ return new Promise ( function ( resolve , reject ) {
38
+ var img = new Image ( ) ;
39
+ img . onload = function ( ) {
40
+ expect ( img . width ) . toBe ( width , 'image width' ) ;
41
+ expect ( img . height ) . toBe ( height , 'image height' ) ;
42
+ resolve ( url ) ;
43
+ } ;
44
+ img . onerror = reject ;
45
+ img . src = url ;
46
+ } ) ;
47
+ }
48
+
34
49
it ( 'should be attached to Plotly' , function ( ) {
35
50
expect ( Plotly . toImage ) . toBeDefined ( ) ;
36
51
} ) ;
@@ -109,18 +124,22 @@ describe('Plotly.toImage', function() {
109
124
110
125
Plotly . plot ( gd , fig . data , fig . layout )
111
126
. then ( function ( ) { return Plotly . toImage ( gd , { format : 'png' } ) ; } )
127
+ . then ( function ( url ) { return assertSize ( url , 700 , 450 ) ; } )
112
128
. then ( function ( url ) {
113
129
expect ( url . split ( 'png' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
114
130
} )
115
131
. then ( function ( ) { return Plotly . toImage ( gd , { format : 'jpeg' } ) ; } )
132
+ . then ( function ( url ) { return assertSize ( url , 700 , 450 ) ; } )
116
133
. then ( function ( url ) {
117
134
expect ( url . split ( 'jpeg' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
118
135
} )
119
136
. then ( function ( ) { return Plotly . toImage ( gd , { format : 'svg' } ) ; } )
137
+ . then ( function ( url ) { return assertSize ( url , 700 , 450 ) ; } )
120
138
. then ( function ( url ) {
121
139
expect ( url . split ( 'svg' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
122
140
} )
123
141
. then ( function ( ) { return Plotly . toImage ( gd , { format : 'webp' } ) ; } )
142
+ . then ( function ( url ) { return assertSize ( url , 700 , 450 ) ; } )
124
143
. then ( function ( url ) {
125
144
expect ( url . split ( 'webp' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
126
145
} )
@@ -156,6 +175,20 @@ describe('Plotly.toImage', function() {
156
175
. then ( done ) ;
157
176
} ) ;
158
177
178
+ FORMATS . forEach ( function ( f ) {
179
+ it ( 'should respond to *scale* option ( format ' + f + ')' , function ( done ) {
180
+ var fig = Lib . extendDeep ( { } , subplotMock ) ;
181
+
182
+ Plotly . plot ( gd , fig . data , fig . layout )
183
+ . then ( function ( ) { return Plotly . toImage ( gd , { format : f , scale : 2 } ) ; } )
184
+ . then ( function ( url ) { return assertSize ( url , 1400 , 900 ) ; } )
185
+ . then ( function ( ) { return Plotly . toImage ( gd , { format : f , scale : 0.5 } ) ; } )
186
+ . then ( function ( url ) { return assertSize ( url , 350 , 225 ) ; } )
187
+ . catch ( fail )
188
+ . then ( done ) ;
189
+ } ) ;
190
+ } ) ;
191
+
159
192
it ( 'should accept data/layout/config figure object as input' , function ( done ) {
160
193
var fig = Lib . extendDeep ( { } , subplotMock ) ;
161
194
0 commit comments