Skip to content

Commit d4c641b

Browse files
committed
streamline gl2d_click_test
1 parent 41b6b66 commit d4c641b

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

test/jasmine/tests/gl2d_click_test.js

+47-36
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var fail = require('../assets/fail_test.js');
1212
// a click event on mouseup
1313
var click = require('../assets/timed_click');
1414
var hover = require('../assets/hover');
15+
var delay = require('../assets/delay');
1516

1617
// contourgl is not part of the dist plotly.js bundle initially
1718
Plotly.register([
@@ -62,13 +63,6 @@ var mock4 = {
6263
describe('Test hover and click interactions', function() {
6364
var gd;
6465

65-
// need to wait a little bit before canvas can properly catch mouse events
66-
function wait() {
67-
return new Promise(function(resolve) {
68-
setTimeout(resolve, 100);
69-
});
70-
}
71-
7266
function makeHoverFn(gd, x, y) {
7367
return function() {
7468
return new Promise(function(resolve) {
@@ -90,26 +84,27 @@ describe('Test hover and click interactions', function() {
9084
function makeUnhoverFn(gd, x0, y0) {
9185
return function() {
9286
return new Promise(function(resolve) {
93-
var eventData = null;
94-
95-
gd.on('plotly_unhover', function() {
96-
eventData = 'emitted plotly_unhover';
97-
});
98-
9987
// fairly realistic simulation of moving with the cursor
10088
var canceler = setInterval(function() {
101-
hover(x0--, y0--);
89+
x0 -= 2;
90+
y0 -= 2;
91+
hover(x0, y0);
10292
}, 10);
10393

94+
gd.on('plotly_unhover', function() {
95+
clearInterval(canceler);
96+
resolve('emitted plotly_unhover');
97+
});
98+
10499
setTimeout(function() {
105100
clearInterval(canceler);
106-
resolve(eventData);
101+
resolve(null);
107102
}, 350);
108103
});
109104
};
110105
}
111106

112-
function assertEventData(actual, expected) {
107+
function assertEventData(actual, expected, msg) {
113108
expect(actual.points.length).toEqual(1, 'points length');
114109

115110
var pt = actual.points[0];
@@ -119,30 +114,30 @@ describe('Test hover and click interactions', function() {
119114
'data', 'fullData', 'xaxis', 'yaxis'
120115
], 'event data keys');
121116

122-
expect(typeof pt.data.uid).toEqual('string', 'uid');
123-
expect(pt.xaxis.domain.length).toEqual(2, 'xaxis');
124-
expect(pt.yaxis.domain.length).toEqual(2, 'yaxis');
117+
expect(typeof pt.data.uid).toBe('string', msg + ' - uid');
118+
expect(pt.xaxis.domain.length).toBe(2, msg + ' - xaxis');
119+
expect(pt.yaxis.domain.length).toBe(2, msg + ' - yaxis');
125120

126-
expect(pt.x).toEqual(expected.x, 'x');
127-
expect(pt.y).toEqual(expected.y, 'y');
128-
expect(pt.curveNumber).toEqual(expected.curveNumber, 'curve number');
129-
expect(pt.pointNumber).toEqual(expected.pointNumber, 'point number');
121+
expect(pt.x).toBe(expected.x, msg + ' - x');
122+
expect(pt.y).toBe(expected.y, msg + ' - y');
123+
expect(pt.curveNumber).toBe(expected.curveNumber, msg + ' - curve number');
124+
expect(String(pt.pointNumber)).toBe(String(expected.pointNumber), msg + ' - point number');
130125
}
131126

132-
function assertHoverLabelStyle(sel, expected) {
127+
function assertHoverLabelStyle(sel, expected, msg) {
133128
if(sel.node() === null) {
134129
expect(expected.noHoverLabel).toBe(true);
135130
return;
136131
}
137132

138133
var path = sel.select('path');
139-
expect(path.style('fill')).toEqual(expected.bgColor, 'bgcolor');
140-
expect(path.style('stroke')).toEqual(expected.borderColor, 'bordercolor');
134+
expect(path.style('fill')).toBe(expected.bgColor, msg + ' - bgcolor');
135+
expect(path.style('stroke')).toBe(expected.borderColor, msg + ' - bordercolor');
141136

142137
var text = sel.select('text.nums');
143-
expect(parseInt(text.style('font-size'))).toEqual(expected.fontSize, 'font.size');
144-
expect(text.style('font-family').split(',')[0]).toEqual(expected.fontFamily, 'font.family');
145-
expect(text.style('fill')).toEqual(expected.fontColor, 'font.color');
138+
expect(parseInt(text.style('font-size'))).toBe(expected.fontSize, msg + ' - font.size');
139+
expect(text.style('font-family').split(',')[0]).toBe(expected.fontFamily, msg + ' - font.family');
140+
expect(text.style('fill')).toBe(expected.fontColor, msg + ' - font.color');
146141
}
147142

148143
// returns basic hover/click/unhover runner for one xy position
@@ -157,19 +152,19 @@ describe('Test hover and click interactions', function() {
157152
makeUnhoverFn(gd, pos[0], pos[1]);
158153

159154
return function() {
160-
return wait()
155+
return delay(100)()
161156
.then(_hover)
162157
.then(function(eventData) {
163-
assertEventData(eventData, expected);
164-
assertHoverLabelStyle(d3.select('g.hovertext'), expected);
158+
assertEventData(eventData, expected, opts.msg);
159+
assertHoverLabelStyle(d3.select('g.hovertext'), expected, opts.msg);
165160
})
166161
.then(_click)
167162
.then(function(eventData) {
168-
assertEventData(eventData, expected);
163+
assertEventData(eventData, expected, opts.msg);
169164
})
170165
.then(_unhover)
171166
.then(function(eventData) {
172-
expect(eventData).toEqual('emitted plotly_unhover');
167+
expect(eventData).toBe('emitted plotly_unhover', opts.msg);
173168
});
174169
};
175170
}
@@ -211,6 +206,8 @@ describe('Test hover and click interactions', function() {
211206
fontSize: 20,
212207
fontFamily: 'Arial',
213208
fontColor: 'rgb(255, 255, 0)'
209+
}, {
210+
msg: 'scattergl'
214211
});
215212

216213
Plotly.plot(gd, _mock)
@@ -229,6 +226,8 @@ describe('Test hover and click interactions', function() {
229226
curveNumber: 0,
230227
pointNumber: 33,
231228
noHoverLabel: true
229+
}, {
230+
msg: 'scattergl with hoverinfo'
232231
});
233232

234233
Plotly.plot(gd, _mock)
@@ -255,6 +254,8 @@ describe('Test hover and click interactions', function() {
255254
fontSize: 8,
256255
fontFamily: 'Arial',
257256
fontColor: 'rgb(255, 255, 255)'
257+
}, {
258+
msg: 'pointcloud'
258259
});
259260

260261
Plotly.plot(gd, _mock)
@@ -286,7 +287,8 @@ describe('Test hover and click interactions', function() {
286287
fontFamily: 'Roboto',
287288
fontColor: 'rgb(255, 255, 255)'
288289
}, {
289-
noUnHover: true
290+
noUnHover: true,
291+
msg: 'heatmapgl'
290292
});
291293

292294
Plotly.plot(gd, _mock)
@@ -308,6 +310,8 @@ describe('Test hover and click interactions', function() {
308310
fontSize: 13,
309311
fontFamily: 'Arial',
310312
fontColor: 'rgb(255, 255, 255)'
313+
}, {
314+
msg: 'scattergl before visibility restyle'
311315
});
312316

313317
// after the restyle, autorange changes the y range
@@ -321,6 +325,8 @@ describe('Test hover and click interactions', function() {
321325
fontSize: 13,
322326
fontFamily: 'Arial',
323327
fontColor: 'rgb(68, 68, 68)'
328+
}, {
329+
msg: 'scattergl after visibility restyle'
324330
});
325331

326332
Plotly.plot(gd, _mock)
@@ -349,6 +355,8 @@ describe('Test hover and click interactions', function() {
349355
fontSize: 13,
350356
fontFamily: 'Arial',
351357
fontColor: 'rgb(255, 255, 255)'
358+
}, {
359+
msg: 'scattergl fancy before visibility restyle'
352360
});
353361

354362
// after the restyle, autorange changes the x AND y ranges
@@ -365,6 +373,8 @@ describe('Test hover and click interactions', function() {
365373
fontSize: 13,
366374
fontFamily: 'Arial',
367375
fontColor: 'rgb(68, 68, 68)'
376+
}, {
377+
msg: 'scattergl fancy after visibility restyle'
368378
});
369379

370380
Plotly.plot(gd, _mock)
@@ -395,7 +405,8 @@ describe('Test hover and click interactions', function() {
395405
fontFamily: 'Arial',
396406
fontColor: 'rgb(255, 255, 255)'
397407
}, {
398-
noUnHover: true
408+
noUnHover: true,
409+
msg: 'contourgl'
399410
});
400411

401412
Plotly.plot(gd, _mock)

0 commit comments

Comments
 (0)