@@ -5,7 +5,6 @@ var Plotly = require('@lib/index');
55var createGraphDiv = require ( '../assets/create_graph_div' ) ;
66var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
77var subplotMock = require ( '../../image/mocks/multiple_subplots.json' ) ;
8- var plot3dMock = require ( '../../image/mocks/gl3d_bunny.json' ) ;
98
109
1110describe ( 'Plotly.toImage' , function ( ) {
@@ -35,24 +34,28 @@ describe('Plotly.toImage', function() {
3534 returnValue . then ( done ) ;
3635 } ) ;
3736
38- it ( 'should throw error with unsupported file type' , function ( done ) {
37+ it ( 'should throw error with unsupported file type' , function ( done ) {
3938 // error should actually come in the svgToImg step
4039
4140 Plotly . plot ( gd , subplotMock . data , subplotMock . layout )
4241 . then ( function ( gd ) {
43- Plotly . toImage ( gd , { format : 'x' } ) . catch ( function ( err ) {
42+ Plotly . toImage ( gd , { format : 'x' } ) . catch ( function ( err ) {
4443 expect ( err . message ) . toEqual ( 'Image format is not jpeg, png or svg' ) ;
4544 done ( ) ;
4645 } ) ;
4746 } ) ;
4847
4948 } ) ;
5049
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 ) {
5251 // let user know that Plotly expects pixel values
5352 Plotly . plot ( gd , subplotMock . data , subplotMock . layout )
5453 . 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 ) {
5659 expect ( err . message ) . toEqual ( 'Height and width should be pixel values.' ) ;
5760 done ( ) ;
5861 } ) ;
@@ -69,16 +72,22 @@ describe('Plotly.toImage', function() {
6972 Plotly . plot ( gd , subplotMock . data , subplotMock . layout ) . then ( function ( gd ) {
7073 return Plotly . toImage ( gd ) ;
7174 } ) . 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+ } ) ;
7784 } ) . then ( function ( url ) {
7885 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+ } ;
8291 } ) ;
8392 } ) ;
8493
@@ -88,15 +97,15 @@ describe('Plotly.toImage', function() {
8897 plot . then ( function ( gd ) {
8998 return Plotly . toImage ( gd , { format : 'png' } ) ;
9099 } ) . then ( function ( url ) {
91- expect ( url . substr ( 0 , 15 ) ) . toBe ( 'data:image/png; ' ) ;
100+ expect ( url . split ( 'png' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
92101 // now do jpeg
93102 return Plotly . toImage ( gd , { format : 'jpeg' } ) ;
94103 } ) . then ( function ( url ) {
95- expect ( url . substr ( 0 , 16 ) ) . toBe ( 'data:image/jpeg; ' ) ;
104+ expect ( url . split ( 'jpeg' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
96105 // now do webp
97106 return Plotly . toImage ( gd , { format : 'webp' } ) ;
98107 } ) . then ( function ( url ) {
99- expect ( url . substr ( 0 , 16 ) ) . toBe ( 'data:image/webp; ' ) ;
108+ expect ( url . split ( 'webp' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
100109 // now do svg
101110 return Plotly . toImage ( gd , { format : 'svg' } ) ;
102111 } ) . then ( function ( url ) {
0 commit comments