-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
ArrayOk hoverinfo fixups #2055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ArrayOk hoverinfo fixups #2055
Changes from 1 commit
4cf489e
90e2de1
c89c895
b24759d
0a77239
85d7061
bc8294d
b637179
294714b
2fe641d
7f2604a
6a44a9a
3931273
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this fn - really nice idea! I find the signature a little unintuitive though... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get annoyed of typing object keys in assertion functions arguments when writing tests. But I guess there's value in being a little more verbose for project-wide custom assertions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and good call about the somewhat useless single-vs-multi line logic. Our There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in 294714b Thoughts? |
||
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).sele 8000 ct('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.'); | ||
} | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - I find that a lot easier, especially as you point out since this lives in a different file from where it gets called.