Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2c8c04

Browse files
committed
simplify hover_label_test with more clearThrottle
1 parent ed19b7c commit e2c8c04

File tree

1 file changed

+64
-81
lines changed

1 file changed

+64
-81
lines changed

test/jasmine/tests/hover_label_test.js

Lines changed: 64 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,34 +1308,28 @@ describe('hover on fill', function() {
13081308
afterEach(destroyGraphDiv);
13091309

13101310
function assertLabelsCorrect(mousePos, labelPos, labelText) {
1311-
return new Promise(function(resolve) {
1312-
Lib.clearThrottle();
1313-
mouseEvent('mousemove', mousePos[0], mousePos[1]);
1314-
1315-
var hoverText = d3.selectAll('g.hovertext');
1316-
expect(hoverText.size()).toEqual(1);
1317-
expect(hoverText.text()).toEqual(labelText);
1311+
Lib.clearThrottle();
1312+
mouseEvent('mousemove', mousePos[0], mousePos[1]);
13181313

1319-
var transformParts = hoverText.attr('transform').split('(');
1320-
expect(transformParts[0]).toEqual('translate');
1321-
var transformCoords = transformParts[1].split(')')[0].split(',');
1322-
expect(+transformCoords[0]).toBeCloseTo(labelPos[0], -1.2, labelText + ':x');
1323-
expect(+transformCoords[1]).toBeCloseTo(labelPos[1], -1.2, labelText + ':y');
1314+
var hoverText = d3.selectAll('g.hovertext');
1315+
expect(hoverText.size()).toEqual(1);
1316+
expect(hoverText.text()).toEqual(labelText);
13241317

1325-
resolve();
1326-
});
1318+
var transformParts = hoverText.attr('transform').split('(');
1319+
expect(transformParts[0]).toEqual('translate');
1320+
var transformCoords = transformParts[1].split(')')[0].split(',');
1321+
expect(+transformCoords[0]).toBeCloseTo(labelPos[0], -1.2, labelText + ':x');
1322+
expect(+transformCoords[1]).toBeCloseTo(labelPos[1], -1.2, labelText + ':y');
13271323
}
13281324

13291325
it('should always show one label in the right place', function(done) {
13301326
var mock = Lib.extendDeep({}, require('@mocks/scatter_fill_self_next.json'));
13311327
mock.data.forEach(function(trace) { trace.hoveron = 'fills'; });
13321328

13331329
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(function() {
1334-
return assertLabelsCorrect([242, 142], [252, 133.8], 'trace 2');
1335-
}).then(function() {
1336-
return assertLabelsCorrect([242, 292], [233, 210], 'trace 1');
1337-
}).then(function() {
1338-
return assertLabelsCorrect([147, 252], [158.925, 248.1], 'trace 0');
1330+
assertLabelsCorrect([242, 142], [252, 133.8], 'trace 2');
1331+
assertLabelsCorrect([242, 292], [233, 210], 'trace 1');
1332+
assertLabelsCorrect([147, 252], [158.925, 248.1], 'trace 0');
13391333
}).then(done);
13401334
});
13411335

@@ -1353,17 +1347,16 @@ describe('hover on fill', function() {
13531347
margin: {l: 50, t: 50, r: 50, b: 50}
13541348
})
13551349
.then(function() {
1356-
return assertLabelsCorrect([200, 200], [73.75, 250], 'trace 0');
1357-
})
1358-
.then(function() {
1350+
assertLabelsCorrect([200, 200], [73.75, 250], 'trace 0');
1351+
13591352
return Plotly.restyle(gd, {
13601353
x: [[6, 7, 8, 7]],
13611354
y: [[5, 4, 5, 6]]
13621355
});
13631356
})
13641357
.then(function() {
13651358
// gives same results w/o closing point
1366-
return assertLabelsCorrect([200, 200], [73.75, 250], 'trace 0');
1359+
assertLabelsCorrect([200, 200], [73.75, 250], 'trace 0');
13671360
})
13681361
.catch(fail)
13691362
.then(done);
@@ -1378,16 +1371,14 @@ describe('hover on fill', function() {
13781371

13791372
// hover over a point when that's closest, even if you're over
13801373
// a fill, because by default we have hoveron='points+fills'
1381-
return assertLabelsCorrect([237, 150], [240.0, 144],
1374+
assertLabelsCorrect([237, 150], [240.0, 144],
13821375
'trace 2Component A: 0.8Component B: 0.1Component C: 0.1');
1383-
}).then(function() {
1376+
13841377
// the rest are hovers over fills
1385-
return assertLabelsCorrect([237, 170], [247.7, 166], 'trace 2');
1386-
}).then(function() {
1387-
return assertLabelsCorrect([237, 218], [266.75, 265], 'trace 1');
1388-
}).then(function() {
1389-
return assertLabelsCorrect([237, 240], [247.7, 254], 'trace 0');
1390-
}).then(function() {
1378+
assertLabelsCorrect([237, 170], [247.7, 166], 'trace 2');
1379+
assertLabelsCorrect([237, 218], [266.75, 265], 'trace 1');
1380+
assertLabelsCorrect([237, 240], [247.7, 254], 'trace 0');
1381+
13911382
// zoom in to test clipping of large out-of-viewport shapes
13921383
return Plotly.relayout(gd, {
13931384
'ternary.aaxis.min': 0.5,
@@ -1396,13 +1387,13 @@ describe('hover on fill', function() {
13961387
}).then(function() {
13971388
// this particular one has a hover label disconnected from the shape itself
13981389
// so if we ever fix this, the test will have to be fixed too.
1399-
return assertLabelsCorrect([295, 218], [275.1, 166], 'trace 2');
1400-
}).then(function() {
1390+
assertLabelsCorrect([295, 218], [275.1, 166], 'trace 2');
1391+
14011392
// trigger an autoscale redraw, which goes through dragElement
14021393
return doubleClick(237, 251);
14031394
}).then(function() {
14041395
// then make sure we can still select a *different* item afterward
1405-
return assertLabelsCorrect([237, 218], [266.75, 265], 'trace 1');
1396+
assertLabelsCorrect([237, 218], [266.75, 265], 'trace 1');
14061397
})
14071398
.catch(fail)
14081399
.then(done);
@@ -1421,14 +1412,14 @@ describe('hover on fill', function() {
14211412

14221413
// hover over a point when that's closest, even if you're over
14231414
// a fill, because by default we have hoveron='points+fills'
1424-
return assertLabelsCorrect([237, 150], [240.0, 144],
1415+
assertLabelsCorrect([237, 150], [240.0, 144],
14251416
'trace 2Component A: 0.8Component B: 0.1Component C: 0.1');
1426-
}).then(function() {
1417+
14271418
// hovers over fills
1428-
return assertLabelsCorrect([237, 170], [247.7, 166], 'trace 2');
1429-
}).then(function() {
1419+
assertLabelsCorrect([237, 170], [247.7, 166], 'trace 2');
1420+
14301421
// hover on the cartesian trace in the corner
1431-
return assertLabelsCorrect([363, 122], [363, 122], 'trace 38');
1422+
assertLabelsCorrect([363, 122], [363, 122], 'trace 38');
14321423
})
14331424
.catch(fail)
14341425
.then(done);
@@ -1441,29 +1432,25 @@ describe('hover updates', function() {
14411432
afterEach(destroyGraphDiv);
14421433

14431434
function assertLabelsCorrect(mousePos, labelPos, labelText, msg) {
1444-
return new Promise(function(resolve) {
1445-
if(mousePos) {
1446-
mouseEvent('mousemove', mousePos[0], mousePos[1]);
1447-
}
1435+
Lib.clearThrottle();
14481436

1449-
setTimeout(function() {
1450-
var hoverText = d3.selectAll('g.hovertext');
1451-
if(labelPos) {
1452-
expect(hoverText.size()).toBe(1, msg);
1453-
expect(hoverText.text()).toBe(labelText, msg);
1454-
1455-
var transformParts = hoverText.attr('transform').split('(');
1456-
expect(transformParts[0]).toBe('translate', msg);
1457-
var transformCoords = transformParts[1].split(')')[0].split(',');
1458-
expect(+transformCoords[0]).toBeCloseTo(labelPos[0], -1, labelText + ':x ' + msg);
1459-
expect(+transformCoords[1]).toBeCloseTo(labelPos[1], -1, labelText + ':y ' + msg);
1460-
} else {
1461-
expect(hoverText.size()).toEqual(0);
1462-
}
1437+
if(mousePos) {
1438+
mouseEvent('mousemove', mousePos[0], mousePos[1]);
1439+
}
14631440

1464-
resolve();
1465-
}, HOVERMINTIME);
1466-
});
1441+
var hoverText = d3.selectAll('g.hovertext');
1442+
if(labelPos) {
1443+
expect(hoverText.size()).toBe(1, msg);
1444+
expect(hoverText.text()).toBe(labelText, msg);
1445+
1446+
var transformParts = hoverText.attr('transform').split('(');
1447+
expect(transformParts[0]).toBe('translate', msg);
1448+
var transformCoords = transformParts[1].split(')')[0].split(',');
1449+
expect(+transformCoords[0]).toBeCloseTo(labelPos[0], -1, labelText + ':x ' + msg);
1450+
expect(+transformCoords[1]).toBeCloseTo(labelPos[1], -1, labelText + ':y ' + msg);
1451+
} else {
1452+
expect(hoverText.size()).toEqual(0);
1453+
}
14671454
}
14681455

14691456
it('should update the labels on animation', function(done) {
@@ -1484,28 +1471,30 @@ describe('hover updates', function() {
14841471
var gd = createGraphDiv();
14851472
Plotly.plot(gd, mock).then(function() {
14861473
// The label text gets concatenated together when queried. Such is life.
1487-
return assertLabelsCorrect([100, 100], [103, 100], 'trace 00.5', 'animation/update 0');
1474+
assertLabelsCorrect([100, 100], [103, 100], 'trace 00.5', 'animation/update 0');
14881475
}).then(function() {
14891476
return Plotly.animate(gd, [{
14901477
data: [{x: [0], y: [0]}, {x: [0.5], y: [0.5]}],
14911478
traces: [0, 1],
14921479
}], {frame: {redraw: false, duration: 0}});
1493-
}).then(function() {
1480+
})
1481+
.then(delay(HOVERMINTIME))
1482+
.then(function() {
14941483
// No mouse event this time. Just change the data and check the label.
14951484
// Ditto on concatenation. This is "trace 1" + "0.5"
1496-
return assertLabelsCorrect(null, [103, 100], 'trace 10.5', 'animation/update 1');
1497-
}).then(function() {
1485+
assertLabelsCorrect(null, [103, 100], 'trace 10.5', 'animation/update 1');
1486+
14981487
// Restyle to move the point out of the window:
14991488
return Plotly.relayout(gd, {'xaxis.range': [2, 3]});
15001489
}).then(function() {
15011490
// Assert label removed:
1502-
return assertLabelsCorrect(null, null, null, 'animation/update 2');
1503-
}).then(function() {
1491+
assertLabelsCorrect(null, null, null, 'animation/update 2');
1492+
15041493
// Move back to the original xaxis range:
15051494
return Plotly.relayout(gd, {'xaxis.range': [0, 1]});
15061495
}).then(function() {
15071496
// Assert label restored:
1508-
return assertLabelsCorrect(null, [103, 100], 'trace 10.5', 'animation/update 3');
1497+
assertLabelsCorrect(null, [103, 100], 'trace 10.5', 'animation/update 3');
15091498
}).catch(fail).then(done);
15101499
});
15111500

@@ -1514,12 +1503,8 @@ describe('hover updates', function() {
15141503
var colors0 = ['#000000', '#000000', '#000000', '#000000', '#000000', '#000000', '#000000'];
15151504

15161505
function unhover() {
1517-
return new Promise(function(resolve) {
1518-
mouseEvent('mousemove', 394, 285);
1519-
setTimeout(function() {
1520-
resolve();
1521-
}, HOVERMINTIME);
1522-
});
1506+
Lib.clearThrottle();
1507+
mouseEvent('mousemove', 394, 285);
15231508
}
15241509

15251510
var hoverCnt = 0;
@@ -1549,17 +1534,15 @@ describe('hover updates', function() {
15491534
Plotly.restyle(gd, 'marker.color', [colors0.slice()]);
15501535
});
15511536

1552-
return assertLabelsCorrect([351, 251], [358, 272], '2', 'events 0');
1553-
})
1554-
.then(unhover)
1555-
.then(function() {
1537+
assertLabelsCorrect([351, 251], [358, 272], '2', 'events 0');
1538+
1539+
unhover();
15561540
expect(hoverCnt).toEqual(1);
15571541
expect(unHoverCnt).toEqual(1);
15581542

1559-
return assertLabelsCorrect([420, 100], [435, 198], '3', 'events 1');
1560-
})
1561-
.then(unhover)
1562-
.then(function() {
1543+
assertLabelsCorrect([420, 100], [435, 198], '3', 'events 1');
1544+
1545+
unhover();
15631546
expect(hoverCnt).toEqual(2);
15641547
expect(unHoverCnt).toEqual(2);
15651548
})

0 commit comments

Comments
 (0)