@@ -6,6 +6,7 @@ var destroyGraphDiv = require('../assets/destroy_graph_div');
6
6
var click = require ( '../assets/click' ) ;
7
7
var mouseEvent = require ( '../assets/mouse_event' ) ;
8
8
var failTest = require ( '../assets/fail_test' ) ;
9
+ var delay = require ( '../assets/delay' ) ;
9
10
10
11
describe ( 'config argument' , function ( ) {
11
12
@@ -529,4 +530,108 @@ describe('config argument', function() {
529
530
} ) ;
530
531
} ) ;
531
532
} ) ;
533
+
534
+ describe ( 'responsive figure' , function ( ) {
535
+ var gd ;
536
+ var startWidth = 960 , startHeight = 400 ;
537
+ var newWidth = 400 , newHeight = 700 ;
538
+ var data = [ { x : [ 1 , 2 , 3 , 4 ] , y : [ 5 , 10 , 2 , 8 ] } ] ;
539
+
540
+ beforeEach ( function ( ) {
541
+ viewport . set ( startWidth , startHeight ) ;
542
+ gd = createGraphDiv ( ) ;
543
+
544
+ // Make the graph fill the parent
545
+ gd . style . width = '100%' ;
546
+ gd . style . height = '100%' ;
547
+ } ) ;
548
+
549
+ afterEach ( function ( ) {
550
+ Plotly . purge ( gd ) ; // Needed to remove all event listeners
551
+ destroyGraphDiv ( ) ;
552
+ viewport . reset ( ) ;
553
+ } ) ;
554
+
555
+ function checkLayoutSize ( width , height ) {
556
+ expect ( gd . _fullLayout . width ) . toBe ( width ) ;
557
+ expect ( gd . _fullLayout . height ) . toBe ( height ) ;
558
+
559
+ var svg = document . getElementsByClassName ( 'main-svg' ) [ 0 ] ;
560
+ expect ( + svg . getAttribute ( 'width' ) ) . toBe ( width ) ;
561
+ expect ( + svg . getAttribute ( 'height' ) ) . toBe ( height ) ;
562
+ }
563
+
564
+ function testResponsive ( ) {
565
+ checkLayoutSize ( startWidth , startHeight ) ;
566
+ viewport . set ( newWidth , newHeight ) ;
567
+
568
+ return Promise . resolve ( )
569
+ . then ( delay ( 200 ) )
570
+ . then ( function ( ) {
571
+ checkLayoutSize ( newWidth , newHeight ) ;
572
+ } )
573
+ . catch ( failTest ) ;
574
+ }
575
+
576
+ it ( 'should resize when the viewport width/height changes' , function ( done ) {
577
+ Plotly . plot ( gd , data , { } , { responsive : true } )
578
+ . then ( testResponsive )
579
+ . then ( done ) ;
580
+ } ) ;
581
+
582
+ it ( 'should still be responsive if the plot is edited' , function ( done ) {
583
+ Plotly . plot ( gd , data , { } , { responsive : true } )
584
+ . then ( function ( ) { return Plotly . restyle ( gd , 'y[0]' , data [ 0 ] . y [ 0 ] + 2 ) ; } )
585
+ . then ( testResponsive )
586
+ . then ( done ) ;
587
+ } ) ;
588
+
589
+ it ( 'should still be responsive if the plot is purged and replotted' , function ( done ) {
590
+ Plotly . plot ( gd , data , { } , { responsive : true } )
591
+ . then ( function ( ) { return Plotly . newPlot ( gd , data , { } , { responsive : true } ) ; } )
592
+ . then ( testResponsive )
593
+ . then ( done ) ;
594
+ } ) ;
595
+
596
+ it ( 'should only have one resize handler when plotted more than once' , function ( done ) {
597
+ var cntWindowResize = 0 ;
598
+ window . addEventListener ( 'resize' , function ( ) { cntWindowResize ++ ; } ) ;
599
+ spyOn ( Plotly . Plots , 'resize' ) . and . callThrough ( ) ;
600
+
601
+ Plotly . plot ( gd , data , { } , { responsive : true } )
602
+ . then ( function ( ) { return Plotly . restyle ( gd , 'y[0]' , data [ 0 ] . y [ 0 ] + 2 ) ; } )
603
+ . then ( function ( ) { viewport . set ( newWidth , newHeight ) ; } )
604
+ . then ( delay ( 200 ) )
605
+ // .then(function() {viewport.set(newWidth, 2 * newHeight);}).then(delay(200))
606
+ . then ( function ( ) {
607
+ expect ( cntWindowResize ) . toBe ( 1 ) ;
608
+ expect ( Plotly . Plots . resize . calls . count ( ) ) . toBe ( 1 ) ;
609
+ } )
610
+ . catch ( failTest )
611
+ . then ( done ) ;
612
+ } ) ;
613
+
614
+ it ( 'should become responsive if configured as such via Plotly.react' , function ( done ) {
615
+ Plotly . plot ( gd , data , { } , { responsive : false } )
616
+ . then ( function ( ) { return Plotly . react ( gd , data , { } , { responsive : true } ) ; } )
617
+ . then ( testResponsive )
618
+ . then ( done ) ;
619
+ } ) ;
620
+
621
+ it ( 'should stop being responsive if configured as such via Plotly.react' , function ( done ) {
622
+ Plotly . plot ( gd , data , { } , { responsive : true } )
623
+ // Check initial size
624
+ . then ( function ( ) { checkLayoutSize ( startWidth , startHeight ) ; } )
625
+ // Turn off responsiveness
626
+ . then ( function ( ) { return Plotly . react ( gd , data , { } , { responsive : false } ) ; } )
627
+ // Resize viewport
628
+ . then ( function ( ) { viewport . set ( newWidth , newHeight ) ; } )
629
+ // Wait for resize to happen (Plotly.resize has an internal timeout)
630
+ . then ( delay ( 200 ) )
631
+ // Check that final figure's size hasn't changed
632
+ . then ( function ( ) { checkLayoutSize ( startWidth , startHeight ) ; } )
633
+ . catch ( failTest )
634
+ . then ( done ) ;
635
+ } ) ;
636
+ } ) ;
532
637
} ) ;
0 commit comments