Skip to content

Commit 92b606b

Browse files
committed
revamp hover spike tests
- assert number of spike line visible as well as their position attributes
1 parent d306612 commit 92b606b

File tree

1 file changed

+79
-28
lines changed

1 file changed

+79
-28
lines changed

test/jasmine/tests/hover_spikeline_test.js

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,106 @@ var Lib = require('@src/lib');
77
var fail = require('../assets/fail_test');
88
var createGraphDiv = require('../assets/create_graph_div');
99
var destroyGraphDiv = require('../assets/destroy_graph_div');
10+
var customMatchers = require('../assets/custom_matchers');
1011

1112
describe('spikeline', function() {
1213
'use strict';
1314

14-
var mock = require('@mocks/19.json');
15+
beforeAll(function() {
16+
jasmine.addMatchers(customMatchers);
17+
});
1518

1619
afterEach(destroyGraphDiv);
1720

1821
describe('hover', function() {
19-
var mockCopy = Lib.extendDeep({}, mock);
2022
var gd;
2123

22-
mockCopy.layout.xaxis.showspikes = true;
23-
mockCopy.layout.xaxis.spikemode = 'toaxis';
24-
mockCopy.layout.yaxis.showspikes = true;
25-
mockCopy.layout.yaxis.spikemode = 'toaxis+marker';
26-
mockCopy.layout.xaxis2.showspikes = true;
27-
mockCopy.layout.xaxis2.spikemode = 'toaxis';
28-
mockCopy.layout.hovermode = 'closest';
24+
function makeMock() {
25+
var _mock = Lib.extendDeep({}, require('@mocks/19.json'));
26+
_mock.layout.xaxis.showspikes = true;
27+
_mock.layout.xaxis.spikemode = 'toaxis';
28+
_mock.layout.yaxis.showspikes = true;
29+
_mock.layout.yaxis.spikemode = 'toaxis+marker';
30+
_mock.layout.xaxis2.showspikes = true;
31+
_mock.layout.xaxis2.spikemode = 'toaxis';
32+
_mock.layout.hovermode = 'closest';
33+
return _mock;
34+
}
35+
36+
function _hover(evt, subplot) {
37+
Fx.hover(gd, evt, subplot);
38+
delete gd._lastHoverTime;
39+
}
40+
41+
function _assert(lineExpect, circleExpect) {
42+
var lines = d3.selectAll('line.spikeline');
43+
var circles = d3.selectAll('circle.spikeline');
44+
45+
expect(lines.size()).toBe(lineExpect.length, '# of line nodes');
46+
expect(circles.size()).toBe(circleExpect.length, '# of circle nodes');
47+
48+
lines.each(function(_, i) {
49+
var sel = d3.select(this);
50+
['x1', 'y1', 'x2', 'y2'].forEach(function(d, j) {
51+
expect(sel.attr(d))
52+
.toBeWithin(lineExpect[i][j], 1, 'line ' + i + ' attr ' + d);
53+
});
54+
});
2955

30-
beforeEach(function(done) {
56+
circles.each(function(_, i) {
57+
var sel = d3.select(this);
58+
['cx', 'cy'].forEach(function(d, j) {
59+
expect(sel.attr(d))
60+
.toBeWithin(circleExpect[i][j], 1, 'circle ' + i + ' attr ' + d);
61+
});
62+
});
63+
}
64+
65+
it('draws lines and markers on enabled axes', function(done) {
3166
gd = createGraphDiv();
32-
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
33-
});
67+
var _mock = makeMock();
3468

35-
it('draws lines and markers on enabled axes', function() {
36-
Fx.hover('graph', {xval: 2, yval: 3}, 'xy');
37-
expect(d3.selectAll('line.spikeline').size()).toEqual(4);
38-
expect(d3.selectAll('circle.spikeline').size()).toEqual(1);
69+
Plotly.plot(gd, _mock).then(function() {
70+
_hover({xval: 2, yval: 3}, 'xy');
71+
_assert(
72+
[[80, 250, 557, 250], [80, 250, 557, 250], [557, 401, 557, 250], [557, 401, 557, 250]],
73+
[[83, 250]]
74+
);
75+
})
76+
.then(function() {
77+
_hover({xval: 30, yval: 40}, 'x2y2');
78+
_assert(
79+
[[820, 220, 820, 167], [820, 220, 820, 167]],
80+
[]
81+
);
82+
})
83+
.catch(fail)
84+
.then(done);
3985
});
4086

4187
it('draws lines and markers on enabled axes w/o tick labels', function(done) {
42-
Plotly.relayout(gd, {
43-
'xaxis.showticklabels': false,
44-
'yaxis.showticklabels': false
88+
gd = createGraphDiv();
89+
var _mock = makeMock();
90+
91+
_mock.layout.xaxis.showticklabels = false;
92+
_mock.layout.yaxis.showticklabels = false;
93+
94+
Plotly.plot(gd, _mock).then(function() {
95+
_hover({xval: 2, yval: 3}, 'xy');
96+
_assert(
97+
[[80, 250, 557, 250], [80, 250, 557, 250], [557, 401, 557, 250], [557, 401, 557, 250]],
98+
[[83, 250]]
99+
);
45100
})
46101
.then(function() {
47-
Fx.hover('graph', {xval: 2, yval: 3}, 'xy');
48-
expect(d3.selectAll('line.spikeline').size()).toEqual(4);
49-
expect(d3.selectAll('circle.spikeline').size()).toEqual(1);
102+
_hover({xval: 30, yval: 40}, 'x2y2');
103+
_assert(
104+
[[820, 220, 820, 167], [820, 220, 820, 167]],
105+
[]
106+
);
50107
})
51108
.catch(fail)
52109
.then(done);
53110
});
54-
55-
it('doesn\'t draw lines and markers on disabled axes', function() {
56-
Fx.hover('graph', {xval: 30, yval: 40}, 'x2y2');
57-
expect(d3.selectAll('line.spikeline').size()).toEqual(2);
58-
expect(d3.selectAll('circle.spikeline').size()).toEqual(0);
59-
});
60111
});
61112
});

0 commit comments

Comments
 (0)