Skip to content

Commit beaaf16

Browse files
committed
DRY-up hover-label alignment tests
1 parent 2837ca7 commit beaaf16

File tree

1 file changed

+111
-120
lines changed

1 file changed

+111
-120
lines changed

test/jasmine/tests/hover_label_test.js

+111-120
Original file line numberDiff line numberDiff line change
@@ -1432,143 +1432,134 @@ describe('hover info', function() {
14321432
.catch(failTest)
14331433
.then(done);
14341434
});
1435-
14361435
});
14371436

1438-
function hoverInfoNodes(traceName) {
1439-
var g = d3.selectAll('g.hoverlayer g.hovertext').filter(function() {
1440-
return !d3.select(this).select('[data-unformatted="' + traceName + '"]').empty();
1441-
});
1442-
1443-
return {
1444-
primaryText: g.select('text:not([data-unformatted="' + traceName + '"])').node(),
1445-
primaryBox: g.select('path').node(),
1446-
secondaryText: g.select('[data-unformatted="' + traceName + '"]').node(),
1447-
secondaryBox: g.select('rect').node()
1448-
};
1449-
}
1450-
1451-
function ensureCentered(hoverInfoNodes) {
1452-
expect(hoverInfoNodes.primaryText.getAttribute('text-anchor')).toBe('middle');
1453-
expect(hoverInfoNodes.secondaryText.getAttribute('text-anchor')).toBe('middle');
1454-
return hoverInfoNodes;
1455-
}
1456-
1457-
function assertLabelsInsideBoxes(nodes, msgPrefix) {
1458-
var msgPrefixFmt = msgPrefix ? '[' + msgPrefix + '] ' : '';
1459-
1460-
assertElemInside(nodes.primaryText, nodes.primaryBox,
1461-
msgPrefixFmt + 'Primary text inside box');
1462-
assertElemInside(nodes.secondaryText, nodes.secondaryBox,
1463-
msgPrefixFmt + 'Secondary text inside box');
1464-
}
1465-
1466-
function assertSecondaryRightToPrimaryBox(nodes, msgPrefix) {
1467-
var msgPrefixFmt = msgPrefix ? '[' + msgPrefix + '] ' : '';
1468-
1469-
assertElemRightTo(nodes.secondaryBox, nodes.primaryBox,
1470-
msgPrefixFmt + 'Secondary box right to primary box');
1471-
assertElemTopsAligned(nodes.secondaryBox, nodes.primaryBox,
1472-
msgPrefixFmt + 'Top edges of primary and secondary boxes aligned');
1473-
}
1474-
1475-
describe('centered', function() {
1476-
var trace1 = {
1477-
x: ['giraffes'],
1478-
y: [5],
1479-
name: 'LA Zoo',
1480-
type: 'bar',
1481-
text: ['Way too long hover info!']
1482-
};
1483-
var trace2 = {
1484-
x: ['giraffes'],
1485-
y: [5],
1486-
name: 'SF Zoo',
1487-
type: 'bar',
1488-
text: ['San Francisco']
1489-
};
1490-
var data = [trace1, trace2];
1491-
var layout = {width: 600, height: 300, barmode: 'stack'};
1492-
1437+
describe('alignment while avoiding overlaps:', function() {
14931438
var gd;
14941439

1495-
beforeEach(function(done) {
1496-
gd = createGraphDiv();
1497-
Plotly.plot(gd, data, layout).then(done);
1498-
});
1440+
beforeEach(function() { gd = createGraphDiv(); });
14991441

1500-
it('renders labels inside boxes', function() {
1501-
_hover(gd, 300, 150);
1442+
function hoverInfoNodes(traceName) {
1443+
var g = d3.selectAll('g.hoverlayer g.hovertext').filter(function() {
1444+
return !d3.select(this).select('[data-unformatted="' + traceName + '"]').empty();
1445+
});
15021446

1503-
var nodes = ensureCentered(hoverInfoNodes('LA Zoo'));
1504-
assertLabelsInsideBoxes(nodes);
1505-
});
1447+
return {
1448+
primaryText: g.select('text:not([data-unformatted="' + traceName + '"])').node(),
1449+
primaryBox: g.select('path').node(),
1450+
secondaryText: g.select('[data-unformatted="' + traceName + '"]').node(),
1451+
secondaryBox: g.select('rect').node()
1452+
};
1453+
}
15061454

1507-
it('renders secondary info box right to primary info box', function() {
1508-
_hover(gd, 300, 150);
1455+
function ensureCentered(hoverInfoNodes) {
1456+
expect(hoverInfoNodes.primaryText.getAttribute('text-anchor')).toBe('middle');
1457+
expect(hoverInfoNodes.secondaryText.getAttribute('text-anchor')).toBe('middle');
1458+
return hoverInfoNodes;
1459+
}
15091460

1510-
var nodes = ensureCentered(hoverInfoNodes('LA Zoo'));
1511-
assertSecondaryRightToPrimaryBox(nodes);
1512-
});
1513-
});
1461+
function assertLabelsInsideBoxes(nodes, msgPrefix) {
1462+
var msgPrefixFmt = msgPrefix ? '[' + msgPrefix + '] ' : '';
15141463

1515-
describe('centered', function() {
1516-
var trace1 = {
1517-
x: ['giraffes'],
1518-
y: [5],
1519-
name: 'LA Zoo',
1520-
type: 'bar',
1521-
text: ['Way too long hover info!']
1522-
};
1523-
var trace2 = {
1524-
x: ['giraffes'],
1525-
y: [5],
1526-
name: 'SF Zoo',
1527-
type: 'bar',
1528-
text: ['Way too looooong hover info!']
1529-
};
1530-
var trace3 = {
1531-
x: ['giraffes'],
1532-
y: [5],
1533-
name: 'NY Zoo',
1534-
type: 'bar',
1535-
text: ['New York']
1536-
};
1537-
var data = [trace1, trace2, trace3];
1538-
var layout = {width: 600, height: 300};
1464+
assertElemInside(nodes.primaryText, nodes.primaryBox,
1465+
msgPrefixFmt + 'Primary text inside box');
1466+
assertElemInside(nodes.secondaryText, nodes.secondaryBox,
1467+
msgPrefixFmt + 'Secondary text inside box');
1468+
}
15391469

1540-
var gd;
1470+
function assertSecondaryRightToPrimaryBox(nodes, msgPrefix) {
1471+
var msgPrefixFmt = msgPrefix ? '[' + msgPrefix + '] ' : '';
15411472

1542-
beforeEach(function(done) {
1543-
gd = createGraphDiv();
1544-
Plotly.plot(gd, data, layout).then(done);
1545-
});
1473+
assertElemRightTo(nodes.secondaryBox, nodes.primaryBox,
1474+
msgPrefixFmt + 'Secondary box right to primary box');
1475+
assertElemTopsAligned(nodes.secondaryBox, nodes.primaryBox,
1476+
msgPrefixFmt + 'Top edges of primary and secondary boxes aligned');
1477+
}
15461478

1547-
function calcLineOverlap(minA, maxA, minB, maxB) {
1548-
expect(minA).toBeLessThan(maxA);
1549-
expect(minB).toBeLessThan(maxB);
1479+
it('centered-aligned, should render labels inside boxes', function(done) {
1480+
var trace1 = {
1481+
x: ['giraffes'],
1482+
y: [5],
1483+
name: 'LA Zoo',
1484+
type: 'bar',
1485+
text: ['Way too long hover info!']
1486+
};
1487+
var trace2 = {
1488+
x: ['giraffes'],
1489+
y: [5],
1490+
name: 'SF Zoo',
1491+
type: 'bar',
1492+
text: ['San Francisco']
1493+
};
1494+
var data = [trace1, trace2];
1495+
var layout = {width: 600, height: 300, barmode: 'stack'};
15501496

1551-
var overlap = Math.min(maxA, maxB) - Math.max(minA, minB);
1552-
return Math.max(0, overlap);
1553-
}
1497+
Plotly.plot(gd, data, layout)
1498+
.then(function() { _hover(gd, 300, 150); })
1499+
.then(function() {
1500+
var nodes = ensureCentered(hoverInfoNodes('LA Zoo'));
1501+
assertLabelsInsideBoxes(nodes);
1502+
assertSecondaryRightToPrimaryBox(nodes);
1503+
})
1504+
.catch(failTest)
1505+
.then(done);
1506+
});
15541507

1555-
it('stacks nicely upon each other', function() {
1556-
_hover(gd, 300, 150);
1508+
it('centered-aligned, should stack nicely upon each other', function(done) {
1509+
function calcLineOverlap(minA, maxA, minB, maxB) {
1510+
expect(minA).toBeLessThan(maxA);
1511+
expect(minB).toBeLessThan(maxB);
15571512

1558-
var nodesLA = ensureCentered(hoverInfoNodes('LA Zoo'));
1559-
var nodesSF = ensureCentered(hoverInfoNodes('SF Zoo'));
1513+
var overlap = Math.min(maxA, maxB) - Math.max(minA, minB);
1514+
return Math.max(0, overlap);
1515+
}
15601516

1561-
// Ensure layout correct
1562-
assertLabelsInsideBoxes(nodesLA, 'LA Zoo');
1563-
assertLabelsInsideBoxes(nodesSF, 'SF Zoo');
1564-
assertSecondaryRightToPrimaryBox(nodesLA, 'LA Zoo');
1565-
assertSecondaryRightToPrimaryBox(nodesSF, 'SF Zoo');
1517+
var trace1 = {
1518+
x: ['giraffes'],
1519+
y: [5],
1520+
name: 'LA Zoo',
1521+
type: 'bar',
1522+
text: ['Way too long hover info!']
1523+
};
1524+
var trace2 = {
1525+
x: ['giraffes'],
1526+
y: [5],
1527+
name: 'SF Zoo',
1528+
type: 'bar',
1529+
text: ['Way too looooong hover info!']
1530+
};
1531+
var trace3 = {
1532+
x: ['giraffes'],
1533+
y: [5],
1534+
name: 'NY Zoo',
1535+
type: 'bar',
1536+
text: ['New York']
1537+
};
1538+
var data = [trace1, trace2, trace3];
1539+
var layout = {width: 600, height: 300};
15661540

1567-
// Ensure stacking, finally
1568-
var boxLABB = nodesLA.primaryBox.getBoundingClientRect();
1569-
var boxSFBB = nodesSF.primaryBox.getBoundingClientRect();
1570-
expect(calcLineOverlap(boxLABB.top, boxLABB.bottom, boxSFBB.top, boxSFBB.bottom))
1571-
.toBeWithin(0, 1); // Be robust against floating point arithmetic and subtle future layout changes
1541+
Plotly.plot(gd, data, layout)
1542+
.then(function() { _hover(gd, 300, 150); })
1543+
.then(function() {
1544+
var nodesLA = ensureCentered(hoverInfoNodes('LA Zoo'));
1545+
var nodesSF = ensureCentered(hoverInfoNodes('SF Zoo'));
1546+
1547+
// Ensure layout correct
1548+
assertLabelsInsideBoxes(nodesLA, 'LA Zoo');
1549+
assertLabelsInsideBoxes(nodesSF, 'SF Zoo');
1550+
assertSecondaryRightToPrimaryBox(nodesLA, 'LA Zoo');
1551+
assertSecondaryRightToPrimaryBox(nodesSF, 'SF Zoo');
1552+
1553+
// Ensure stacking, finally
1554+
var boxLABB = nodesLA.primaryBox.getBoundingClientRect();
1555+
var boxSFBB = nodesSF.primaryBox.getBoundingClientRect();
1556+
1557+
// Be robust against floating point arithmetic and subtle future layout changes
1558+
expect(calcLineOverlap(boxLABB.top, boxLABB.bottom, boxSFBB.top, boxSFBB.bottom))
1559+
.toBeWithin(0, 1);
1560+
})
1561+
.catch(failTest)
1562+
.then(done);
15721563
});
15731564
});
15741565

0 commit comments

Comments
 (0)