Skip to content

Commit

Permalink
test: improved meter test coverage & removed redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraghazra committed Oct 22, 2020
1 parent 6579b8f commit 5eda94c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/meter/MeterState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
useSealedState,
} from "reakit-utils/useSealedState";

import { isFunction, valueToPercent } from "../utils";
import { getDefaultOptimumValue, calculateStatus, clamp } from "./helpers";
import { isFunction, valueToPercent, clampValue } from "../utils";
import { getDefaultOptimumValue, calculateStatus } from "./helpers";

type Status = "safe" | "caution" | "danger" | undefined;

Expand Down Expand Up @@ -83,10 +83,10 @@ export const useMeterState = (
const initialOptimum =
sealed.optimum ?? getDefaultOptimumValue(initialLow, initialHigh);

const value = clamp(initialValue, min, max);
const optimum = clamp(initialOptimum, min, max);
let low = clamp(initialLow, min, max);
let high = clamp(initialHigh, min, max);
const value = clampValue(initialValue, min, max);
const optimum = clampValue(initialOptimum, min, max);
let low = clampValue(initialLow, min, max);
let high = clampValue(initialHigh, min, max);

// More inequalities handled
// low ≤ high (if both low and high are specified)
Expand Down
14 changes: 14 additions & 0 deletions src/meter/__tests__/Meter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,18 @@ describe("Meter", () => {
},
data,
);

test("useMeterState: low >= high", function () {
const { current } = renderMeterStateHook({ low: 1, high: 0.2 });

expect(current.low).toBe(0.2);
expect(current.high).toBe(0.2);
});

test("useMeterState: high >= low", function () {
const { current } = renderMeterStateHook({ high: 1, low: 0.2 });

expect(current.low).toBe(0.2);
expect(current.high).toBe(1);
});
});
16 changes: 0 additions & 16 deletions src/meter/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ export function getDefaultOptimumValue(min: number, max: number) {
return max < min ? min : min + (max - min) / 2;
}

/**
* Handle Inequalities with received values
*
* minimum ≤ value ≤ maximum
* minimum ≤ low ≤ maximum (if low is specified)
* minimum ≤ high ≤ maximum (if high is specified)
* minimum ≤ optimum ≤ maximum (if optimum is specified)
*
* @see https://html.spec.whatwg.org/multipage/form-elements.html#the-meter-element:attr-meter-max-3:~:text=following%20inequalities%20must%20hold
*/
export function clamp(value: number, min: number, max: number) {
if (value == null) return 0;

return Math.min(Math.max(value, min), max);
}

type CalculateStatusProps = {
value: number;
optimum: number;
Expand Down

0 comments on commit 5eda94c

Please sign in to comment.