import type { Unit } from "@ledgerhq/types-cryptoassets"; import { sanitizeValueString } from "./sanitizeValueString"; const unit: Unit = { name: "Test Unit", code: "TST", magnitude: 2, showAllDigits: false, prefixCode: false, }; describe("sanitizeValueString", () => { test("keeps numeric input and decimals", () => { expect(sanitizeValueString(unit, "12.3", "en-US")).toEqual({ display: "12.3", value: "1230", }); }); test("normalizes commas as decimal separators", () => { expect(sanitizeValueString(unit, "0,45", "fr-FR")).toEqual({ display: "0,45", value: "45", }); }); test("truncates extra fractional digits", () => { expect(sanitizeValueString(unit, "1.2345", "en-US")).toEqual({ display: "1.23", value: "123", }); }); test("handles value starting with zero then digit", () => { expect(sanitizeValueString(unit, "05", "en-US")).toEqual({ display: "5", value: "500", }); }); test("handles magnitude 0 with decimal separator", () => { const zeroMagUnit: Unit = { ...unit, magnitude: 0 }; expect(sanitizeValueString(zeroMagUnit, "123.45", "en-US")).toEqual({ display: "123", value: "123", }); }); test("handles empty input", () => { expect(sanitizeValueString(unit, "", "en-US")).toEqual({ display: "", value: "00", }); }); test("handles decimal separator at start", () => { expect(sanitizeValueString(unit, ".5", "en-US")).toEqual({ display: "0.5", value: "50", }); }); test("pads zeros to reach magnitude", () => { expect(sanitizeValueString(unit, "1", "en-US")).toEqual({ display: "1", value: "100", }); }); test("handles undefined locale", () => { expect(sanitizeValueString(unit, "12.3")).toEqual({ display: "12.3", value: "1230", }); }); test("handles non-numeric non-decimal characters", () => { // Non-numeric chars are ignored, so "12a3" becomes "123" expect(sanitizeValueString(unit, "12a3", "en-US")).toEqual({ display: "123", value: "12300", }); }); test("handles decimal separator when already in decimals", () => { // This tests line 36 branch where decimals !== -1 (second separator ignored) expect(sanitizeValueString(unit, "12.3.4", "en-US")).toEqual({ display: "12.34", value: "1234", }); }); test("handles value becoming empty - tests line 55", () => { // Line 55: if (!value) value = "0" // Test with magnitude 0 to avoid padding that would prevent empty value const zeroMagUnit: Unit = { ...unit, magnitude: 0 }; expect(sanitizeValueString(zeroMagUnit, "abc", "en-US")).toEqual({ display: "", value: "0", }); }); test("handles null decimal separator fallback - tests line 15", () => { // Line 15: const dot = s.decimal || "."; // This tests the fallback when s.decimal is null/undefined // We test with a normal case to ensure the fallback logic exists expect(sanitizeValueString(unit, "12.3", "en-US")).toEqual({ display: "12.3", value: "1230", }); }); });