@@ -551,24 +551,6 @@ describe('config argument', function() {
551
551
viewport . reset ( ) ;
552
552
} ) ;
553
553
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
-
572
554
function checkLayoutSize ( width , height ) {
573
555
expect ( gd . _fullLayout . width ) . toBe ( width ) ;
574
556
expect ( gd . _fullLayout . height ) . toBe ( height ) ;
@@ -578,63 +560,74 @@ describe('config argument', function() {
578
560
expect ( + svg . getAttribute ( 'height' ) ) . toBe ( height ) ;
579
561
}
580
562
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
+ }
583
574
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 )
585
578
. then ( done ) ;
586
579
} ) ;
587
580
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 )
595
585
. then ( done ) ;
596
586
} ) ;
597
587
598
588
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 )
605
592
. then ( done ) ;
606
593
} ) ;
607
594
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 ) )
610
604
. 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
+ } ) ;
613
611
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 )
615
616
. then ( done ) ;
616
617
} ) ;
617
618
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 ) {
619
620
Plotly . plot ( gd , data , { } , { responsive : true } )
620
621
// Check initial size
621
- . then ( function ( ) {
622
- checkLayoutSize ( startWidth , startHeight ) ;
623
- } )
622
+ . then ( function ( ) { checkLayoutSize ( startWidth , startHeight ) ; } )
624
623
// Turn off responsiveness
625
- . then ( function ( ) {
626
- Plotly . react ( gd , data , { } , { responsive : false } ) ;
627
- } )
624
+ . then ( function ( ) { return Plotly . react ( gd , data , { } , { responsive : false } ) ; } )
628
625
// Resize viewport
629
- . then ( function ( ) {
630
- viewport . set ( newWidth , newHeight ) ;
631
- } )
626
+ . then ( function ( ) { viewport . set ( newWidth , newHeight ) ; } )
632
627
// Wait for resize to happen (Plotly.resize has an internal timeout)
633
628
. 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 ) ; } )
638
631
. catch ( failTest )
639
632
. then ( done ) ;
640
633
} ) ;
0 commit comments