11var Plotly = require ( '@lib' ) ;
22var Lib = require ( '@src/lib' ) ;
3+ var DBLCLICKDELAY = require ( '@src/plots/cartesian/constants' ) . DBLCLICKDELAY ;
4+
35var d3 = require ( 'd3' ) ;
46var createGraphDiv = require ( '../assets/create_graph_div' ) ;
57var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
8+ var mouseEvent = require ( '../assets/mouse_event' ) ;
9+ var customMatchers = require ( '../assets/custom_matchers' ) ;
610
711
812describe ( 'ternary plots' , function ( ) {
913 'use strict' ;
1014
15+ beforeAll ( function ( ) {
16+ jasmine . addMatchers ( customMatchers ) ;
17+ } ) ;
18+
1119 afterEach ( destroyGraphDiv ) ;
1220
1321 describe ( 'with scatterternary trace(s)' , function ( ) {
1422 var mock = require ( '@mocks/ternary_simple.json' ) ;
1523 var gd ;
1624
25+ var pointPos = [ 391 , 219 ] ;
26+ var blankPos = [ 200 , 200 ] ;
27+
1728 beforeEach ( function ( done ) {
1829 gd = createGraphDiv ( ) ;
1930
@@ -73,7 +84,99 @@ describe('ternary plots', function() {
7384
7485 done ( ) ;
7586 } ) ;
87+ } ) ;
88+
89+ it ( 'should display to hover labels' , function ( ) {
90+ var hoverLabels ;
91+
92+ mouseEvent ( 'mousemove' , blankPos [ 0 ] , blankPos [ 1 ] ) ;
93+ hoverLabels = findHoverLabels ( ) ;
94+ expect ( hoverLabels . size ( ) ) . toEqual ( 0 , 'only on data points' ) ;
95+
96+ mouseEvent ( 'mousemove' , pointPos [ 0 ] , pointPos [ 1 ] ) ;
97+ hoverLabels = findHoverLabels ( ) ;
98+ expect ( hoverLabels . size ( ) ) . toEqual ( 1 , 'one per data point' ) ;
99+
100+ var rows = hoverLabels . selectAll ( 'tspan' ) ;
101+ expect ( rows [ 0 ] [ 0 ] . innerHTML ) . toEqual ( 'Component A: 0.5' , 'with correct text' ) ;
102+ expect ( rows [ 0 ] [ 1 ] . innerHTML ) . toEqual ( 'B: 0.25' , 'with correct text' ) ;
103+ expect ( rows [ 0 ] [ 2 ] . innerHTML ) . toEqual ( 'Component C: 0.25' , 'with correct text' ) ;
104+ } ) ;
105+
106+ it ( 'should respond to hover interactions by' , function ( ) {
107+ var hoverCnt = 0 ,
108+ unhoverCnt = 0 ;
109+
110+ var hoverData , unhoverData ;
111+
112+ gd . on ( 'plotly_hover' , function ( eventData ) {
113+ hoverCnt ++ ;
114+ hoverData = eventData . points [ 0 ] ;
115+ } ) ;
116+
117+ gd . on ( 'plotly_unhover' , function ( eventData ) {
118+ unhoverCnt ++ ;
119+ unhoverData = eventData . points [ 0 ] ;
120+ } ) ;
121+
122+ mouseEvent ( 'mousemove' , blankPos [ 0 ] , blankPos [ 1 ] ) ;
123+ expect ( hoverData ) . toBe ( undefined , 'not firing on blank points' ) ;
124+ expect ( unhoverData ) . toBe ( undefined , 'not firing on blank points' ) ;
125+
126+ mouseEvent ( 'mousemove' , pointPos [ 0 ] , pointPos [ 1 ] ) ;
127+ expect ( hoverData ) . not . toBe ( undefined , 'firing on data points' ) ;
128+ expect ( Object . keys ( hoverData ) ) . toEqual ( [
129+ 'data' , 'fullData' , 'curveNumber' , 'pointNumber' ,
130+ 'x' , 'y' , 'xaxis' , 'yaxis'
131+ ] , 'returning the correct event data keys' ) ;
132+ expect ( hoverData . curveNumber ) . toEqual ( 0 , 'returning the correct curve number' ) ;
133+ expect ( hoverData . pointNumber ) . toEqual ( 0 , 'returning the correct point number' ) ;
134+
135+ mouseEvent ( 'mouseout' , pointPos [ 0 ] , pointPos [ 1 ] ) ;
136+ expect ( unhoverData ) . not . toBe ( undefined , 'firing on data points' ) ;
137+ expect ( Object . keys ( unhoverData ) ) . toEqual ( [
138+ 'data' , 'fullData' , 'curveNumber' , 'pointNumber' ,
139+ 'x' , 'y' , 'xaxis' , 'yaxis'
140+ ] , 'returning the correct event data keys' ) ;
141+ expect ( unhoverData . curveNumber ) . toEqual ( 0 , 'returning the correct curve number' ) ;
142+ expect ( unhoverData . pointNumber ) . toEqual ( 0 , 'returning the correct point number' ) ;
143+
144+ expect ( hoverCnt ) . toEqual ( 1 ) ;
145+ expect ( unhoverCnt ) . toEqual ( 1 ) ;
146+ } ) ;
147+
148+ it ( 'should respond to click interactions by' , function ( ) {
149+ var ptData ;
150+
151+ gd . on ( 'plotly_click' , function ( eventData ) {
152+ ptData = eventData . points [ 0 ] ;
153+ } ) ;
154+
155+ click ( blankPos [ 0 ] , blankPos [ 1 ] ) ;
156+ expect ( ptData ) . toBe ( undefined , 'not firing on blank points' ) ;
157+
158+ click ( pointPos [ 0 ] , pointPos [ 1 ] ) ;
159+ expect ( ptData ) . not . toBe ( undefined , 'firing on data points' ) ;
160+ expect ( Object . keys ( ptData ) ) . toEqual ( [
161+ 'data' , 'fullData' , 'curveNumber' , 'pointNumber' ,
162+ 'x' , 'y' , 'xaxis' , 'yaxis'
163+ ] , 'returning the correct event data keys' ) ;
164+ expect ( ptData . curveNumber ) . toEqual ( 0 , 'returning the correct curve number' ) ;
165+ expect ( ptData . pointNumber ) . toEqual ( 0 , 'returning the correct point number' ) ;
166+ } ) ;
167+
168+ it ( 'should respond zoom drag interactions' , function ( done ) {
169+ assertRange ( gd , [ 0.231 , 0.2 , 0.11 ] ) ;
170+
171+ Plotly . relayout ( gd , 'ternary.aaxis.min' , 0.1 ) . then ( function ( ) {
172+ assertRange ( gd , [ 0.1 , 0.2 , 0.11 ] ) ;
76173
174+ return doubleClick ( pointPos [ 0 ] , pointPos [ 1 ] ) ;
175+ } ) . then ( function ( ) {
176+ assertRange ( gd , [ 0 , 0 , 0 ] ) ;
177+
178+ done ( ) ;
179+ } ) ;
77180 } ) ;
78181
79182 } ) ;
@@ -85,4 +188,36 @@ describe('ternary plots', function() {
85188 function countTraces ( type ) {
86189 return d3 . selectAll ( '.ternary' ) . selectAll ( 'g.trace.' + type ) . size ( ) ;
87190 }
191+
192+ function findHoverLabels ( ) {
193+ return d3 . select ( '.hoverlayer' ) . selectAll ( 'g' ) ;
194+ }
195+
196+ function click ( x , y ) {
197+ mouseEvent ( 'mousemove' , x , y ) ;
198+ mouseEvent ( 'mousedown' , x , y ) ;
199+ mouseEvent ( 'mouseup' , x , y ) ;
200+ }
201+
202+ function doubleClick ( x , y ) {
203+ return new Promise ( function ( resolve ) {
204+ click ( x , y ) ;
205+
206+ setTimeout ( function ( ) {
207+ click ( x , y ) ;
208+ resolve ( ) ;
209+ } , DBLCLICKDELAY / 2 ) ;
210+ } ) ;
211+ }
212+
213+ function assertRange ( gd , expected ) {
214+ var ternary = gd . _fullLayout . ternary ;
215+ var actual = [
216+ ternary . aaxis . min ,
217+ ternary . baxis . min ,
218+ ternary . caxis . min
219+ ] ;
220+
221+ expect ( actual ) . toBeCloseToArray ( expected ) ;
222+ }
88223} ) ;
0 commit comments