@@ -7,6 +7,7 @@ var Lib = require('@src/lib');
77var Plotly = require ( '@lib/index' ) ;
88var createGraphDiv = require ( '../assets/create_graph_div' ) ;
99var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
10+ var customMatchers = require ( '../assets/custom_matchers' ) ;
1011var fail = require ( '../assets/fail_test' ) ;
1112
1213describe ( 'Test scatter' , function ( ) {
@@ -372,3 +373,76 @@ describe('end-to-end scatter tests', function() {
372373 } ) . catch ( fail ) . then ( done ) ;
373374 } ) ;
374375} ) ;
376+
377+ describe ( 'scatter hoverPoints' , function ( ) {
378+
379+ beforeAll ( function ( ) {
380+ jasmine . addMatchers ( customMatchers ) ;
381+ } ) ;
382+
383+ afterEach ( destroyGraphDiv ) ;
384+
385+ function _hover ( gd , xval , yval , hovermode ) {
386+ return gd . _fullData . map ( function ( trace , i ) {
387+ var cd = gd . calcdata [ i ] ;
388+ var subplot = gd . _fullLayout . _plots . xy ;
389+
390+ var out = Scatter . hoverPoints ( {
391+ index : false ,
392+ distance : 20 ,
393+ cd : cd ,
394+ trace : trace ,
395+ xa : subplot . xaxis ,
396+ ya : subplot . yaxis
397+ } , xval , yval , hovermode ) ;
398+
399+ return Array . isArray ( out ) ? out [ 0 ] : null ;
400+ } ) ;
401+ }
402+
403+ it ( 'should show \'hovertext\' items when present, \'text\' if not' , function ( done ) {
404+ var gd = createGraphDiv ( ) ;
405+ var mock = Lib . extendDeep ( { } , require ( '@mocks/text_chart_arrays' ) ) ;
406+
407+ Plotly . plot ( gd , mock ) . then ( function ( ) {
408+ var pts = _hover ( gd , 0 , 1 , 'x' ) ;
409+
410+ // as in 'hovertext' arrays
411+ expect ( pts [ 0 ] . text ) . toEqual ( 'Hover text\nA' , 'hover text' ) ;
412+ expect ( pts [ 1 ] . text ) . toEqual ( 'Hover text G' , 'hover text' ) ;
413+ expect ( pts [ 2 ] . text ) . toEqual ( 'a (hover)' , 'hover text' ) ;
414+
415+ return Plotly . restyle ( gd , 'hovertext' , null ) ;
416+ } )
417+ . then ( function ( ) {
418+ var pts = _hover ( gd , 0 , 1 , 'x' ) ;
419+
420+ // as in 'text' arrays
421+ expect ( pts [ 0 ] . text ) . toEqual ( 'Text\nA' , 'hover text' ) ;
422+ expect ( pts [ 1 ] . text ) . toEqual ( 'Text G' , 'hover text' ) ;
423+ expect ( pts [ 2 ] . text ) . toEqual ( 'a' , 'hover text' ) ;
424+
425+ return Plotly . restyle ( gd , 'text' , [ 'APPLE' , 'BANANA' , 'ORANGE' ] ) ;
426+ } )
427+ . then ( function ( ) {
428+ var pts = _hover ( gd , 1 , 1 , 'x' ) ;
429+
430+ // as in 'text' values
431+ expect ( pts [ 0 ] . text ) . toEqual ( 'APPLE' , 'hover text' ) ;
432+ expect ( pts [ 1 ] . text ) . toEqual ( 'BANANA' , 'hover text' ) ;
433+ expect ( pts [ 2 ] . text ) . toEqual ( 'ORANGE' , 'hover text' ) ;
434+
435+ return Plotly . restyle ( gd , 'hovertext' , [ 'apple' , 'banana' , 'orange' ] ) ;
436+ } )
437+ . then ( function ( ) {
438+ var pts = _hover ( gd , 1 , 1 , 'x' ) ;
439+
440+ // as in 'hovertext' values
441+ expect ( pts [ 0 ] . text ) . toEqual ( 'apple' , 'hover text' ) ;
442+ expect ( pts [ 1 ] . text ) . toEqual ( 'banana' , 'hover text' ) ;
443+ expect ( pts [ 2 ] . text ) . toEqual ( 'orange' , 'hover text' ) ;
444+ } )
445+ . catch ( fail )
446+ . then ( done ) ;
447+ } ) ;
448+ } ) ;
0 commit comments