Skip to content

Commit 294714b

Browse files
committed
improve assertHoverLabelContent API
1 parent b637179 commit 294714b

10 files changed

+213
-192
lines changed

test/jasmine/assets/custom_assertions.js

+42-31
Original file line numberDiff line numberDiff line change
@@ -60,88 +60,99 @@ exports.assertHoverLabelStyle = function(g, expectation, msg, textSelector) {
6060
};
6161

6262
function assertLabelContent(label, expectation, msg) {
63-
var lines = label.selectAll('tspan');
64-
var lineCnt = lines.size();
65-
var expectMultiLine = Array.isArray(expectation);
63+
if(!expectation) expectation = '';
6664

67-
function extract(sel) {
68-
return sel.node() ? sel.html() : null;
69-
}
65+
var lines = label.selectAll('tspan.line');
66+
var content = [];
7067

71-
if(lineCnt > 0) {
72-
if(expectMultiLine) {
73-
expect(lines.size()).toBe(expectation.length, msg + ': # of lines');
74-
lines.each(function(_, i) {
75-
var l = d3.select(this);
76-
expect(extract(l)).toBe(expectation[i], msg + ': tspan line ' + i);
77-
});
78-
} else {
79-
fail('Expected a single-line label, found multiple lines');
68+
function fill(sel) {
69+
if(sel.node()) {
70+
var html = sel.html();
71+
if(html) content.push(html);
8072
}
73+
}
74+
75+
if(lines.size()) {
76+
lines.each(function() { fill(d3.select(this)); });
8177
} else {
82-
if(!expectMultiLine) {
83-
expect(extract(label)).toBe(expectation, msg + ': text content');
84-
} else {
85-
fail('Expected a multi-line label, found single');
86-
}
78+
fill(label);
8779
}
80+
81+
expect(content.join('\n')).toBe(expectation, msg + ': text content');
8882
}
8983

9084
function count(selector) {
9185
return d3.selectAll(selector).size();
9286
}
9387

88+
/**
89+
* @param {object} expectation
90+
* - nums {string || array of strings}
91+
* - name {string || array of strings}
92+
* - axis {string}
93+
* @param {string} msg
94+
*/
9495
exports.assertHoverLabelContent = function(expectation, msg) {
9596
if(!msg) msg = '';
9697

9798
var ptSelector = 'g.hovertext';
98-
var ptExpectation = expectation[0];
9999
var ptMsg = msg + ' point hover label';
100100
var ptCnt = count(ptSelector);
101101

102102
var axSelector = 'g.axistext';
103-
var axExpectation = expectation[1];
104103
var axMsg = 'common axis hover label';
105104
var axCnt = count(axSelector);
106105

107106
if(ptCnt === 1) {
108107
assertLabelContent(
109108
d3.select(ptSelector + '> text.nums'),
110-
ptExpectation[0],
109+
expectation.nums,
111110
ptMsg + ' (nums)'
112111
);
113112
assertLabelContent(
114113
d3.select(ptSelector + '> text.name'),
115-
ptExpectation[1],
114+
expectation.name,
116115
ptMsg + ' (name)'
117116
);
118117
} else if(ptCnt > 1) {
119-
expect(ptCnt).toBe(ptExpectation.length, ptMsg + ' # of visible nodes');
118+
if(!Array.isArray(expectation.nums) || !Array.isArray(expectation.name)) {
119+
fail(ptMsg + ': expecting more than 1 labels.');
120+
}
121+
122+
expect(ptCnt)
123+
.toBe(expectation.name.length, ptMsg + ' # of visible labels');
120124

121125
d3.selectAll(ptSelector).each(function(_, i) {
122126
assertLabelContent(
123127
d3.select(this).select('text.nums'),
124-
ptExpectation[i][0],
128+
expectation.nums[i],
125129
ptMsg + ' (nums ' + i + ')'
126130
);
127131
assertLabelContent(
128132
d3.select(this).select('text.name'),
129-
ptExpectation[i][1],
133+
expectation.name[i],
130134
ptMsg + ' (name ' + i + ')'
131135
);
132136
});
133137
} else {
134-
expect(ptExpectation).toBe(null, ptMsg + ' should not be displayed');
138+
if(expectation.nums) {
139+
fail(ptMsg + ': expecting *nums* labels, did not find any.');
140+
}
141+
if(expectation.name) {
142+
fail(ptMsg + ': expecting *nums* labels, did not find any.');
143+
}
135144
}
136145

137-
if(axCnt > 0) {
146+
if(axCnt) {
138147
assertLabelContent(
139148
d3.select(axSelector + '> text'),
140-
axExpectation,
149+
expectation.axis,
141150
axMsg
142151
);
143152
} else {
144-
expect(axExpectation).toBe(null, axMsg + ' should not be displayed');
153+
if(expectation.axis) {
154+
fail(axMsg + ': expecting label, did not find any.');
155+
}
145156
}
146157
};
147158

test/jasmine/tests/carpet_test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,10 @@ describe('scattercarpet hover labels', function() {
579579

580580
return Plotly.plot(gd, fig).then(function() {
581581
mouseEvent('mousemove', pos[0], pos[1]);
582-
assertHoverLabelContent([content, null]);
582+
assertHoverLabelContent({
583+
nums: content[0].join('\n'),
584+
name: content[1]
585+
});
583586
});
584587
}
585588

test/jasmine/tests/choropleth_test.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@ describe('Test choropleth hover:', function() {
7777

7878
return Plotly.plot(gd, fig).then(function() {
7979
mouseEvent('mousemove', pos[0], pos[1]);
80-
assertHoverLabelContent([content, null]);
81-
assertHoverLabelStyle(d3.select('g.hovertext'), style);
80+
assertHoverLabelContent({
81+
nums: content[0],
82+
name: content[1]
83+
});
84+
assertHoverLabelStyle(
85+
d3.select('g.hovertext'),
86+
style
87+
);
8288
});
8389
}
8490

@@ -88,7 +94,7 @@ describe('Test choropleth hover:', function() {
8894
run(
8995
[400, 160],
9096
fig,
91-
[['RUS', '10', ''], 'trace 1']
97+
['RUS\n10', 'trace 1']
9298
)
9399
.then(done);
94100
});
@@ -130,7 +136,7 @@ describe('Test choropleth hover:', function() {
130136
run(
131137
[400, 160],
132138
fig,
133-
[['RUS', '10', ''], 'trace 1'],
139+
['RUS\n10', 'trace 1'],
134140
{
135141
bgcolor: 'rgb(255, 0, 0)',
136142
bordercolor: 'rgb(0, 128, 0)',

test/jasmine/tests/gl2d_click_test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ describe('Test hover and click interactions', function() {
158158
assertHoverLabelStyle(g, expected, opts.msg);
159159
}
160160
if(expected.label) {
161-
assertHoverLabelContent([expected.label, null]);
161+
assertHoverLabelContent({
162+
nums: expected.label[0],
163+
name: expected.label[1]
164+
});
162165
}
163166
})
164167
.then(_click)

test/jasmine/tests/gl_plot_interact_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Test gl3d plots', function() {
3838
function assertHoverText(xLabel, yLabel, zLabel, textLabel) {
3939
var content = [xLabel, yLabel, zLabel];
4040
if(textLabel) content.push(textLabel);
41-
assertHoverLabelContent([[content, null], null]);
41+
assertHoverLabelContent({nums: content.join('\n')});
4242
}
4343

4444
function assertEventData(x, y, z, curveNumber, pointNumber, extra) {

0 commit comments

Comments
 (0)