@@ -44,45 +44,84 @@ describe('Plotly.downloadImage', function() {
44
44
} ) ;
45
45
46
46
it ( 'should create link, remove link, accept options' , function ( done ) {
47
- //use MutationObserver to monitor the DOM
48
- //for changes
49
- //code modeled after
50
- //https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
51
- // select the target node
52
- var target = document . body ;
53
- var domchanges = [ ] ;
54
-
55
- // create an observer instance
56
- var observer = new MutationObserver ( function ( mutations ) {
57
- mutations . forEach ( function ( mutation ) {
58
- domchanges . push ( mutation ) ;
59
- } ) ;
60
- } ) ;
47
+ downloadTest ( gd , 'jpeg' , done ) ;
48
+ } ) ;
61
49
62
- Plotly . plot ( gd , textchartMock . data , textchartMock . layout ) . then ( function ( gd ) {
63
- // start observing dom
64
- // configuration of the observer:
65
- var config = { childList : true } ;
66
-
67
- // pass in the target node and observer options
68
- observer . observe ( target , config ) ;
69
-
70
- return Plotly . downloadImage ( gd , { format : 'jpeg' , height : 300 , width : 300 , filename : 'plotly_download' } ) ;
71
- } ) . then ( function ( filename ) {
72
- // stop observing
73
- observer . disconnect ( ) ;
74
- // look for an added and removed link
75
- var linkadded = domchanges [ domchanges . length - 2 ] . addedNodes [ 0 ] . outerHTML ;
76
- var linkdeleted = domchanges [ domchanges . length - 1 ] . removedNodes [ 0 ] . outerHTML ;
77
-
78
- // check for a <a element and proper file type
79
- expect ( linkadded . split ( 'href="' ) [ 1 ] . split ( 'jpeg;' ) [ 0 ] ) . toEqual ( 'data:image/' ) ;
80
- // check that filename option handled properly
81
- expect ( filename ) . toBe ( 'plotly_download.jpeg' ) ;
82
-
83
- // check that link removed
84
- expect ( linkadded ) . toBe ( linkdeleted ) ;
85
- done ( ) ;
50
+ it ( 'should create link, remove link, accept options' , function ( done ) {
51
+ downloadTest ( gd , 'png' , done ) ;
52
+ } ) ;
53
+
54
+ it ( 'should create link, remove link, accept options' , function ( done ) {
55
+ checkWebp ( function ( supported ) {
56
+ if ( supported ) {
57
+ downloadTest ( gd , 'webp' , done ) ;
58
+ } else {
59
+ done ( ) ;
60
+ }
86
61
} ) ;
62
+
63
+ } ) ;
64
+
65
+ it ( 'should create link, remove link, accept options' , function ( done ) {
66
+ downloadTest ( gd , 'svg' , done ) ;
87
67
} ) ;
88
68
} ) ;
69
+
70
+
71
+ function downloadTest ( gd , format , done ) {
72
+ //use MutationObserver to monitor the DOM
73
+ //for changes
74
+ //code modeled after
75
+ //https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
76
+ // select the target node
77
+ var target = document . body ;
78
+ var domchanges = [ ] ;
79
+
80
+ // create an observer instance
81
+ var observer = new MutationObserver ( function ( mutations ) {
82
+ mutations . forEach ( function ( mutation ) {
83
+ domchanges . push ( mutation ) ;
84
+ } ) ;
85
+ } ) ;
86
+
87
+ Plotly . plot ( gd , textchartMock . data , textchartMock . layout ) . then ( function ( gd ) {
88
+ // start observing dom
89
+ // configuration of the observer:
90
+ var config = { childList : true } ;
91
+
92
+ // pass in the target node and observer options
93
+ observer . observe ( target , config ) ;
94
+
95
+ return Plotly . downloadImage ( gd , { format : format , height : 300 , width : 300 , filename : 'plotly_download' } ) ;
96
+ } ) . then ( function ( filename ) {
97
+ // stop observing
98
+ observer . disconnect ( ) ;
99
+ // look for an added and removed link
100
+ var linkadded = domchanges [ domchanges . length - 2 ] . addedNodes [ 0 ] ;
101
+ var linkdeleted = domchanges [ domchanges . length - 1 ] . removedNodes [ 0 ] ;
102
+
103
+ // check for a <a element and proper file type
104
+ expect ( linkadded . getAttribute ( 'href' ) . split ( format ) [ 0 ] ) . toEqual ( 'data:image/' ) ;
105
+ // check that filename option handled properly
106
+ expect ( filename ) . toEqual ( 'plotly_download.' + format ) ;
107
+
108
+ // check that link removed
109
+ expect ( linkadded ) . toBe ( linkdeleted ) ;
110
+ done ( ) ;
111
+ } ) ;
112
+ }
113
+
114
+
115
+ // Only chrome supports webp at the time of writing
116
+ function checkWebp ( cb ) {
117
+ var img = new Image ( ) ;
118
+ img . onload = function ( ) {
119
+ cb ( true ) ;
120
+ } ;
121
+
122
+ img . onerror = function ( ) {
123
+ cb ( false ) ;
124
+ } ;
125
+
126
+ img . src = 'data:image/webp;base64,UklGRjoAAABXRUJQVlA4IC4AAACyAgCdASoCAAIALmk0mk0iIiIiIgBoSygABc6WWgAA/veff/0PP8bA//LwYAAA' ;
127
+ }
0 commit comments