From 9d7094b72362d61bfa13e5b60929df80d05ec204 Mon Sep 17 00:00:00 2001 From: Chris <151883835+VeronicDev@users.noreply.github.com> Date: Thu, 22 Oct 2026 12:12:25 +0100 Subject: [PATCH] Fix #1780: [test] Cover amount parser locale and exponent inputs --- frontend/src/utils/amount.test.ts | 72 +++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 frontend/src/utils/amount.test.ts diff --git a/frontend/src/utils/amount.test.ts b/frontend/src/utils/amount.test.ts new file mode 100644 index 00000000..a7b0165f --- /dev/null +++ b/frontend/src/utils/amount.test.ts @@ -0,0 +1,72 @@ +import { describe, it, expect } from "vitest"; +import { + parseAmount, + formatAmount, + validateAmountInput, + hasValidPrecision, +} from "./amount"; + +describe("amount parser locale, exponent, whitespace, and empty strings", () => { + describe("parseAmount with locale and edge inputs", () => { + it("handles empty and whitespace-only strings", () => { + expect(parseAmount("", 7)).toBe(0n); + expect(parseAmount(" ", 7)).toBe(0n); + expect(parseAmount("\t\n", 7)).toBe(0n); + }); + + it("rejects or handles exponent notation correctly", () => { + // Exponent notation like '1e5' is non-standard for strict decimal input parsers unless handled, + // parseAmount regex /^[\d]*\.?[\d]*$/ rejects 'e'. Verify it returns 0n. + expect(parseAmount("1e5", 7)).toBe(0n); + expect(parseAmount("1.5E-2", 7)).toBe(0n); + }); + + it("handles locale separators (comma vs dot) and invalid punctuation", () => { + // parseAmount expects dot (.) as decimal separator. Commas should be treated as non-digits / invalid formats. + expect(parseAmount("1,234", 7)).toBe(0n); + expect(parseAmount("1.234.56", 7)).toBe(0n); + }); + + it("handles leading and trailing whitespace around valid numbers", () => { + expect(parseAmount(" 123.45 ", 7)).toBe(1234500000n); + expect(parseAmount("\t0.1\n", 7)).toBe(1000000n); + }); + }); + + describe("validateAmountInput with exponent and locale inputs", () => { + it("rejects exponent notation with valid error message", () => { + expect(validateAmountInput("1e5", 7)).toBe("Please enter a valid number"); + expect(validateAmountInput("1.2e-3", 7)).toBe("Please enter a valid number"); + }); + + it("rejects locale comma separators", () => { + expect(validateAmountInput("1,234.56", 7)).toBe("Please enter a valid number"); + expect(validateAmountInput("1,5", 7)).toBe("Please enter a valid number"); + }); + + it("validates whitespace and empty strings", () => { + expect(validateAmountInput("", 7)).toBe("Amount is required"); + expect(validateAmountInput(" ", 7)).toBe("Amount is required"); + expect(validateAmountInput("0", 7)).toBe("Amount must be greater than 0"); + }); + }); + + describe("formatAmount and edge cases", () => { + it("formats zero and boundary bigints correctly", () => { + expect(formatAmount(0n, 7)).toBe("0"); + expect(formatAmount(1n, 7)).toBe("0.0000001"); + expect(formatAmount(10000000n, 7)).toBe("1"); + }); + }); + + describe("hasValidPrecision with exponents and edge cases", () => { + it("handles empty and whitespace correctly", () => { + expect(hasValidPrecision("", 7)).toBe(true); + expect(hasValidPrecision(" ", 7)).toBe(true); + }); + + it("rejects exponential notation in precision check", () => { + expect(hasValidPrecision("1e-5", 7)).toBe(false); + }); + }); +});