Skip to content

Commit 7c20804

Browse files
committed
Add 10 more test cases for nearest interaction when spanGaps=true
1 parent fe0cd3d commit 7c20804

File tree

1 file changed

+86
-54
lines changed

1 file changed

+86
-54
lines changed

test/specs/core.interaction.tests.js

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -913,61 +913,93 @@ describe('Core.Interaction', function() {
913913
});
914914
});
915915

916-
it('should select closest non-null elements if spanGaps=true and closest non-null element is to the left', function() {
917-
const chart = window.acquireChart({
918-
type: 'line',
919-
data: {
920-
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9],
921-
datasets: [{
922-
data: [12, 19, null, null, null, null, 5, 2, 3],
923-
spanGaps: true,
924-
}]
925-
}
926-
});
927-
chart.update();
928-
const interactionPointIndex = 3;
929-
const meta = chart.getDatasetMeta(0);
930-
const point = meta.data[interactionPointIndex];
931-
932-
const evt = {
933-
type: 'click',
934-
chart: chart,
935-
native: true, // needed otherwise things its a DOM event
936-
x: point.x,
937-
y: point.y,
938-
};
939-
940-
const expectedInteractionPointIndex = 1;
941-
const elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}).map(item => item.element);
942-
expect(elements).toEqual([meta.data[expectedInteractionPointIndex]]);
943-
});
916+
const testCases = [
917+
{
918+
data: [12, 19, null, null, null, null, 5, 2],
919+
clickPointIndex: 0,
920+
expectedNearestPointIndex: 0
921+
},
922+
{
923+
data: [12, 19, null, null, null, null, 5, 2],
924+
clickPointIndex: 1,
925+
expectedNearestPointIndex: 1},
926+
{
927+
data: [12, 19, null, null, null, null, 5, 2],
928+
clickPointIndex: 2,
929+
expectedNearestPointIndex: 1
930+
},
931+
{
932+
data: [12, 19, null, null, null, null, 5, 2],
933+
clickPointIndex: 3,
934+
expectedNearestPointIndex: 1
935+
},
936+
{
937+
data: [12, 19, null, null, null, null, 5, 2],
938+
clickPointIndex: 4,
939+
expectedNearestPointIndex: 6
940+
},
941+
{
942+
data: [12, 19, null, null, null, null, 5, 2],
943+
clickPointIndex: 5,
944+
expectedNearestPointIndex: 6
945+
},
946+
{
947+
data: [12, 19, null, null, null, null, 5, 2],
948+
clickPointIndex: 6,
949+
expectedNearestPointIndex: 6
950+
},
951+
{
952+
data: [12, 19, null, null, null, null, 5, 2],
953+
clickPointIndex: 7,
954+
expectedNearestPointIndex: 7
955+
},
956+
{
957+
data: [12, 0, null, null, null, null, 0, 2],
958+
clickPointIndex: 3,
959+
expectedNearestPointIndex: 1
960+
},
961+
{
962+
data: [12, 0, null, null, null, null, 0, 2],
963+
clickPointIndex: 4,
964+
expectedNearestPointIndex: 6
965+
},
966+
{
967+
data: [12, -1, null, null, null, null, -1, 2],
968+
clickPointIndex: 3,
969+
expectedNearestPointIndex: 1
970+
},
971+
{
972+
data: [12, -1, null, null, null, null, -1, 2],
973+
clickPointIndex: 4,
974+
expectedNearestPointIndex: 6
975+
}
976+
];
977+
testCases.forEach(({data, clickPointIndex, expectedNearestPointIndex}, i) => {
978+
it(`should select nearest non-null element with index ${expectedNearestPointIndex} when clicking on element with index ${clickPointIndex} in test case ${i + 1} if spanGaps=true`, function() {
979+
const chart = window.acquireChart({
980+
type: 'line',
981+
data: {
982+
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9],
983+
datasets: [{
984+
data: data,
985+
spanGaps: true,
986+
}]
987+
}
988+
});
989+
chart.update();
990+
const meta = chart.getDatasetMeta(0);
991+
const point = meta.data[clickPointIndex];
944992

945-
it('should select closest non-null elements if spanGaps=true and closest non-null element is to the right', function() {
946-
const chart = window.acquireChart({
947-
type: 'line',
948-
data: {
949-
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9],
950-
datasets: [{
951-
data: [12, 19, null, null, null, null, 5, 2, 3],
952-
spanGaps: true,
953-
}]
954-
}
993+
const evt = {
994+
type: 'click',
995+
chart: chart,
996+
native: true, // needed otherwise things its a DOM event
997+
x: point.x,
998+
y: point.y,
999+
};
1000+
1001+
const elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}).map(item => item.element);
1002+
expect(elements).toEqual([meta.data[expectedNearestPointIndex]]);
9551003
});
956-
chart.update();
957-
const interactionPointIndex = 4;
958-
const meta = chart.getDatasetMeta(0);
959-
const point = meta.data[interactionPointIndex];
960-
961-
const evt = {
962-
type: 'click',
963-
chart: chart,
964-
native: true, // needed otherwise things its a DOM event
965-
x: point.x,
966-
y: point.y,
967-
};
968-
969-
const expectedInteractionPointIndex = 6;
970-
const elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}).map(item => item.element);
971-
expect(elements).toEqual([meta.data[expectedInteractionPointIndex]]);
9721004
});
9731005
});

0 commit comments

Comments
 (0)