|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { describe, expect, it, vi } from "vitest"; |
2 | 2 |
|
3 | 3 | import { |
4 | 4 | displayDecimal, |
5 | 5 | normalizeDecimalInput, |
| 6 | + padDecimalOnBlur, |
6 | 7 | padDecimalToTwo, |
7 | 8 | parseNumber, |
8 | 9 | } from "../shared/number"; |
@@ -98,3 +99,35 @@ describe("padDecimalToTwo", () => { |
98 | 99 | expect(padDecimalToTwo("abc")).toBe("abc"); |
99 | 100 | }); |
100 | 101 | }); |
| 102 | + |
| 103 | +describe("padDecimalOnBlur", () => { |
| 104 | + it("calls the setter with the padded value when padding changes it", () => { |
| 105 | + const setter = vi.fn(); |
| 106 | + padDecimalOnBlur("100", setter); |
| 107 | + expect(setter).toHaveBeenCalledWith("100.00"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("calls the setter when only a trailing zero is missing", () => { |
| 111 | + const setter = vi.fn(); |
| 112 | + padDecimalOnBlur("100.5", setter); |
| 113 | + expect(setter).toHaveBeenCalledWith("100.50"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("does not call the setter when the value is already padded", () => { |
| 117 | + const setter = vi.fn(); |
| 118 | + padDecimalOnBlur("100.50", setter); |
| 119 | + expect(setter).not.toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("does not call the setter on an empty value", () => { |
| 123 | + const setter = vi.fn(); |
| 124 | + padDecimalOnBlur("", setter); |
| 125 | + expect(setter).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("does not call the setter on a non-numeric value", () => { |
| 129 | + const setter = vi.fn(); |
| 130 | + padDecimalOnBlur("abc", setter); |
| 131 | + expect(setter).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | +}); |
0 commit comments