@@ -3,6 +3,11 @@ var d3 = require('d3');
33var createModeBar = require ( '@src/components/modebar' ) ;
44var manageModeBar = require ( '@src/components/modebar/manage' ) ;
55
6+ var Plotly = require ( '@lib/index' ) ;
7+ var createGraphDiv = require ( '../assets/create_graph_div' ) ;
8+ var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
9+ var selectButton = require ( '../assets/modebar_button' ) ;
10+
611
712describe ( 'ModeBar' , function ( ) {
813 'use strict' ;
@@ -553,4 +558,222 @@ describe('ModeBar', function() {
553558
554559 } ) ;
555560
561+ describe ( 'modebar on clicks' , function ( ) {
562+ var gd , modeBar ;
563+
564+ afterEach ( destroyGraphDiv ) ;
565+
566+ function assertRange ( actual , expected ) {
567+ var PRECISION = 4 ;
568+ expect ( actual [ 0 ] ) . toBeCloseTo ( expected [ 0 ] , PRECISION ) ;
569+ expect ( actual [ 1 ] ) . toBeCloseTo ( expected [ 1 ] , PRECISION ) ;
570+ }
571+
572+ function assertActive ( buttons , activeButton ) {
573+ for ( var i = 0 ; i < buttons . length ; i ++ ) {
574+ expect ( buttons [ i ] . isActive ( ) ) . toBe (
575+ buttons [ i ] === activeButton
576+ ) ;
577+ }
578+ }
579+
580+ describe ( 'cartesian handlers' , function ( ) {
581+
582+ beforeEach ( function ( done ) {
583+ var mockData = [ {
584+ type : 'scatter' ,
585+ y : [ 2 , 1 , 2 ]
586+ } , {
587+ type : 'bar' ,
588+ y : [ 2 , 1 , 2 ] ,
589+ xaxis : 'x2' ,
590+ yaxis : 'y2'
591+ } ] ;
592+
593+ var mockLayout = {
594+ xaxis : {
595+ anchor : 'y' ,
596+ domain : [ 0 , 0.5 ] ,
597+ range : [ 0 , 5 ]
598+ } ,
599+ yaxis : {
600+ anchor : 'x' ,
601+ range : [ 0 , 3 ]
602+ } ,
603+ xaxis2 : {
604+ anchor : 'y2' ,
605+ domain : [ 0.5 , 1 ] ,
606+ range : [ - 1 , 4 ]
607+ } ,
608+ yaxis2 : {
609+ anchor : 'x2' ,
610+ range : [ 0 , 4 ]
611+ }
612+ } ;
613+
614+ gd = createGraphDiv ( ) ;
615+ Plotly . plot ( gd , mockData , mockLayout ) . then ( function ( ) {
616+ modeBar = gd . _fullLayout . _modeBar ;
617+ done ( ) ;
618+ } ) ;
619+ } ) ;
620+
621+ describe ( 'buttons zoomIn2d, zoomOut2d, autoScale2d and resetScale2d' , function ( ) {
622+ it ( 'should update axis ranges' , function ( ) {
623+ var buttonZoomIn = selectButton ( modeBar , 'zoomIn2d' ) ,
624+ buttonZoomOut = selectButton ( modeBar , 'zoomOut2d' ) ,
625+ buttonAutoScale = selectButton ( modeBar , 'autoScale2d' ) ,
626+ buttonResetScale = selectButton ( modeBar , 'resetScale2d' ) ;
627+
628+ assertRange ( gd . _fullLayout . xaxis . range , [ 0 , 5 ] ) ;
629+ assertRange ( gd . _fullLayout . yaxis . range , [ 0 , 3 ] ) ;
630+ assertRange ( gd . _fullLayout . xaxis2 . range , [ - 1 , 4 ] ) ;
631+ assertRange ( gd . _fullLayout . yaxis2 . range , [ 0 , 4 ] ) ;
632+
633+ buttonZoomIn . click ( ) ;
634+ assertRange ( gd . _fullLayout . xaxis . range , [ 1.25 , 3.75 ] ) ;
635+ assertRange ( gd . _fullLayout . yaxis . range , [ 0.75 , 2.25 ] ) ;
636+ assertRange ( gd . _fullLayout . xaxis2 . range , [ 0.25 , 2.75 ] ) ;
637+ assertRange ( gd . _fullLayout . yaxis2 . range , [ 1 , 3 ] ) ;
638+
639+ buttonZoomOut . click ( ) ;
640+ assertRange ( gd . _fullLayout . xaxis . range , [ 0 , 5 ] ) ;
641+ assertRange ( gd . _fullLayout . yaxis . range , [ 0 , 3 ] ) ;
642+ assertRange ( gd . _fullLayout . xaxis2 . range , [ - 1 , 4 ] ) ;
643+ assertRange ( gd . _fullLayout . yaxis2 . range , [ 0 , 4 ] ) ;
644+
645+ buttonZoomIn . click ( ) ;
646+ buttonAutoScale . click ( ) ;
647+ assertRange ( gd . _fullLayout . xaxis . range , [ - 0.1375913 , 2.137591 ] ) ;
648+ assertRange ( gd . _fullLayout . yaxis . range , [ 0.92675159 , 2.073248 ] ) ;
649+ assertRange ( gd . _fullLayout . xaxis2 . range , [ - 0.5 , 2.5 ] ) ;
650+ assertRange ( gd . _fullLayout . yaxis2 . range , [ 0 , 2.105263 ] ) ;
651+
652+ buttonResetScale . click ( ) ;
653+ assertRange ( gd . _fullLayout . xaxis . range , [ 0 , 5 ] ) ;
654+ assertRange ( gd . _fullLayout . yaxis . range , [ 0 , 3 ] ) ;
655+ assertRange ( gd . _fullLayout . xaxis2 . range , [ - 1 , 4 ] ) ;
656+ assertRange ( gd . _fullLayout . yaxis2 . range , [ 0 , 4 ] ) ;
657+ } ) ;
658+ } ) ;
659+
660+ describe ( 'buttons zoom2d, pan2d, select2d and lasso2d' , function ( ) {
661+ it ( 'should update the layout dragmode' , function ( ) {
662+ var zoom2d = selectButton ( modeBar , 'zoom2d' ) ,
663+ pan2d = selectButton ( modeBar , 'pan2d' ) ,
664+ select2d = selectButton ( modeBar , 'select2d' ) ,
665+ lasso2d = selectButton ( modeBar , 'lasso2d' ) ,
666+ buttons = [ zoom2d , pan2d , select2d , lasso2d ] ;
667+
668+ expect ( gd . _fullLayout . dragmode ) . toBe ( 'zoom' ) ;
669+ assertActive ( buttons , zoom2d ) ;
670+
671+ pan2d . click ( ) ;
672+ expect ( gd . _fullLayout . dragmode ) . toBe ( 'pan' ) ;
673+ assertActive ( buttons , pan2d ) ;
674+
675+ select2d . click ( ) ;
676+ expect ( gd . _fullLayout . dragmode ) . toBe ( 'select' ) ;
677+ assertActive ( buttons , select2d ) ;
678+
679+ lasso2d . click ( ) ;
680+ expect ( gd . _fullLayout . dragmode ) . toBe ( 'lasso' ) ;
681+ assertActive ( buttons , lasso2d ) ;
682+
683+ zoom2d . click ( ) ;
684+ expect ( gd . _fullLayout . dragmode ) . toBe ( 'zoom' ) ;
685+ assertActive ( buttons , zoom2d ) ;
686+ } ) ;
687+ } ) ;
688+
689+ describe ( 'buttons hoverCompareCartesian and hoverClosestCartesian ' , function ( ) {
690+ it ( 'should update layout hovermode' , function ( ) {
691+ var buttonCompare = selectButton ( modeBar , 'hoverCompareCartesian' ) ,
692+ buttonClosest = selectButton ( modeBar , 'hoverClosestCartesian' ) ,
693+ buttons = [ buttonCompare , buttonClosest ] ;
694+
695+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'x' ) ;
696+ assertActive ( buttons , buttonCompare ) ;
697+
698+ buttonClosest . click ( ) ;
699+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'closest' ) ;
700+ assertActive ( buttons , buttonClosest ) ;
701+
702+ buttonCompare . click ( ) ;
703+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'x' ) ;
704+ assertActive ( buttons , buttonCompare ) ;
705+ } ) ;
706+ } ) ;
707+ } ) ;
708+
709+ describe ( 'pie handlers' , function ( ) {
710+
711+ beforeEach ( function ( done ) {
712+ var mockData = [ {
713+ type : 'pie' ,
714+ labels : [ 'apples' , 'bananas' , 'grapes' ] ,
715+ values : [ 10 , 20 , 30 ]
716+ } ] ;
717+
718+ gd = createGraphDiv ( ) ;
719+ Plotly . plot ( gd , mockData ) . then ( function ( ) {
720+ modeBar = gd . _fullLayout . _modeBar ;
721+ done ( ) ;
722+ } ) ;
723+ } ) ;
724+
725+ describe ( 'buttons hoverClosestPie' , function ( ) {
726+ it ( 'should update layout hovermode' , function ( ) {
727+ var button = selectButton ( modeBar , 'hoverClosestPie' ) ;
728+
729+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'closest' ) ;
730+ expect ( button . isActive ( ) ) . toBe ( true ) ;
731+
732+ button . click ( ) ;
733+ expect ( gd . _fullLayout . hovermode ) . toBe ( false ) ;
734+ expect ( button . isActive ( ) ) . toBe ( false ) ;
735+
736+ button . click ( ) ;
737+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'closest' ) ;
738+ expect ( button . isActive ( ) ) . toBe ( true ) ;
739+ } ) ;
740+ } ) ;
741+ } ) ;
742+
743+ describe ( 'geo handlers' , function ( ) {
744+
745+ beforeEach ( function ( done ) {
746+ var mockData = [ {
747+ type : 'scattergeo' ,
748+ lon : [ 10 , 20 , 30 ] ,
749+ lat : [ 10 , 20 , 30 ]
750+ } ] ;
751+
752+ gd = createGraphDiv ( ) ;
753+ Plotly . plot ( gd , mockData ) . then ( function ( ) {
754+ modeBar = gd . _fullLayout . _modeBar ;
755+ done ( ) ;
756+ } ) ;
757+ } ) ;
758+
759+ describe ( 'buttons hoverClosestGeo' , function ( ) {
760+ it ( 'should update layout hovermode' , function ( ) {
761+ var button = selectButton ( modeBar , 'hoverClosestGeo' ) ;
762+
763+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'closest' ) ;
764+ expect ( button . isActive ( ) ) . toBe ( true ) ;
765+
766+ button . click ( ) ;
767+ expect ( gd . _fullLayout . hovermode ) . toBe ( false ) ;
768+ expect ( button . isActive ( ) ) . toBe ( false ) ;
769+
770+ button . click ( ) ;
771+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'closest' ) ;
772+ expect ( button . isActive ( ) ) . toBe ( true ) ;
773+ } ) ;
774+ } ) ;
775+
776+ } ) ;
777+
778+ } ) ;
556779} ) ;
0 commit comments