1
1
var Plotly = require ( '@lib' ) ;
2
2
var Lib = require ( '@src/lib' ) ;
3
+ var DBLCLICKDELAY = require ( '@src/plots/cartesian/constants' ) . DBLCLICKDELAY ;
4
+
3
5
var d3 = require ( 'd3' ) ;
4
6
var createGraphDiv = require ( '../assets/create_graph_div' ) ;
5
7
var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
8
+ var mouseEvent = require ( '../assets/mouse_event' ) ;
9
+ var customMatchers = require ( '../assets/custom_matchers' ) ;
6
10
7
11
8
12
describe ( 'ternary plots' , function ( ) {
9
13
'use strict' ;
10
14
15
+ beforeAll ( function ( ) {
16
+ jasmine . addMatchers ( customMatchers ) ;
17
+ } ) ;
18
+
11
19
afterEach ( destroyGraphDiv ) ;
12
20
13
21
describe ( 'with scatterternary trace(s)' , function ( ) {
14
22
var mock = require ( '@mocks/ternary_simple.json' ) ;
15
23
var gd ;
16
24
25
+ var pointPos = [ 391 , 219 ] ;
26
+ var blankPos = [ 200 , 200 ] ;
27
+
17
28
beforeEach ( function ( done ) {
18
29
gd = createGraphDiv ( ) ;
19
30
@@ -73,7 +84,99 @@ describe('ternary plots', function() {
73
84
74
85
done ( ) ;
75
86
} ) ;
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 ] ) ;
76
173
174
+ return doubleClick ( pointPos [ 0 ] , pointPos [ 1 ] ) ;
175
+ } ) . then ( function ( ) {
176
+ assertRange ( gd , [ 0 , 0 , 0 ] ) ;
177
+
178
+ done ( ) ;
179
+ } ) ;
77
180
} ) ;
78
181
79
182
} ) ;
@@ -85,4 +188,36 @@ describe('ternary plots', function() {
85
188
function countTraces ( type ) {
86
189
return d3 . selectAll ( '.ternary' ) . selectAll ( 'g.trace.' + type ) . size ( ) ;
87
190
}
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
+ }
88
223
} ) ;
0 commit comments