Skip to content

Commit

Permalink
Added tests for hoveron fills to scatter jasmine test suite.
Browse files Browse the repository at this point in the history
The tests rely on scatter mocks `scatter_fill_self_next`
and `scatter_fill_corner_cases` and test for a number of points
that are currently not correctly detected when hovered over
for tonexty and tozeroy fills.
  • Loading branch information
lumip committed Jan 22, 2024
1 parent 620a56b commit 4628741
Showing 1 changed file with 170 additions and 0 deletions.
170 changes: 170 additions & 0 deletions test/jasmine/tests/scatter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,176 @@ describe('scatter hoverPoints', function() {
});
});

describe('scatter hoverFills', function() {
afterEach(destroyGraphDiv);

function _hover(gd, xval, yval, hovermode, subplotId) {
return gd._fullData.map(function(trace, i) {
var cd = gd.calcdata[i];
var subplot = gd._fullLayout._plots[subplotId];

var out = Scatter.hoverPoints({
index: false,
distance: 20,
cd: cd,
trace: trace,
xa: subplot.xaxis,
ya: subplot.yaxis
}, xval, yval, hovermode);

return Array.isArray(out) ? out[0] : null;
});
}

it('should correctly detect the fill that is hovered over for self and next fills', function(done) {
var gd = createGraphDiv();
var mock = Lib.extendDeep({}, require('../../image/mocks/scatter_fill_self_next'));

var testPoints = [
[[2, 2.9], [2, 2], [1.1, 2], [5.99, 3.01], [4.6, 3.5]],
[[2, 3.1], [-0.2, 1.1], [5, 2.99], [7, 2], [1.2, 5.1]],
[[6, 5], [7, 6], [8, 5], [7, 5], [6.7, 5.3]]
];

Plotly.newPlot(gd, mock).then(function() {
return Plotly.restyle(gd, 'hoveron', 'fills');
})
.then(function() {
for(var i = 0; i < testPoints.length; i++) {
for(var j = 0; j < testPoints[i].length; j++) {
var testCoords = testPoints[i][j];
var pts = _hover(gd, testCoords[0], testCoords[1], 'x', 'xy');
expect(pts[i]).toBeTruthy(
'correct trace not detected ' + testCoords.join(',') + ', should be ' + i
);
for(var k = 0; k < pts.length; k++) {
var traceId = (i + 1) % pts.length;
expect(pts[traceId]).toBeFalsy(
'wrong trace detected ' + testCoords.join(',') + '; got ' +
traceId + ' but should be ' + i
);
}
}
}
})
.then(done, done.fail);
});

it('should correctly detect the fill that is hovered over for tozeroy fills', function(done) {
var gd = createGraphDiv();
var mock = Lib.extendDeep({}, require('../../image/mocks/scatter_fill_corner_cases'));

var traceOffset = 0;

var testPoints = [ // all the following points should be in fill region of corresponding tozeroy traces 0-4
[[1.5, 1.24], [1.5, 1.06]], // single point has "fill" along line to zero
[[0.1, 0.9], [0.1, 0.8], [1.5, 0.9], [1.5, 1.04], [2, 0.8], [2, 1.09], [3, 0.8]],
[[0.1, 0.75], [0.1, 0.61], [1.01, 0.501], [1.5, 0.8], [1.5, 0.55], [2, 0.74], [2, 0.55], [3, 0.74], [3, 0.51]],
[[0.1, 0.599], [0.1, 0.5], [0.1, 0.3], [0.99, 0.59], [1, 0.49], [1, 0.36], [1.5, 0.26], [2, 0.49], [2, 0.16], [3, 0.49], [3, 0.26]],
[[0.1, 0.25], [0.1, 0.1], [1, 0.34], [1.5, 0.24], [2, 0.14], [3, 0.24], [3, 0.1]],
];

var outsidePoints = [ // all these should not result in a hover detection, for any trace
[1, 1.1], [2, 1.14],
];

Plotly.newPlot(gd, mock).then(function() {
return Plotly.restyle(gd, 'hoveron', 'fills');
})
.then(function() {
var testCoords, pts;
var i, j, k;
for(i = 0; i < testPoints.length; i++) {
for(j = 0; j < testPoints[i].length; j++) {
testCoords = testPoints[i][j];
pts = _hover(gd, testCoords[0], testCoords[1], 'x', 'xy');
expect(pts[traceOffset + i]).toBeTruthy(
'correct trace not detected ' + testCoords.join(',') + ', should be ' + (traceOffset + i)
);

// since all polygons do extend to the zero axis, many points will be detected by the
// correct trace and previous ones, but a point should not be detected as hover points
// by any trace defined later than the correct trace!
// (in actual hover detection, the real _hover takes care of the overlap with previous traces
// so that is not an issue in practice)
for(k = i + 1; k < testPoints.length; k++) {
var traceId = traceOffset + k;
expect(pts[traceId]).toBeFalsy(
'wrong trace detected ' + testCoords.join(',') + '; got ' +
traceId + ' but should be ' + (traceOffset + i)
);
}
}
}

for(j = 0; j < outsidePoints.length; j++) {
testCoords = outsidePoints[j];
pts = _hover(gd, testCoords[0], testCoords[1], 'x', 'xy');
for(k = 0; k < testPoints.length; k++) {
expect(pts[i]).toBeFalsy(
'trace detected for outside point ' + testCoords.join(',') + ', got ' + (traceOffset + k)
);
}
}
})
.then(done, done.fail);
});


it('should correctly detect the fill that is hovered over for tonexty fills', function(done) {
var gd = createGraphDiv();
var mock = Lib.extendDeep({}, require('../../image/mocks/scatter_fill_corner_cases'));

var traceOffset = 10;

var testPoints = [ // all the following points should be in fill region of corresponding tonexty traces 10-14
[],
[[1, 1.1], [1.5, 1.24], [1.5, 1.06], [2, 1.14]],
[[0.1, 0.9], [0.1, 0.8], [1.5, 0.9], [1.5, 1.04], [2, 0.8], [2, 1.09], [3, 0.8]],
[[0.1, 0.75], [0.1, 0.61], [1.01, 0.501], [1.5, 0.8], [1.5, 0.55], [2, 0.74], [2, 0.55], [3, 0.74], [3, 0.51]],
[[0.1, 0.599], [0.1, 0.5], [0.1, 0.3], [0.99, 0.59], [1, 0.49], [1, 0.36], [1.5, 0.26], [2, 0.49], [2, 0.16], [3, 0.49], [3, 0.26]],
];
var outsidePoints = [ // all these should not result in a hover detection, for any trace
[0.1, 0.25], [0.1, 0.1], [1, 0.34], [1.5, 0.24], [2, 0.14], [3, 0.24], [3, 0.1], [0.5, 1.15], [2.5, 1.15],
];

Plotly.newPlot(gd, mock).then(function() {
return Plotly.restyle(gd, 'hoveron', 'fills');
})
.then(function() {
var testCoords, pts;
var i, j, k;
for(i = 0; i < testPoints.length; i++) {
for(j = 0; j < testPoints[i].length; j++) {
testCoords = testPoints[i][j];
pts = _hover(gd, testCoords[0], testCoords[1], 'x', 'xy2');
expect(pts[traceOffset + i]).toBeTruthy(
'correct trace not detected ' + testCoords.join(',') + ', should be ' + (traceOffset + i)
);

for(k = 1; k < testPoints.length; k++) {
var traceId = traceOffset + ((i + k) % testPoints.length);
expect(pts[traceId]).toBeFalsy(
'wrong trace detected ' + testCoords.join(',') + '; got ' +
traceId + ' but should be ' + (traceOffset + i)
);
}
}
}
for(j = 0; j < outsidePoints.length; j++) {
testCoords = outsidePoints[j];
pts = _hover(gd, testCoords[0], testCoords[1], 'x', 'xy2');
for(k = 0; k < testPoints.length; k++) {
expect(pts[traceOffset + k]).toBeFalsy(
'trace detected for outside point ' + testCoords.join(',') + ', got ' + (traceOffset + k)
);
}
}
})
.then(done, done.fail);
});
});

describe('Test Scatter.style', function() {
var gd;

Expand Down

0 comments on commit 4628741

Please sign in to comment.