Repository: chartjs/Chart.js
File: test/specs/core.interaction.tests.js (JavaScript)

Code before:
    });
    chart.update();
    const interactionPointIndex = 3;
    const meta = chart.getDatasetMeta(0);
    const point = meta.data[interactionPointIndex];

    const evt = {
      type: 'click',
      chart: chart,
      native: true, // needed otherwise things its a DOM event
      x: point.x,
      y: point.y,
    };

    const expectedInteractionPointIndex = 1;
    const elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}).map(item => item.element);
    expect(elements).toEqual([meta.data[expectedInteractionPointIndex]]);
  });

  it('should select closest non-null elements if spanGaps=true and closest non-null element is to the right', function() {
    const chart = window.acquireChart({
      type: 'line',
      data: {
        labels: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        datasets: [{
          data: [12, 19, null, null, null, null, 5, 2, 3],
          spanGaps: true,
        }]
      }
    });
    chart.update();
    const interactionPointIndex = 4;
    const meta = chart.getDatasetMeta(0);
    const point = meta.data[interactionPointIndex];

    const evt = {
      type: 'click',
      chart: chart,
      native: true, // needed otherwise things its a DOM event
      x: point.x,
      y: point.y,
    };

    const expectedInteractionPointIndex = 6;
    const elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}).map(item => item.element);
    expect(elements).toEqual([meta.data[expectedInteractionPointIndex]]);
  });
});


Review comment (bug):
thought: maybe it would be good to have test(s) for the zero and edge cases, something like: 

```suggestion
  it.each([
    [12, 19, null, null, null, null, 5, 2, 3],
    [12, 19, null, null, null, null, 0, 2, 3],
    [12, 19, null, null, null, null, -1],]
  ])('should select closest non-null elements if spanGaps=true and closest non-null element is to the right', function(data) {
    const chart = window.acquireChart({
      type: 'line',
      data: {
        labels: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        datasets: [{
          data,
```

Code after:
    {
      data: [12, 19, null, null, null, null, 5, 2],
      clickPointIndex: 2,
      expectedNearestPointIndex: 1
    },
    {
      data: [12, 19, null, null, null, null, 5, 2],
      clickPointIndex: 3,
      expectedNearestPointIndex: 1
    },
    {
      data: [12, 19, null, null, null, null, 5, 2],
      clickPointIndex: 4,
      expectedNearestPointIndex: 6
    },
    {
      data: [12, 19, null, null, null, null, 5, 2],
      clickPointIndex: 5,
      expectedNearestPointIndex: 6
    },
    {
      data: [12, 19, null, null, null, null, 5, 2],
      clickPointIndex: 6,
      expectedNearestPointIndex: 6
    },
    {
      data: [12, 19, null, null, null, null, 5, 2],
      clickPointIndex: 7,
      expectedNearestPointIndex: 7
    },
    {
      data: [12, 0, null, null, null, null, 0, 2],
      clickPointIndex: 3,
      expectedNearestPointIndex: 1
    },
    {
      data: [12, 0, null, null, null, null, 0, 2],
      clickPointIndex: 4,
      expectedNearestPointIndex: 6
    },
    {
      data: [12, -1, null, null, null, null, -1, 2],
      clickPointIndex: 3,
      expectedNearestPointIndex: 1
    },
    {
      data: [12, -1, null, null, null, null, -1, 2],
      clickPointIndex: 4,
      expectedNearestPointIndex: 6
    }
  ];