@@ -2,6 +2,12 @@ var Drawing = require('@src/components/drawing');
22
33var d3 = require ( 'd3' ) ;
44
5+ var Plotly = require ( '@lib/index' ) ;
6+
7+ var createGraphDiv = require ( '../assets/create_graph_div' ) ;
8+ var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
9+ var fail = require ( '../assets/fail_test' ) ;
10+
511describe ( 'Drawing' , function ( ) {
612 'use strict' ;
713
@@ -309,3 +315,83 @@ describe('Drawing', function() {
309315 } ) ;
310316 } ) ;
311317} ) ;
318+
319+ describe ( 'gradients' , function ( ) {
320+ var gd ;
321+
322+ beforeEach ( function ( ) {
323+ gd = createGraphDiv ( ) ;
324+ } ) ;
325+
326+ afterEach ( destroyGraphDiv ) ;
327+
328+ function checkGradientIds ( uid , specs ) {
329+ var sortedExpected = specs . map ( function ( spec ) {
330+ return ( spec [ 0 ] + uid + '-' + spec [ 1 ] + '-' + spec [ 2 ] ) . replace ( / [ ^ \w \- ] + / g, '_' ) ;
331+ } ) . sort ( ) ;
332+
333+ var gids = [ ] ;
334+ var gradients = d3 . select ( gd ) . selectAll ( 'radialGradient,linearGradient' ) ;
335+ gradients . each ( function ( ) { gids . push ( this . id ) ; } ) ;
336+ gids . sort ( ) ;
337+
338+ expect ( gids . length ) . toBe ( sortedExpected . length ) ;
339+
340+ for ( var i = 0 ; i < Math . min ( gids . length , sortedExpected . length ) ; i ++ ) {
341+ expect ( gids [ i ] ) . toBe ( sortedExpected [ i ] ) ;
342+ }
343+ }
344+
345+ it ( 'clears unused gradients after a replot' , function ( done ) {
346+ Plotly . plot ( gd , [ {
347+ y : [ 0 , 1 , 2 ] ,
348+ mode : 'markers' ,
349+ marker : {
350+ color : '#123' ,
351+ gradient : {
352+ type : 'radial' ,
353+ color : [ '#fff' , '#eee' , '#ddd' ]
354+ }
355+ }
356+ } ] )
357+ . then ( function ( ) {
358+ checkGradientIds ( gd . _fullLayout . _uid , [
359+ [ 'radial' , '#123' , '#fff' ] ,
360+ [ 'radial' , '#123' , '#eee' ] ,
361+ [ 'radial' , '#123' , '#ddd' ]
362+ ] ) ;
363+
364+ return Plotly . restyle ( gd , { 'marker.color' : '#456' } ) ;
365+ } )
366+ . then ( function ( ) {
367+ // simple scalar restyle doesn't trigger a full replot, so
368+ // doesn't clear the old gradients
369+ checkGradientIds ( gd . _fullLayout . _uid , [
370+ [ 'radial' , '#123' , '#fff' ] ,
371+ [ 'radial' , '#123' , '#eee' ] ,
372+ [ 'radial' , '#123' , '#ddd' ] ,
373+ [ 'radial' , '#456' , '#fff' ] ,
374+ [ 'radial' , '#456' , '#eee' ] ,
375+ [ 'radial' , '#456' , '#ddd' ]
376+ ] ) ;
377+
378+ return Plotly . restyle ( gd , { 'marker.gradient.type' : [ [ 'horizontal' , 'vertical' , 'radial' ] ] } ) ;
379+ } )
380+ . then ( function ( ) {
381+ // array restyle does replot
382+ checkGradientIds ( gd . _fullLayout . _uid , [
383+ [ 'horizontal' , '#456' , '#fff' ] ,
384+ [ 'vertical' , '#456' , '#eee' ] ,
385+ [ 'radial' , '#456' , '#ddd' ]
386+ ] ) ;
387+
388+ return Plotly . restyle ( gd , { 'mode' : 'lines' } ) ;
389+ } )
390+ . then ( function ( ) {
391+ // full replot and no resulting markers at all -> no gradients
392+ checkGradientIds ( gd . _fullLayout . _uid , [ ] ) ;
393+ } )
394+ . catch ( fail )
395+ . then ( done ) ;
396+ } ) ;
397+ } ) ;
0 commit comments