1
- // move toimage to plot_api_test.js
2
- // once established and confirmed?
3
-
4
- var Plotly = require ( '@lib/index' ) ;
1
+ var Plotly = require ( '@lib' ) ;
2
+ var Lib = require ( '@src/lib' ) ;
5
3
6
4
var d3 = require ( 'd3' ) ;
7
5
var createGraphDiv = require ( '../assets/create_graph_div' ) ;
6
+ var fail = require ( '../assets/fail_test' ) ;
8
7
var subplotMock = require ( '@mocks/multiple_subplots.json' ) ;
9
8
10
9
@@ -25,6 +24,15 @@ describe('Plotly.toImage', function() {
25
24
d3 . selectAll ( '#graph' ) . remove ( ) ;
26
25
} ) ;
27
26
27
+ function createImage ( url ) {
28
+ return new Promise ( function ( resolve , reject ) {
29
+ var img = document . createElement ( 'img' ) ;
30
+ img . src = url ;
31
+ img . onload = function ( ) { return resolve ( img ) ; } ;
32
+ img . onerror = function ( ) { return reject ( 'error during createImage' ) ; } ;
33
+ } ) ;
34
+ }
35
+
28
36
it ( 'should be attached to Plotly' , function ( ) {
29
37
expect ( Plotly . toImage ) . toBeDefined ( ) ;
30
38
} ) ;
@@ -71,52 +79,54 @@ describe('Plotly.toImage', function() {
71
79
} ) ;
72
80
73
81
it ( 'should create img with proper height and width' , function ( done ) {
74
- var img = document . createElement ( 'img' ) ;
82
+ var fig = Lib . extendDeep ( { } , subplotMock ) ;
75
83
76
84
// specify height and width
77
- subplotMock . layout . height = 600 ;
78
- subplotMock . layout . width = 700 ;
85
+ fig . layout . height = 600 ;
86
+ fig . layout . width = 700 ;
79
87
80
- Plotly . plot ( gd , subplotMock . data , subplotMock . layout ) . then ( function ( gd ) {
88
+ Plotly . plot ( gd , fig . data , fig . layout ) . then ( function ( gd ) {
81
89
expect ( gd . layout . height ) . toBe ( 600 ) ;
82
90
expect ( gd . layout . width ) . toBe ( 700 ) ;
83
91
return Plotly . toImage ( gd ) ;
84
- } ) . then ( function ( url ) {
85
- return new Promise ( function ( resolve ) {
86
- img . src = url ;
87
- img . onload = function ( ) {
88
- expect ( img . height ) . toBe ( 600 ) ;
89
- expect ( img . width ) . toBe ( 700 ) ;
90
- } ;
91
- // now provide height and width in opts
92
- resolve ( Plotly . toImage ( gd , { height : 400 , width : 400 } ) ) ;
93
- } ) ;
94
- } ) . then ( function ( url ) {
95
- img . src = url ;
96
- img . onload = function ( ) {
97
- expect ( img . height ) . toBe ( 400 ) ;
98
- expect ( img . width ) . toBe ( 400 ) ;
99
- done ( ) ;
100
- } ;
101
- } ) ;
92
+ } )
93
+ . then ( createImage )
94
+ . then ( function ( img ) {
95
+ expect ( img . height ) . toBe ( 600 ) ;
96
+ expect ( img . width ) . toBe ( 700 ) ;
97
+
98
+ return Plotly . toImage ( gd , { height : 400 , width : 400 } ) ;
99
+ } )
100
+ . then ( createImage )
101
+ . then ( function ( img ) {
102
+ expect ( img . height ) . toBe ( 400 ) ;
103
+ expect ( img . width ) . toBe ( 400 ) ;
104
+ } )
105
+ . catch ( fail )
106
+ . then ( done ) ;
102
107
} ) ;
103
108
104
109
it ( 'should create proper file type' , function ( done ) {
105
- var plot = Plotly . plot ( gd , subplotMock . data , subplotMock . layout ) ;
110
+ var fig = Lib . extendDeep ( { } , subplotMock ) ;
106
111
107
- plot . then ( function ( gd ) {
108
- return Plotly . toImage ( gd , { format : 'png' } ) ;
109
- } ) . then ( function ( url ) {
112
+ Plotly . plot ( gd , fig . data , fig . layout )
113
+ . then ( function ( ) { return Plotly . toImage ( gd , { format : 'png' } ) ; } )
114
+ . then ( function ( url ) {
110
115
expect ( url . split ( 'png' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
111
- // now do jpeg
112
- return Plotly . toImage ( gd , { format : 'jpeg' } ) ;
113
- } ) . then ( function ( url ) {
116
+ } )
117
+ . then ( function ( ) { return Plotly . toImage ( gd , { format : 'jpeg' } ) ; } )
118
+ . then ( function ( url ) {
114
119
expect ( url . split ( 'jpeg' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
115
- // now do svg
116
- return Plotly . toImage ( gd , { format : 'svg' } ) ;
117
- } ) . then ( function ( url ) {
120
+ } )
121
+ . then ( function ( ) { return Plotly . toImage ( gd , { format : 'svg' } ) ; } )
122
+ . then ( function ( url ) {
118
123
expect ( url . split ( 'svg' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
119
- done ( ) ;
120
- } ) ;
124
+ } )
125
+ . then ( function ( ) { return Plotly . toImage ( gd , { format : 'webp' } ) ; } )
126
+ . then ( function ( url ) {
127
+ expect ( url . split ( 'webp' ) [ 0 ] ) . toBe ( 'data:image/' ) ;
128
+ } )
129
+ . catch ( fail )
130
+ . then ( done ) ;
121
131
} ) ;
122
132
} ) ;
0 commit comments