@@ -551,24 +551,6 @@ describe('config argument', function() {
551551 viewport . reset ( ) ;
552552 } ) ;
553553
554- function testResponsive ( promise ) {
555- return promise
556- . then ( function ( ) {
557- checkLayoutSize ( startWidth , startHeight ) ;
558- } )
559- // Resize viewport
560- . then ( function ( ) {
561- viewport . set ( newWidth , newHeight ) ;
562- } )
563- // Wait for resize to happen (Plotly.resize has an internal timeout)
564- . then ( delay ( 200 ) )
565- // Check final figure size
566- . then ( function ( ) {
567- checkLayoutSize ( newWidth , newHeight ) ;
568- } )
569- . catch ( failTest ) ;
570- }
571-
572554 function checkLayoutSize ( width , height ) {
573555 expect ( gd . _fullLayout . width ) . toBe ( width ) ;
574556 expect ( gd . _fullLayout . height ) . toBe ( height ) ;
@@ -578,63 +560,74 @@ describe('config argument', function() {
578560 expect ( + svg . getAttribute ( 'height' ) ) . toBe ( height ) ;
579561 }
580562
581- it ( 'should resize when the viewport width/height changes' , function ( done ) {
582- var promise = Plotly . plot ( gd , data , { } , { responsive : true } ) ;
563+ function testResponsive ( ) {
564+ checkLayoutSize ( startWidth , startHeight ) ;
565+ viewport . set ( newWidth , newHeight ) ;
566+
567+ return Promise . resolve ( )
568+ . then ( delay ( 200 ) )
569+ . then ( function ( ) {
570+ checkLayoutSize ( newWidth , newHeight ) ;
571+ } )
572+ . catch ( failTest ) ;
573+ }
583574
584- testResponsive ( promise )
575+ it ( 'should resize when the viewport width/height changes' , function ( done ) {
576+ Plotly . plot ( gd , data , { } , { responsive : true } )
577+ . then ( testResponsive )
585578 . then ( done ) ;
586579 } ) ;
587580
588- it ( 'should still be responsive if the plot is redrawn' , function ( done ) {
589- var promise = Plotly . plot ( gd , data , { } , { responsive : true } )
590- . then ( function ( ) {
591- Plotly . restyle ( gd , 'y[0]' , data [ 0 ] . y [ 0 ] * 2 ) ;
592- } ) ;
593-
594- testResponsive ( promise )
581+ it ( 'should still be responsive if the plot is edited' , function ( done ) {
582+ Plotly . plot ( gd , data , { } , { responsive : true } )
583+ . then ( function ( ) { return Plotly . restyle ( gd , 'y[0]' , data [ 0 ] . y [ 0 ] + 2 ) ; } )
584+ . then ( testResponsive )
595585 . then ( done ) ;
596586 } ) ;
597587
598588 it ( 'should still be responsive if the plot is purged and replotted' , function ( done ) {
599- var promise = Plotly . plot ( gd , data , { } , { responsive : true } )
600- . then ( function ( ) {
601- Plotly . newPlot ( gd , data , { } , { responsive : true } ) ;
602- } ) ;
603-
604- testResponsive ( promise )
589+ Plotly . plot ( gd , data , { } , { responsive : true } )
590+ . then ( function ( ) { return Plotly . newPlot ( gd , data , { } , { responsive : true } ) ; } )
591+ . then ( testResponsive )
605592 . then ( done ) ;
606593 } ) ;
607594
608- it ( 'should become responsive if configured to be so by Plotly.react' , function ( done ) {
609- var promise = Plotly . plot ( gd , data , { } , { responsive : false } )
595+ it ( 'should only have one resize handler when plotted more than once' , function ( done ) {
596+ var cntWindowResize = 0 ;
597+ window . addEventListener ( 'resize' , function ( ) { cntWindowResize ++ ; } ) ;
598+ spyOn ( Plotly . Plots , 'resize' ) . and . callThrough ( ) ;
599+
600+ Plotly . plot ( gd , data , { } , { responsive : true } )
601+ . then ( function ( ) { return Plotly . restyle ( gd , 'y[0]' , data [ 0 ] . y [ 0 ] + 2 ) ; } )
602+ . then ( function ( ) { viewport . set ( newWidth , newHeight ) ; } )
603+ . then ( delay ( 200 ) )
610604 . then ( function ( ) {
611- Plotly . react ( gd , data , { } , { responsive : true } ) ;
612- } ) ;
605+ expect ( cntWindowResize ) . toBe ( 1 ) ;
606+ expect ( Plotly . Plots . resize . calls . count ( ) ) . toBe ( 4 ) ;
607+ } )
608+ . catch ( failTest )
609+ . then ( done ) ;
610+ } ) ;
613611
614- testResponsive ( promise )
612+ it ( 'should become responsive if configured as such via Plotly.react' , function ( done ) {
613+ Plotly . plot ( gd , data , { } , { responsive : false } )
614+ . then ( function ( ) { return Plotly . react ( gd , data , { } , { responsive : true } ) ; } )
615+ . then ( testResponsive )
615616 . then ( done ) ;
616617 } ) ;
617618
618- it ( 'should stop being responsive if configured to be so by Plotly.react' , function ( done ) {
619+ it ( 'should stop being responsive if configured as such via Plotly.react' , function ( done ) {
619620 Plotly . plot ( gd , data , { } , { responsive : true } )
620621 // Check initial size
621- . then ( function ( ) {
622- checkLayoutSize ( startWidth , startHeight ) ;
623- } )
622+ . then ( function ( ) { checkLayoutSize ( startWidth , startHeight ) ; } )
624623 // Turn off responsiveness
625- . then ( function ( ) {
626- Plotly . react ( gd , data , { } , { responsive : false } ) ;
627- } )
624+ . then ( function ( ) { return Plotly . react ( gd , data , { } , { responsive : false } ) ; } )
628625 // Resize viewport
629- . then ( function ( ) {
630- viewport . set ( newWidth , newHeight ) ;
631- } )
626+ . then ( function ( ) { viewport . set ( newWidth , newHeight ) ; } )
632627 // Wait for resize to happen (Plotly.resize has an internal timeout)
633628 . then ( delay ( 200 ) )
634- // Check final figure size hasn't changed
635- . then ( function ( ) {
636- checkLayoutSize ( startWidth , startHeight ) ;
637- } )
629+ // Check that final figure's size hasn't changed
630+ . then ( function ( ) { checkLayoutSize ( startWidth , startHeight ) ; } )
638631 . catch ( failTest )
639632 . then ( done ) ;
640633 } ) ;
0 commit comments