|
1 | | -import { trimTrailingZeros } from "../formatters"; |
| 1 | +import BigNumber from "bignumber.js"; |
| 2 | +import { |
| 3 | + getValidBigNumber, |
| 4 | + isValidPositiveAmount, |
| 5 | + normalizeNumericString, |
| 6 | + trimTrailingZeros, |
| 7 | +} from "../formatters"; |
| 8 | + |
| 9 | +describe("normalizeNumericString", () => { |
| 10 | + it("strips non-numeric characters", () => { |
| 11 | + expect(normalizeNumericString("1a2b3")).toBe("123"); |
| 12 | + expect(normalizeNumericString("abc")).toBe(""); |
| 13 | + expect(normalizeNumericString("$1,234.56")).toBe("1234.56"); |
| 14 | + }); |
| 15 | + |
| 16 | + it("keeps a single decimal point and drops subsequent ones", () => { |
| 17 | + expect(normalizeNumericString("1.2.3")).toBe("1.23"); |
| 18 | + expect(normalizeNumericString("1..2")).toBe("1.2"); |
| 19 | + expect(normalizeNumericString("1.2.3.4")).toBe("1.234"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("handles values starting or ending with a decimal", () => { |
| 23 | + expect(normalizeNumericString(".5")).toBe(".5"); |
| 24 | + expect(normalizeNumericString("5.")).toBe("5."); |
| 25 | + }); |
| 26 | + |
| 27 | + it("preserves a single valid decimal value unchanged", () => { |
| 28 | + expect(normalizeNumericString("123.456")).toBe("123.456"); |
| 29 | + expect(normalizeNumericString("0")).toBe("0"); |
| 30 | + expect(normalizeNumericString("")).toBe(""); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("getValidBigNumber", () => { |
| 35 | + it("returns null for empty or decimal-only inputs", () => { |
| 36 | + expect(getValidBigNumber("")).toBeNull(); |
| 37 | + expect(getValidBigNumber(".")).toBeNull(); |
| 38 | + expect(getValidBigNumber("abc")).toBeNull(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("returns a BigNumber for valid numeric strings", () => { |
| 42 | + const result = getValidBigNumber("1.5"); |
| 43 | + expect(result).toBeInstanceOf(BigNumber); |
| 44 | + expect(result?.toString()).toBe("1.5"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("normalizes multi-decimal input before parsing", () => { |
| 48 | + // "1.2.3" normalizes to "1.23" |
| 49 | + expect(getValidBigNumber("1.2.3")?.toString()).toBe("1.23"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("returns BigNumber(0) for zero input", () => { |
| 53 | + expect(getValidBigNumber("0")?.toString()).toBe("0"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns a BigNumber for negative-like input by stripping the sign", () => { |
| 57 | + // cleanAmount strips "-", so "-5" becomes "5" |
| 58 | + expect(getValidBigNumber("-5")?.toString()).toBe("5"); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("isValidPositiveAmount", () => { |
| 63 | + it("returns false for empty, decimal-only, or non-numeric input", () => { |
| 64 | + expect(isValidPositiveAmount("")).toBe(false); |
| 65 | + expect(isValidPositiveAmount(".")).toBe(false); |
| 66 | + expect(isValidPositiveAmount("abc")).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + it("returns false for zero", () => { |
| 70 | + expect(isValidPositiveAmount("0")).toBe(false); |
| 71 | + expect(isValidPositiveAmount("0.00")).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns true for positive amounts", () => { |
| 75 | + expect(isValidPositiveAmount("1")).toBe(true); |
| 76 | + expect(isValidPositiveAmount("0.00001")).toBe(true); |
| 77 | + expect(isValidPositiveAmount("1000000")).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns true after normalizing multi-decimal input", () => { |
| 81 | + // "1.2.3" → "1.23" → > 0 |
| 82 | + expect(isValidPositiveAmount("1.2.3")).toBe(true); |
| 83 | + }); |
| 84 | +}); |
2 | 85 |
|
3 | 86 | describe("trimTrailingZeros", () => { |
4 | 87 | it("removes trailing zeros from decimal numbers", () => { |
|
0 commit comments