@@ -3646,3 +3646,134 @@ describe('dragmode: false', function() {
3646
3646
. then ( done ) ;
3647
3647
} ) ;
3648
3648
} ) ;
3649
+
3650
+ describe ( 'hovermode: (x|y)unified' , function ( ) {
3651
+ var gd ;
3652
+ var mock = require ( '@mocks/hovermode_xunified.json' ) ;
3653
+
3654
+ beforeEach ( function ( ) {
3655
+ gd = createGraphDiv ( ) ;
3656
+ } ) ;
3657
+
3658
+ afterEach ( destroyGraphDiv ) ;
3659
+
3660
+ function _hover ( gd , opts ) {
3661
+ Fx . hover ( gd , opts ) ;
3662
+ Lib . clearThrottle ( ) ;
3663
+ }
3664
+
3665
+ function assertElementCount ( className , size ) {
3666
+ var g = d3 . selectAll ( 'g.' + className ) ;
3667
+ expect ( g . size ( ) ) . toBe ( size ) ;
3668
+ }
3669
+
3670
+ function assertLabel ( expectation ) {
3671
+ var hover = d3 . select ( 'g.legend' ) ;
3672
+ var title = hover . select ( 'text.legendtitletext' ) ;
3673
+ var traces = hover . selectAll ( 'g.traces' ) ;
3674
+
3675
+ if ( expectation . title ) {
3676
+ expect ( title . text ( ) ) . toBe ( expectation . title ) ;
3677
+ }
3678
+
3679
+ expect ( traces . size ( ) ) . toBe ( expectation . items . length , 'has the incorrect number of items' ) ;
3680
+ traces . each ( function ( _ , i ) {
3681
+ var e = d3 . select ( this ) ;
3682
+ expect ( e . select ( 'text' ) . text ( ) ) . toBe ( expectation . items [ i ] ) ;
3683
+ } ) ;
3684
+ }
3685
+
3686
+ it ( 'set smart defaults for spikeline in xunified' , function ( done ) {
3687
+ Plotly . newPlot ( gd , [ { y : [ 4 , 6 , 5 ] } ] , { 'hovermode' : 'xunified' , 'xaxis' : { 'color' : 'red' } } )
3688
+ . then ( function ( gd ) {
3689
+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'xunified' ) ;
3690
+ var ax = gd . _fullLayout . xaxis ;
3691
+ expect ( ax . showspike ) . toBeTrue ;
3692
+ expect ( ax . spikemode ) . toBe ( 'across' ) ;
3693
+ expect ( ax . spikedash ) . toBe ( 'dot' ) ;
3694
+ expect ( ax . spikecolor ) . toBe ( 'red' ) ;
3695
+ expect ( gd . _fullLayout . yaxis . showspike ) . toBeFalse ;
3696
+ } )
3697
+ . catch ( failTest )
3698
+ . then ( done ) ;
3699
+ } ) ;
3700
+
3701
+ it ( 'set smart defaults for spikeline in yunified' , function ( done ) {
3702
+ Plotly . newPlot ( gd , [ { y : [ 4 , 6 , 5 ] } ] , { 'hovermode' : 'yunified' , 'yaxis' : { 'color' : 'red' } } )
3703
+ . then ( function ( gd ) {
3704
+ expect ( gd . _fullLayout . hovermode ) . toBe ( 'yunified' ) ;
3705
+ var ax = gd . _fullLayout . yaxis ;
3706
+ expect ( ax . showspike ) . toBeTrue ;
3707
+ expect ( ax . spikemode ) . toBe ( 'across' ) ;
3708
+ expect ( ax . spikedash ) . toBe ( 'dot' ) ;
3709
+ expect ( ax . spikecolor ) . toBe ( 'red' ) ;
3710
+ expect ( gd . _fullLayout . yaxis . showspike ) . toBeFalse ;
3711
+ } )
3712
+ . catch ( failTest )
3713
+ . then ( done ) ;
3714
+ } ) ;
3715
+
3716
+ it ( 'xunified should work for x/y cartesian traces' , function ( done ) {
3717
+ var mockCopy = Lib . extendDeep ( { } , mock ) ;
3718
+ Plotly . newPlot ( gd , mockCopy )
3719
+ . then ( function ( gd ) {
3720
+ _hover ( gd , { xval : 3 } ) ;
3721
+
3722
+ assertLabel ( { title : '3' , items : [ 'trace 0 : 4' , 'trace 1 : 8' ] } ) ;
3723
+ } )
3724
+ . catch ( failTest )
3725
+ . then ( done ) ;
3726
+ } ) ;
3727
+
3728
+ it ( 'yunified should work for x/y cartesian traces' , function ( done ) {
3729
+ var mockCopy = Lib . extendDeep ( { } , mock ) ;
3730
+ mockCopy . layout . hovermode = 'yunified' ;
3731
+ Plotly . newPlot ( gd , mockCopy )
3732
+ . then ( function ( gd ) {
3733
+ _hover ( gd , { yval : 6 } ) ;
3734
+
3735
+ assertLabel ( { title : '6' , items : [ 'trace 0 : 2' , 'trace 1 : 5' ] } ) ;
3736
+ } )
3737
+ . catch ( failTest )
3738
+ . then ( done ) ;
3739
+ } ) ;
3740
+
3741
+ it ( 'should work with hovertemplate' , function ( done ) {
3742
+ var mockCopy = Lib . extendDeep ( { } , mock ) ;
3743
+ mockCopy . data [ 0 ] . hovertemplate = 'hovertemplate: %{y:0.2f}' ;
3744
+ mockCopy . data [ 1 ] . hovertemplate = '<extra>name</extra>%{x:0.2f} %{y:0.2f}' ;
3745
+ Plotly . newPlot ( gd , mockCopy )
3746
+ . then ( function ( gd ) {
3747
+ _hover ( gd , { xval : 3 } ) ;
3748
+
3749
+ assertLabel ( { title : '3' , items : [
3750
+ 'trace 0 : hovertemplate: 4.00' ,
3751
+ 'name : 3.00 8.00'
3752
+ ] } ) ;
3753
+ } )
3754
+ . catch ( failTest )
3755
+ . then ( done ) ;
3756
+ } ) ;
3757
+
3758
+ it ( 'on relayout, it deletes existing hover' , function ( done ) {
3759
+ var mockCopy = Lib . extendDeep ( { } , mock ) ;
3760
+ mockCopy . layout . hovermode = 'x' ;
3761
+ Plotly . newPlot ( gd , mockCopy )
3762
+ . then ( function ( gd ) {
3763
+ _hover ( gd , { xval : 3 } ) ;
3764
+
3765
+ assertElementCount ( 'hovertext' , 2 ) ;
3766
+ assertElementCount ( 'legend' , 0 ) ;
3767
+
3768
+ return Plotly . relayout ( gd , 'hovermode' , 'xunified' ) ;
3769
+ } )
3770
+ . then ( function ( gd ) {
3771
+ _hover ( gd , { xval : 3 } ) ;
3772
+
3773
+ assertElementCount ( 'hovertext' , 0 ) ;
3774
+ assertElementCount ( 'legend' , 1 ) ;
3775
+ } )
3776
+ . catch ( failTest )
3777
+ . then ( done ) ;
3778
+ } ) ;
3779
+ } ) ;
0 commit comments