@@ -7,6 +7,9 @@ var d3 = require('d3');
77var createGraphDiv = require ( '../assets/create_graph_div' ) ;
88var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
99var fail = require ( '../assets/fail_test' ) ;
10+ var mouseEvent = require ( '../assets/mouse_event' ) ;
11+ var click = require ( '../assets/click' ) ;
12+ var doubleClick = require ( '../assets/double_click' ) ;
1013
1114describe ( 'Test legacy polar plots logs:' , function ( ) {
1215 var gd ;
@@ -496,3 +499,153 @@ describe('Test relayout on polar subplots:', function() {
496499 . then ( done ) ;
497500 } ) ;
498501} ) ;
502+
503+ describe ( 'Test polar interactions:' , function ( ) {
504+ var gd ;
505+ var eventData ;
506+ var eventCnts ;
507+
508+ var eventNames = [
509+ 'plotly_hover' , 'plotly_unhover' ,
510+ 'plotly_click' , 'plotly_doubleclick' ,
511+ 'plotly_relayout'
512+ ] ;
513+
514+ beforeEach ( function ( ) {
515+ eventData = '' ;
516+ eventCnts = { } ;
517+ gd = createGraphDiv ( ) ;
518+ } ) ;
519+
520+ afterEach ( destroyGraphDiv ) ;
521+
522+ function _plot ( fig ) {
523+ return Plotly . plot ( gd , fig ) . then ( function ( ) {
524+ eventNames . forEach ( function ( k ) {
525+ eventCnts [ k ] = 0 ;
526+ gd . on ( k , function ( d ) {
527+ eventData = d ;
528+ eventCnts [ k ] ++ ;
529+ Lib . clearThrottle ( ) ;
530+ } ) ;
531+ } ) ;
532+ } ) ;
533+ }
534+
535+ function assertEventPointData ( expected , msg ) {
536+ var actual = eventData . points || [ ] ;
537+
538+ expect ( actual . length )
539+ . toBe ( expected . length , msg + ' same number of pts' ) ;
540+
541+ expected . forEach ( function ( e , i ) {
542+ var a = actual [ i ] ;
543+ var m = msg + ' (pt ' + i + ')' ;
544+
545+ for ( var k in e ) {
546+ expect ( a [ k ] ) . toBeCloseTo ( e [ k ] , 1 , m + ' ' + k ) ;
547+ }
548+ } ) ;
549+ }
550+
551+ function assertEventCnt ( expected , msg ) {
552+ eventNames . forEach ( function ( k ) {
553+ var m = msg + ' event cnt for ' + k ;
554+
555+ if ( k in expected ) {
556+ expect ( eventCnts [ k ] ) . toBe ( expected [ k ] , m ) ;
557+ } else {
558+ expect ( eventCnts [ k ] ) . toBe ( 0 , m ) ;
559+ }
560+ } ) ;
561+ }
562+
563+ function _hover ( pos ) {
564+ eventData = '' ;
565+ mouseEvent ( 'mousemove' , pos [ 0 ] , pos [ 1 ] ) ;
566+ }
567+
568+ function _unhover ( pos ) {
569+ eventData = '' ;
570+ mouseEvent ( 'mouseout' , pos [ 0 ] , pos [ 1 ] ) ;
571+ }
572+
573+ function _click ( pos ) {
574+ eventData = '' ;
575+ gd . _mouseDownTime = 0 ;
576+ click ( pos [ 0 ] , pos [ 1 ] ) ;
577+ }
578+
579+ function _doubleClick ( pos ) {
580+ gd . _mouseDownTime = 0 ;
581+ eventData = '' ;
582+ return doubleClick ( pos [ 0 ] , pos [ 1 ] ) ;
583+ }
584+
585+ it ( 'should trigger hover/unhover/click/doubleclick events' , function ( done ) {
586+ var fig = Lib . extendDeep ( { } , require ( '@mocks/polar_scatter.json' ) ) ;
587+ var ptPos = [ 250 , 200 ] ;
588+ var blankPos = [ 200 , 120 ] ;
589+ var marginPos = [ 20 , 20 ] ;
590+
591+ function _assert ( ptExpectation , cntExpecation , msg ) {
592+ if ( Array . isArray ( ptExpectation ) ) {
593+ assertEventPointData ( ptExpectation , msg ) ;
594+ } else {
595+ expect ( eventData ) . toBe ( ptExpectation , msg ) ;
596+ }
597+ assertEventCnt ( cntExpecation , msg ) ;
598+ }
599+
600+ _plot ( fig )
601+ . then ( function ( ) { _hover ( ptPos ) ; } )
602+ . then ( function ( ) {
603+ _assert ( [ {
604+ r : 3.26 ,
605+ theta : 68.08
606+ } ] , {
607+ plotly_hover : 1
608+ } , 'after hover on pt' ) ;
609+ } )
610+ . then ( function ( ) { _unhover ( blankPos ) ; } )
611+ . then ( function ( ) {
612+ _assert ( [ {
613+ r : 3.26 ,
614+ theta : 68.08
615+ } ] , {
616+ plotly_hover : 1 ,
617+ plotly_unhover : 1
618+ } , 'after unhover off pt' ) ;
619+ } )
620+ . then ( function ( ) { _hover ( marginPos ) ; } )
621+ . then ( function ( ) {
622+ _assert ( '' , {
623+ plotly_hover : 1 ,
624+ plotly_unhover : 1 ,
625+ } , 'after hovering in margin' ) ;
626+ } )
627+ . then ( function ( ) { _click ( ptPos ) ; } )
628+ . then ( function ( ) {
629+ _assert ( [ {
630+ r : 3.26 ,
631+ theta : 68.08
632+ } ] , {
633+ plotly_hover : 2 ,
634+ plotly_unhover : 1 ,
635+ plotly_click : 1
636+ } , 'after click' ) ;
637+ } )
638+ . then ( function ( ) { return _doubleClick ( ptPos ) ; } )
639+ . then ( function ( ) {
640+ assertEventCnt ( {
641+ plotly_hover : 2 ,
642+ plotly_unhover : 1 ,
643+ plotly_click : 3 ,
644+ plotly_doubleclick : 1 ,
645+ plotly_relayout : 1
646+ } , 'after doubleclick' ) ;
647+ } )
648+ . catch ( fail )
649+ . then ( done ) ;
650+ } ) ;
651+ } ) ;
0 commit comments