From 24f77f18f3428f0f0f101744bfee28ddc8b1c912 Mon Sep 17 00:00:00 2001 From: Evan Rusackas Date: Thu, 30 Jun 2022 15:09:03 -0700 Subject: [PATCH] fix: Respecting max/min opacities, and adding tests. (#20555) * Respecting max/min opacities, and adding tests. * revising tests * Adding missing test case for maximum coverage :) * removing unnecessary logic and test * adding another unit test for (hopefully) full coverage. * no more ternary operator * New approach with Math.min - take THAT codecov. * one more stab at making codecov happy... ignoring the file next. * lint fixes (cherry picked from commit ac8e502228d1b247c1b56ee692c2cefade1bf1a9) --- .../src/utils/getColorFormatters.ts | 25 +++++++++++-------- .../test/utils/getColorFormatters.test.ts | 3 +++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/superset-frontend/packages/superset-ui-chart-controls/src/utils/getColorFormatters.ts b/superset-frontend/packages/superset-ui-chart-controls/src/utils/getColorFormatters.ts index 37729459f8dac..f613beb0743f8 100644 --- a/superset-frontend/packages/superset-ui-chart-controls/src/utils/getColorFormatters.ts +++ b/superset-frontend/packages/superset-ui-chart-controls/src/utils/getColorFormatters.ts @@ -37,16 +37,21 @@ export const getOpacity = ( extremeValue: number, minOpacity = MIN_OPACITY_BOUNDED, maxOpacity = MAX_OPACITY, -) => - extremeValue === cutoffPoint - ? maxOpacity - : round( - Math.abs( - ((maxOpacity - minOpacity) / (extremeValue - cutoffPoint)) * - (value - cutoffPoint), - ) + minOpacity, - 2, - ); +) => { + if (extremeValue === cutoffPoint) { + return maxOpacity; + } + return Math.min( + maxOpacity, + round( + Math.abs( + ((maxOpacity - minOpacity) / (extremeValue - cutoffPoint)) * + (value - cutoffPoint), + ) + minOpacity, + 2, + ), + ); +}; export const getColorFunction = ( { diff --git a/superset-frontend/packages/superset-ui-chart-controls/test/utils/getColorFormatters.test.ts b/superset-frontend/packages/superset-ui-chart-controls/test/utils/getColorFormatters.test.ts index 051089f87cfc6..4b957f628c32f 100644 --- a/superset-frontend/packages/superset-ui-chart-controls/test/utils/getColorFormatters.test.ts +++ b/superset-frontend/packages/superset-ui-chart-controls/test/utils/getColorFormatters.test.ts @@ -50,6 +50,9 @@ describe('getOpacity', () => { expect(getOpacity(100, 100, 50)).toEqual(0.05); expect(getOpacity(100, 100, 100, 0, 0.8)).toEqual(0.8); expect(getOpacity(100, 100, 50, 0, 1)).toEqual(0); + expect(getOpacity(999, 100, 50, 0, 1)).toEqual(1); + expect(getOpacity(100, 100, 50, 0.99, 1)).toEqual(0.99); + expect(getOpacity(99, 100, 50, 0, 1)).toEqual(0.02); }); });