@@ -2,36 +2,37 @@ var Plotly = require('@lib/index');
22var createGraphDiv = require ( '../assets/create_graph_div' ) ;
33var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
44var textchartMock = require ( '@mocks/text_chart_arrays.json' ) ;
5+ var fail = require ( '../assets/fail_test' ) ;
6+
7+ var Lib = require ( '@src/lib' ) ;
58
69var LONG_TIMEOUT_INTERVAL = 2 * jasmine . DEFAULT_TIMEOUT_INTERVAL ;
710
811describe ( 'Plotly.downloadImage' , function ( ) {
912 'use strict' ;
1013 var gd ;
1114
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
1615 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 ;
2818
2919 beforeEach ( function ( ) {
3020 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+ } ) ;
3131 } ) ;
3232
3333 afterEach ( function ( ) {
3434 destroyGraphDiv ( ) ;
35+ delete navigator . msSaveBlob ;
3536 } ) ;
3637
3738 it ( 'should be attached to Plotly' , function ( ) {
@@ -59,8 +60,56 @@ describe('Plotly.downloadImage', function() {
5960 it ( 'should create link, remove link, accept options' , function ( done ) {
6061 downloadTest ( gd , 'svg' , done ) ;
6162 } , LONG_TIMEOUT_INTERVAL ) ;
62- } ) ;
6363
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+ } ) ;
64113
65114function downloadTest ( gd , format , done ) {
66115 // use MutationObserver to monitor the DOM
0 commit comments