Skip to content

Commit

Permalink
improve assertHoverLabelContent API
Browse files Browse the repository at this point in the history
  • Loading branch information
etpinard committed Oct 4, 2017
1 parent b637179 commit 294714b
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 192 deletions.
73 changes: 42 additions & 31 deletions test/jasmine/assets/custom_assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,88 +60,99 @@ exports.assertHoverLabelStyle = function(g, expectation, msg, textSelector) {
};

function assertLabelContent(label, expectation, msg) {
var lines = label.selectAll('tspan');
var lineCnt = lines.size();
var expectMultiLine = Array.isArray(expectation);
if(!expectation) expectation = '';

function extract(sel) {
return sel.node() ? sel.html() : null;
}
var lines = label.selectAll('tspan.line');
var content = [];

if(lineCnt > 0) {
if(expectMultiLine) {
expect(lines.size()).toBe(expectation.length, msg + ': # of lines');
lines.each(function(_, i) {
var l = d3.select(this);
expect(extract(l)).toBe(expectation[i], msg + ': tspan line ' + i);
});
} else {
fail('Expected a single-line label, found multiple lines');
function fill(sel) {
if(sel.node()) {
var html = sel.html();
if(html) content.push(html);
}
}

if(lines.size()) {
lines.each(function() { fill(d3.select(this)); });
} else {
if(!expectMultiLine) {
expect(extract(label)).toBe(expectation, msg + ': text content');
} else {
fail('Expected a multi-line label, found single');
}
fill(label);
}

expect(content.join('\n')).toBe(expectation, msg + ': text content');
}

function count(selector) {
return d3.selectAll(selector).size();
}

/**
* @param {object} expectation
* - nums {string || array of strings}
* - name {string || array of strings}
* - axis {string}
* @param {string} msg
*/
exports.assertHoverLabelContent = function(expectation, msg) {
if(!msg) msg = '';

var ptSelector = 'g.hovertext';
var ptExpectation = expectation[0];
var ptMsg = msg + ' point hover label';
var ptCnt = count(ptSelector);

var axSelector = 'g.axistext';
var axExpectation = expectation[1];
var axMsg = 'common axis hover label';
var axCnt = count(axSelector);

if(ptCnt === 1) {
assertLabelContent(
d3.select(ptSelector + '> text.nums'),
ptExpectation[0],
expectation.nums,
ptMsg + ' (nums)'
);
assertLabelContent(
d3.select(ptSelector + '> text.name'),
ptExpectation[1],
expectation.name,
ptMsg + ' (name)'
);
} else if(ptCnt > 1) {
expect(ptCnt).toBe(ptExpectation.length, ptMsg + ' # of visible nodes');
if(!Array.isArray(expectation.nums) || !Array.isArray(expectation.name)) {
fail(ptMsg + ': expecting more than 1 labels.');
}

expect(ptCnt)
.toBe(expectation.name.length, ptMsg + ' # of visible labels');

d3.selectAll(ptSelector).each(function(_, i) {
assertLabelContent(
d3.select(this).select('text.nums'),
ptExpectation[i][0],
expectation.nums[i],
ptMsg + ' (nums ' + i + ')'
);
assertLabelContent(
d3.select(this).select('text.name'),
ptExpectation[i][1],
expectation.name[i],
ptMsg + ' (name ' + i + ')'
);
});
} else {
expect(ptExpectation).toBe(null, ptMsg + ' should not be displayed');
if(expectation.nums) {
fail(ptMsg + ': expecting *nums* labels, did not find any.');
}
if(expectation.name) {
fail(ptMsg + ': expecting *nums* labels, did not find any.');
}
}

if(axCnt > 0) {
if(axCnt) {
assertLabelContent(
d3.select(axSelector + '> text'),
axExpectation,
expectation.axis,
axMsg
);
} else {
expect(axExpectation).toBe(null, axMsg + ' should not be displayed');
if(expectation.axis) {
fail(axMsg + ': expecting label, did not find any.');
}
}
};

Expand Down
5 changes: 4 additions & 1 deletion test/jasmine/tests/carpet_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,10 @@ describe('scattercarpet hover labels', function() {

return Plotly.plot(gd, fig).then(function() {
mouseEvent('mousemove', pos[0], pos[1]);
assertHoverLabelContent([content, null]);
assertHoverLabelContent({
nums: content[0].join('\n'),
name: content[1]
});
});
}

Expand Down
14 changes: 10 additions & 4 deletions test/jasmine/tests/choropleth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ describe('Test choropleth hover:', function() {

return Plotly.plot(gd, fig).then(function() {
mouseEvent('mousemove', pos[0], pos[1]);
assertHoverLabelContent([content, null]);
assertHoverLabelStyle(d3.select('g.hovertext'), style);
assertHoverLabelContent({
nums: content[0],
name: content[1]
});
assertHoverLabelStyle(
d3.select('g.hovertext'),
style
);
});
}

Expand All @@ -88,7 +94,7 @@ describe('Test choropleth hover:', function() {
run(
[400, 160],
fig,
[['RUS', '10', ''], 'trace 1']
['RUS\n10', 'trace 1']
)
.then(done);
});
Expand Down Expand Up @@ -130,7 +136,7 @@ describe('Test choropleth hover:', function() {
run(
[400, 160],
fig,
[['RUS', '10', ''], 'trace 1'],
['RUS\n10', 'trace 1'],
{
bgcolor: 'rgb(255, 0, 0)',
bordercolor: 'rgb(0, 128, 0)',
Expand Down
5 changes: 4 additions & 1 deletion test/jasmine/tests/gl2d_click_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ describe('Test hover and click interactions', function() {
assertHoverLabelStyle(g, expected, opts.msg);
}
if(expected.label) {
assertHoverLabelContent([expected.label, null]);
assertHoverLabelContent({
nums: expected.label[0],
name: expected.label[1]
});
}
})
.then(_click)
Expand Down
2 changes: 1 addition & 1 deletion test/jasmine/tests/gl_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Test gl3d plots', function() {
function assertHoverText(xLabel, yLabel, zLabel, textLabel) {
var content = [xLabel, yLabel, zLabel];
if(textLabel) content.push(textLabel);
assertHoverLabelContent([[content, null], null]);
assertHoverLabelContent({nums: content.join('\n')});
}

function assertEventData(x, y, z, curveNumber, pointNumber, extra) {
Expand Down
Loading

0 comments on commit 294714b

Please sign in to comment.