Skip to content

Commit 1472229

Browse files
committed
test(utils): add tests for 'clamp' and 'getDecimalPrecision'
1 parent 7a63028 commit 1472229

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

src/components/Select/Select.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import propTypes from 'prop-types';
44
import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled';
55
import useForkRef from '../common/hooks/useForkRef';
66

7-
import { clamp, areEqualValues } from '../common/utils';
7+
import { clamp } from '../common/utils';
88

99
import {
1010
StyledDropdownButton,
@@ -30,10 +30,17 @@ const KEYS = {
3030
TAB: 'Tab'
3131
};
3232

33-
export const getWrapper = variant =>
33+
function areEqualValues(a, b) {
34+
if (typeof b === 'object' && b !== null) {
35+
return a === b;
36+
}
37+
return String(a) === String(b);
38+
}
39+
40+
const getWrapper = variant =>
3441
variant === 'flat' ? StyledFlatSelectWrapper : StyledSelectWrapper;
3542

36-
export const getDisplayLabel = (selectedOption, formatDisplay) => {
43+
const getDisplayLabel = (selectedOption, formatDisplay) => {
3744
if (selectedOption) {
3845
if (formatDisplay) {
3946
return formatDisplay(selectedOption);
@@ -43,7 +50,7 @@ export const getDisplayLabel = (selectedOption, formatDisplay) => {
4350
return '';
4451
};
4552

46-
export const getDefaultValue = (defaultValue, options) => {
53+
const getDefaultValue = (defaultValue, options) => {
4754
if (defaultValue) {
4855
return defaultValue;
4956
}

src/components/Slider/Slider.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import {
1010
createDisabledTextStyles,
1111
createHatchedBackground
1212
} from '../common';
13-
import { clamp, percentToValue, roundValueToStep } from '../common/utils';
13+
import { clamp, roundValueToStep } from '../common/utils';
1414
import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled';
1515
import useForkRef from '../common/hooks/useForkRef';
1616
import { useIsFocusVisible } from '../common/hooks/useIsFocusVisible';
1717
import Cutout from '../Cutout/Cutout';
1818

19+
function percentToValue(percent, min, max) {
20+
return (max - min) * percent + min;
21+
}
22+
1923
function trackFinger(event, touchId) {
2024
if (touchId.current !== undefined && event.changedTouches) {
2125
for (let i = 0; i < event.changedTouches.length; i += 1) {

src/components/common/utils/index.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
export const noOp = () => {};
22

33
export function clamp(value, min, max) {
4-
if (min !== null && value > max) {
4+
if (max !== null && value > max) {
55
return max;
66
}
7-
if (max !== null && value < min) {
7+
if (min !== null && value < min) {
88
return min;
99
}
1010
return value;
1111
}
1212

13-
export function percentToValue(percent, min, max) {
14-
return (max - min) * percent + min;
15-
}
16-
1713
// helper functions below are from Material UI (https://github.com/mui-org/material-ui)
1814
export function getDecimalPrecision(num) {
1915
if (Math.abs(num) < 1) {
@@ -33,10 +29,3 @@ export function roundValueToStep(value, step, min) {
3329
const nearest = Math.round((value - min) / step) * step + min;
3430
return Number(nearest.toFixed(getDecimalPrecision(step)));
3531
}
36-
37-
export function areEqualValues(a, b) {
38-
if (typeof b === 'object' && b !== null) {
39-
return a === b;
40-
}
41-
return String(a) === String(b);
42-
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { clamp, getDecimalPrecision, roundValueToStep } from './index';
2+
3+
describe('clamp', () => {
4+
it('should return passed value if its between min and max', () => {
5+
expect(clamp(4, 3, 5)).toBe(4);
6+
});
7+
8+
it('should return min if value is smaller than min', () => {
9+
expect(clamp(2, 3, 5)).toBe(3);
10+
});
11+
12+
it('should return max if value is bigger than max', () => {
13+
expect(clamp(6, 3, 5)).toBe(5);
14+
});
15+
16+
it('should accept null as min', () => {
17+
expect(clamp(6, null, 5)).toBe(5);
18+
});
19+
20+
it('should accept null as max', () => {
21+
expect(clamp(1, 2, null)).toBe(2);
22+
});
23+
});
24+
25+
describe('getDecimalPrecision', () => {
26+
it('should return 0 when passed a round number', () => {
27+
expect(getDecimalPrecision(4)).toBe(0);
28+
});
29+
30+
it('should return correct decimal precision', () => {
31+
expect(getDecimalPrecision(1.233)).toBe(3);
32+
});
33+
});
34+
35+
describe('roundValueToStep', () => {
36+
it('should be able to round down', () => {
37+
expect(roundValueToStep(4, 3, 0)).toBe(3);
38+
});
39+
40+
it('should be able to round up', () => {
41+
expect(roundValueToStep(5, 3, 0)).toBe(6);
42+
});
43+
});

0 commit comments

Comments
 (0)