From: Jacco van den Berg <39033624+LeeLenaleee@users.noreply.github.com> Date: Fri, 19 Feb 2021 22:57:59 +0000 (+0100) Subject: Fix/getRelativePosition NaN value with native event (#8459) X-Git-Tag: v3.0.0-beta.11~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d2646723814c0138cc553b407a5a1fd2849a6bc6;p=thirdparty%2FChart.js.git Fix/getRelativePosition NaN value with native event (#8459) * fix bug where onClick value returned NaN because originalEvent does not exist * add test for this behaviour * test to async --- diff --git a/src/helpers/helpers.dom.js b/src/helpers/helpers.dom.js index 4a06c9f80..05e1aa6a4 100644 --- a/src/helpers/helpers.dom.js +++ b/src/helpers/helpers.dom.js @@ -53,7 +53,7 @@ function getPositionedStyle(styles, style, suffix) { const useOffsetPos = (x, y, target) => (x > 0 || y > 0) && (!target || !target.shadowRoot); function getCanvasPosition(evt, canvas) { - const e = evt.originalEvent || evt; + const e = evt.native || evt; const touches = e.touches; const source = touches && touches.length ? touches[0] : e; const {offsetX, offsetY} = source; diff --git a/test/specs/helpers.dom.tests.js b/test/specs/helpers.dom.tests.js index d3b3f4c6b..fd0d0de14 100644 --- a/test/specs/helpers.dom.tests.js +++ b/test/specs/helpers.dom.tests.js @@ -399,5 +399,33 @@ describe('DOM helpers tests', function() { expect(Math.abs(pos3.x - Math.round((event.clientX - rect3.x - 10) / 360 * 400))).toBeLessThanOrEqual(1); expect(Math.abs(pos3.y - Math.round((event.clientY - rect3.y - 10) / 360 * 400))).toBeLessThanOrEqual(1); }); + + it('Should not return NaN with a custom event', async function() { + let dataX = null; + let dataY = null; + const chart = window.acquireChart( + { + type: 'bar', + data: { + datasets: [{ + data: [{x: 'first', y: 10}, {x: 'second', y: 5}, {x: 'third', y: 15}] + }] + }, + options: { + onHover: (e) => { + const canvasPosition = Chart.helpers.getRelativePosition(e, chart); + + dataX = canvasPosition.x; + dataY = canvasPosition.y; + } + } + }); + + const point = chart.getDatasetMeta(0).data[1]; + await jasmine.triggerMouseEvent(chart, 'mousemove', point); + + expect(dataX).not.toEqual(NaN); + expect(dataY).not.toEqual(NaN); + }); }); });