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
This commit is contained in:
Evan Rusackas 2022-06-30 15:09:03 -07:00 committed by GitHub
parent f5f8ddec3e
commit ac8e502228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View File

@ -37,16 +37,21 @@ export const getOpacity = (
extremeValue: number, extremeValue: number,
minOpacity = MIN_OPACITY_BOUNDED, minOpacity = MIN_OPACITY_BOUNDED,
maxOpacity = MAX_OPACITY, maxOpacity = MAX_OPACITY,
) => ) => {
extremeValue === cutoffPoint if (extremeValue === cutoffPoint) {
? maxOpacity return maxOpacity;
: round( }
return Math.min(
maxOpacity,
round(
Math.abs( Math.abs(
((maxOpacity - minOpacity) / (extremeValue - cutoffPoint)) * ((maxOpacity - minOpacity) / (extremeValue - cutoffPoint)) *
(value - cutoffPoint), (value - cutoffPoint),
) + minOpacity, ) + minOpacity,
2, 2,
),
); );
};
export const getColorFunction = ( export const getColorFunction = (
{ {

View File

@ -50,6 +50,9 @@ describe('getOpacity', () => {
expect(getOpacity(100, 100, 50)).toEqual(0.05); expect(getOpacity(100, 100, 50)).toEqual(0.05);
expect(getOpacity(100, 100, 100, 0, 0.8)).toEqual(0.8); expect(getOpacity(100, 100, 100, 0, 0.8)).toEqual(0.8);
expect(getOpacity(100, 100, 50, 0, 1)).toEqual(0); 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);
}); });
}); });