@@ -2,36 +2,37 @@ var Plotly = require('@lib/index');
2
2
var createGraphDiv = require ( '../assets/create_graph_div' ) ;
3
3
var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
4
4
var textchartMock = require ( '@mocks/text_chart_arrays.json' ) ;
5
+ var fail = require ( '../assets/fail_test' ) ;
6
+
7
+ var Lib = require ( '@src/lib' ) ;
5
8
6
9
var LONG_TIMEOUT_INTERVAL = 2 * jasmine . DEFAULT_TIMEOUT_INTERVAL ;
7
10
8
11
describe ( 'Plotly.downloadImage' , function ( ) {
9
12
'use strict' ;
10
13
var gd ;
11
14
12
- // override click handler on createElement
13
- // so these tests will not actually
14
- // download an image each time they are run
15
- // full credit goes to @etpinard ; thanks
16
15
var createElement = document . createElement ;
17
- beforeAll ( function ( ) {
18
- document . createElement = function ( args ) {
19
- var el = createElement . call ( document , args ) ;
20
- el . click = function ( ) { } ;
21
- return el ;
22
- } ;
23
- } ) ;
24
-
25
- afterAll ( function ( ) {
26
- document . createElement = createElement ;
27
- } ) ;
16
+ var slzProto = ( new window . XMLSerializer ( ) ) . __proto__ ;
17
+ var serializeToString = slzProto . serializeToString ;
28
18
29
19
beforeEach ( function ( ) {
30
20
gd = createGraphDiv ( ) ;
21
+
22
+ // override click handler on createElement
23
+ // so these tests will not actually
24
+ // download an image each time they are run
25
+ // full credit goes to @etpinard ; thanks
26
+ spyOn ( document , 'createElement' ) . and . callFake ( function ( args ) {
27
+ var el = createElement . call ( document , args ) ;
28
+ el . click = function ( ) { } ;
29
+ return el ;
30
+ } ) ;
31
31
} ) ;
32
32
33
33
afterEach ( function ( ) {
34
34
destroyGraphDiv ( ) ;
35
+ delete navigator . msSaveBlob ;
35
36
} ) ;
36
37
37
38
it ( 'should be attached to Plotly' , function ( ) {
@@ -59,8 +60,56 @@ describe('Plotly.downloadImage', function() {
59
60
it ( 'should create link, remove link, accept options' , function ( done ) {
60
61
downloadTest ( gd , 'svg' , done ) ;
61
62
} , LONG_TIMEOUT_INTERVAL ) ;
62
- } ) ;
63
63
64
+ it ( 'should produce the right SVG output in IE' , function ( done ) {
65
+ // mock up IE behavior
66
+ spyOn ( Lib , 'isIE' ) . and . callFake ( function ( ) { return true ; } ) ;
67
+ spyOn ( slzProto , 'serializeToString' ) . and . callFake ( function ( ) {
68
+ return serializeToString . apply ( this , arguments )
69
+ . replace ( / ( \( # ) ( [ ^ " ) ] * ) ( \) ) / gi, '(\"#$2\")' ) ;
70
+ } ) ;
71
+ var savedBlob ;
72
+ navigator . msSaveBlob = function ( blob ) { savedBlob = blob ; } ;
73
+
74
+ var expectedStart = '<svg class=\'main-svg\' xmlns=\'http://www.w3.org/2000/svg\' xmlns:xlink=\'http://www.w3.org/1999/xlink\'' ;
75
+ var plotClip = / c l i p - p a t h = ' u r l \( " # c l i p [ 0 - 9 a - f ] { 6 } x y p l o t " \) / ;
76
+ var legendClip = / c l i p - p a t h = \' u r l \( " # l e g e n d [ 0 - 9 a - f ] { 6 } " \) / ;
77
+
78
+ Plotly . plot ( gd , textchartMock . data , textchartMock . layout )
79
+ . then ( function ( gd ) {
80
+ savedBlob = undefined ;
81
+ return Plotly . downloadImage ( gd , {
82
+ format : 'svg' ,
83
+ height : 300 ,
84
+ width : 300 ,
85
+ filename : 'plotly_download'
86
+ } ) ;
87
+ } )
88
+ . then ( function ( ) {
89
+ if ( savedBlob === undefined ) {
90
+ fail ( 'undefined saveBlob' ) ;
91
+ }
92
+
93
+ return new Promise ( function ( resolve , reject ) {
94
+ var reader = new FileReader ( ) ;
95
+ reader . onloadend = function ( ) {
96
+ var res = reader . result ;
97
+
98
+ expect ( res . substr ( 0 , expectedStart . length ) ) . toBe ( expectedStart ) ;
99
+ expect ( res . match ( plotClip ) ) . not . toBe ( null ) ;
100
+ expect ( res . match ( legendClip ) ) . not . toBe ( null ) ;
101
+
102
+ resolve ( ) ;
103
+ } ;
104
+ reader . onerror = function ( e ) { reject ( e ) ; } ;
105
+
106
+ reader . readAsText ( savedBlob ) ;
107
+ } ) ;
108
+ } )
109
+ . catch ( fail )
110
+ . then ( done ) ;
111
+ } , LONG_TIMEOUT_INTERVAL ) ;
112
+ } ) ;
64
113
65
114
function downloadTest ( gd , format , done ) {
66
115
// use MutationObserver to monitor the DOM
0 commit comments