Files
buzz-sheet/lib/guides/percent-input.test.ts
T

39 lines
1.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { finalizePercentInput, isValidPercent, normalizePercentInput } from "@/lib/guides/percent-input";
describe("percentage input", () => {
it.each([
["-9999", true],
["9999", true],
["0", true],
["12.5", true],
["-12.5", true],
[" 100 ", true],
["1e2", true],
["", false],
[" \t\n", false],
["-", false],
["invalid", false],
["NaN", false],
["Infinity", false],
["-Infinity", false],
["9999.01", false],
["-9999.01", false],
])("validates %j as %s", (value, valid) => {
expect(isValidPercent(value)).toBe(valid);
});
it("turns only a lone dash into zero when editing finishes", () => {
expect(normalizePercentInput("-")).toBe("-");
expect(finalizePercentInput("-")).toBe("0");
expect(finalizePercentInput("-20")).toBe("-20");
});
it("keeps positive and negative decimal input and ignores other text", () => {
expect(normalizePercentInput("12.5")).toBe("12.5");
expect(normalizePercentInput("-12.5")).toBe("-12.5");
expect(normalizePercentInput("invalid", "12")).toBe("12");
});
});