@@ -5,7 +5,6 @@ var Plotly = require('@lib/index');
5
5
var createGraphDiv = require ( '../assets/create_graph_div' ) ;
6
6
var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
7
7
var subplotMock = require ( '../../image/mocks/multiple_subplots.json' ) ;
8
- var plot3dMock = require ( '../../image/mocks/gl3d_bunny.json' ) ;
9
8
10
9
11
10
describe ( 'Plotly.toImage' , function ( ) {
@@ -35,24 +34,28 @@ describe('Plotly.toImage', function() {
35
34
returnValue . then ( done ) ;
36
35
} ) ;
37
36
38
- it ( 'should throw error with unsupported file type' , function ( done ) {
37
+ it ( 'should throw error with unsupported file type' , function ( done ) {
39
38
// error should actually come in the svgToImg step
40
39
41
40
Plotly . plot ( gd , subplotMock . data , subplotMock . layout )
42
41
. then ( function ( gd ) {
43
- Plotly . toImage ( gd , { format : 'x' } ) . catch ( function ( err ) {
42
+ Plotly . toImage ( gd , { format : 'x' } ) . catch ( function ( err ) {
44
43
expect ( err . message ) . toEqual ( 'Image format is not jpeg, png or svg' ) ;
45
44
done ( ) ;
46
45
} ) ;
47
46
} ) ;
48
47
49
48
} ) ;
50
49
51
- it ( 'should throw error with height and width < 1' , function ( done ) {
50
+ it ( 'should throw error with height and/or width < 1' , function ( done ) {
52
51
// let user know that Plotly expects pixel values
53
52
Plotly . plot ( gd , subplotMock . data , subplotMock . layout )
54
53
. then ( function ( gd ) {
55
- Plotly . toImage ( gd , { height : 0.5 } ) . catch ( function ( err ) {
54
+ return Plotly . toImage ( gd , { height : 0.5 } ) . catch ( function ( err ) {
55
+ expect ( err . message ) . toEqual ( 'Height and width should be pixel values.' ) ;
56
+ } ) ;
57
+ } ) . then ( function ( ) {
58
+ Plotly . toImage ( gd , { width : 0.5 } ) . catch ( function ( err ) {
56
59
expect ( err . message ) . toEqual ( 'Height and width should be pixel values.' ) ;
57
60
done ( ) ;
58
61
} ) ;
@@ -69,16 +72,22 @@ describe('Plotly.toImage', function() {
69
72
Plotly . plot ( gd , subplotMock . data , subplotMock . layout ) . then ( function ( gd ) {
70
73
return Plotly . toImage ( gd ) ;
71
74
} ) . then ( function ( url ) {
72
- img . src = url ;
73
- expect ( img . height ) . toBe ( 600 ) ;
74
- expect ( img . width ) . toBe ( 700 ) ;
75
- // now provide height and width in opts
76
- return Plotly . toImage ( gd , { height : 400 , width : 400 } ) ;
75
+ return new Promise ( function ( resolve ) {
76
+ img . src = url ;
77
+ img . onload = function ( ) {
78
+ expect ( img . height ) . toBe ( 600 ) ;
79
+ expect ( img . width ) . toBe ( 700 ) ;
80
+ } ;
81
+ // now provide height and width in opts
82
+ resolve ( Plotly . toImage ( gd , { height : 400 , width : 400 } ) ) ;
83
+ } ) ;
77
84
} ) . then ( function ( url ) {
78
85
img . src = url ;
79
- expect ( img . height ) . toBe ( 400 ) ;
80
- expect ( img . width ) . toBe ( 400 ) ;
81
- done ( ) ;
86
+ img . onload = function ( ) {
87
+ expect ( img . height ) . toBe ( 400 ) ;
88
+ expect ( img . width ) . toBe ( 400 ) ;
89
+ done ( ) ;
90
+ } ;
82
91
} ) ;
83
92
} ) ;
84
93
@@ -88,15 +97,11 @@ describe('Plotly.toImage', function() {
88
97
plot . then ( function ( gd ) {
89
98
return Plotly . toImage ( gd , { format : 'png' } ) ;
90
99
} ) . then ( function ( url ) {
91
- expect ( url . substr ( 0 , 15 ) ) . toBe ( 'data:image/png; ' ) ;
100
+ expect ( url . split ( 'png' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
92
101
// now do jpeg
93
102
return Plotly . toImage ( gd , { format : 'jpeg' } ) ;
94
103
} ) . then ( function ( url ) {
95
- expect ( url . substr ( 0 , 16 ) ) . toBe ( 'data:image/jpeg;' ) ;
96
- // now do webp
97
- return Plotly . toImage ( gd , { format : 'webp' } ) ;
98
- } ) . then ( function ( url ) {
99
- expect ( url . substr ( 0 , 16 ) ) . toBe ( 'data:image/webp;' ) ;
104
+ expect ( url . split ( 'jpeg' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
100
105
// now do svg
101
106
return Plotly . toImage ( gd , { format : 'svg' } ) ;
102
107
} ) . then ( function ( url ) {
0 commit comments