From f7ae0a56eb91656f6c5e2c00391f5b0fc9b94756 Mon Sep 17 00:00:00 2001 From: Jacco van den Berg Date: Fri, 16 Aug 2024 09:11:35 +0200 Subject: [PATCH 1/6] Return false on no data --- src/core/core.scale.js | 1 - src/plugins/plugin.tooltip.js | 5 +++++ test/specs/plugin.tooltip.tests.js | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core/core.scale.js b/src/core/core.scale.js index 3265e103d1a..dcf4bd00b2b 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -301,7 +301,6 @@ export default class Scale extends Element { * @since 3.0 */ getMinMax(canStack) { - // eslint-disable-next-line prefer-const let {min, max, minDefined, maxDefined} = this.getUserBounds(); let range; diff --git a/src/plugins/plugin.tooltip.js b/src/plugins/plugin.tooltip.js index e76b1479e06..b39681ce2ca 100644 --- a/src/plugins/plugin.tooltip.js +++ b/src/plugins/plugin.tooltip.js @@ -38,6 +38,11 @@ const positioners = { } } + // No visible items where found, return false so we don't have to divide by 0 which reduces in NaN + if (count === 0 || xSet.size === 0) { + return false; + } + const xAverage = [...xSet].reduce((a, b) => a + b) / xSet.size; return { diff --git a/test/specs/plugin.tooltip.tests.js b/test/specs/plugin.tooltip.tests.js index 69e8c7f64c9..fa37d5a97a8 100644 --- a/test/specs/plugin.tooltip.tests.js +++ b/test/specs/plugin.tooltip.tests.js @@ -1144,6 +1144,14 @@ describe('Plugin.Tooltip', function() { expect(tooltipModel.caretX).not.toBe(xPositionArrayAverage); expect(tooltipModel.caretX).toBe(xPositionSetAverage); }); + + it('Should not fail with all hiden data elements on the average positioner', function() { + const averagePositioner = tooltipPlugin.positioners.average; + + // Simulate `hasValue` returns false + expect(averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).not.toThrow(); + expect(averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).toBe(false); + }); }); it('Should avoid tooltip truncation in x axis if there is enough space to show tooltip without truncation', async function() { From 3c3d1a3bb2c465ebbe4e0b2edbf64a4276c99a78 Mon Sep 17 00:00:00 2001 From: Jacco van den Berg Date: Fri, 16 Aug 2024 09:26:54 +0200 Subject: [PATCH 2/6] make funciton --- test/specs/plugin.tooltip.tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/specs/plugin.tooltip.tests.js b/test/specs/plugin.tooltip.tests.js index fa37d5a97a8..7726da1e66b 100644 --- a/test/specs/plugin.tooltip.tests.js +++ b/test/specs/plugin.tooltip.tests.js @@ -1149,8 +1149,8 @@ describe('Plugin.Tooltip', function() { const averagePositioner = tooltipPlugin.positioners.average; // Simulate `hasValue` returns false - expect(averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).not.toThrow(); - expect(averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).toBe(false); + expect(averagePositioner(() => [{x: 'invalidNumber', y: 'invalidNumber'}])).not.toThrow(); + expect(averagePositioner(() => [{x: 'invalidNumber', y: 'invalidNumber'}])).toBe(false); }); }); From 4393d60c9b3d1e01de3f3f4148fafc1b06ea85f3 Mon Sep 17 00:00:00 2001 From: Jacco van den Berg Date: Fri, 16 Aug 2024 09:30:15 +0200 Subject: [PATCH 3/6] fix syntax --- test/specs/plugin.tooltip.tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/specs/plugin.tooltip.tests.js b/test/specs/plugin.tooltip.tests.js index 7726da1e66b..da35a79d78c 100644 --- a/test/specs/plugin.tooltip.tests.js +++ b/test/specs/plugin.tooltip.tests.js @@ -1149,8 +1149,8 @@ describe('Plugin.Tooltip', function() { const averagePositioner = tooltipPlugin.positioners.average; // Simulate `hasValue` returns false - expect(averagePositioner(() => [{x: 'invalidNumber', y: 'invalidNumber'}])).not.toThrow(); - expect(averagePositioner(() => [{x: 'invalidNumber', y: 'invalidNumber'}])).toBe(false); + expect(() => averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).not.toThrow(); + expect(() => averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).toBe(false); }); }); From 49b572226312f9e4fbffacc712631a48550e7ee8 Mon Sep 17 00:00:00 2001 From: Jacco van den Berg Date: Fri, 16 Aug 2024 09:33:50 +0200 Subject: [PATCH 4/6] Get result by calling function outside of jasmine --- test/specs/plugin.tooltip.tests.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/specs/plugin.tooltip.tests.js b/test/specs/plugin.tooltip.tests.js index da35a79d78c..94ae45b7246 100644 --- a/test/specs/plugin.tooltip.tests.js +++ b/test/specs/plugin.tooltip.tests.js @@ -1150,7 +1150,8 @@ describe('Plugin.Tooltip', function() { // Simulate `hasValue` returns false expect(() => averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).not.toThrow(); - expect(() => averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).toBe(false); + const result = averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}]); + expect(result).toBe(false); }); }); From 1ec45d3dd5ac5f9bfe9938a087dbebb3d49e7b6b Mon Sep 17 00:00:00 2001 From: Jacco van den Berg Date: Fri, 16 Aug 2024 09:35:50 +0200 Subject: [PATCH 5/6] Disable fix --- src/plugins/plugin.tooltip.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/plugin.tooltip.js b/src/plugins/plugin.tooltip.js index b39681ce2ca..a061aacfc76 100644 --- a/src/plugins/plugin.tooltip.js +++ b/src/plugins/plugin.tooltip.js @@ -38,10 +38,10 @@ const positioners = { } } - // No visible items where found, return false so we don't have to divide by 0 which reduces in NaN - if (count === 0 || xSet.size === 0) { - return false; - } + // // No visible items where found, return false so we don't have to divide by 0 which reduces in NaN + // if (count === 0 || xSet.size === 0) { + // return false; + // } const xAverage = [...xSet].reduce((a, b) => a + b) / xSet.size; From bbc8114aadc186741e81860002ff2d736f68544d Mon Sep 17 00:00:00 2001 From: Jacco van den Berg Date: Fri, 16 Aug 2024 09:36:47 +0200 Subject: [PATCH 6/6] Enable fix --- src/plugins/plugin.tooltip.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/plugin.tooltip.js b/src/plugins/plugin.tooltip.js index a061aacfc76..b39681ce2ca 100644 --- a/src/plugins/plugin.tooltip.js +++ b/src/plugins/plugin.tooltip.js @@ -38,10 +38,10 @@ const positioners = { } } - // // No visible items where found, return false so we don't have to divide by 0 which reduces in NaN - // if (count === 0 || xSet.size === 0) { - // return false; - // } + // No visible items where found, return false so we don't have to divide by 0 which reduces in NaN + if (count === 0 || xSet.size === 0) { + return false; + } const xAverage = [...xSet].reduce((a, b) => a + b) / xSet.size;