|
| 1 | +import { formatNumber } from "../formatNumber"; |
| 2 | + |
| 3 | +describe("formatNumber", () => { |
| 4 | + describe("billions formatting", () => { |
| 5 | + it("should format billions with 'b' suffix", () => { |
| 6 | + expect(formatNumber(1000000000)).toBe("1b"); |
| 7 | + expect(formatNumber(1500000000)).toBe("1.5b"); |
| 8 | + expect(formatNumber(2300000000)).toBe("2.3b"); |
| 9 | + }); |
| 10 | + |
| 11 | + it("should format large billions correctly", () => { |
| 12 | + expect(formatNumber(15000000000)).toBe("15b"); |
| 13 | + expect(formatNumber(999999999999)).toBe("1000b"); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + describe("millions formatting", () => { |
| 18 | + it("should format millions with 'm' suffix", () => { |
| 19 | + expect(formatNumber(1000000)).toBe("1m"); |
| 20 | + expect(formatNumber(1500000)).toBe("1.5m"); |
| 21 | + expect(formatNumber(2300000)).toBe("2.3m"); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should format large millions correctly", () => { |
| 25 | + expect(formatNumber(999000000)).toBe("999m"); |
| 26 | + expect(formatNumber(500000000)).toBe("500m"); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("thousands formatting", () => { |
| 31 | + it("should format thousands with 'k' suffix", () => { |
| 32 | + expect(formatNumber(1000)).toBe("1k"); |
| 33 | + expect(formatNumber(1500)).toBe("1.5k"); |
| 34 | + expect(formatNumber(2300)).toBe("2.3k"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should format large thousands correctly", () => { |
| 38 | + expect(formatNumber(999000)).toBe("999k"); |
| 39 | + expect(formatNumber(500000)).toBe("500k"); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("regular numbers", () => { |
| 44 | + it("should format numbers less than 1000 without suffix", () => { |
| 45 | + expect(formatNumber(0)).toBe("0"); |
| 46 | + expect(formatNumber(1)).toBe("1"); |
| 47 | + expect(formatNumber(100)).toBe("100"); |
| 48 | + expect(formatNumber(999)).toBe("999"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should handle decimal numbers less than 1000", () => { |
| 52 | + expect(formatNumber(1.5)).toBe("1.5"); |
| 53 | + expect(formatNumber(99.9)).toBe("99.9"); |
| 54 | + expect(formatNumber(123.456)).toBe("123.5"); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe("decimal precision", () => { |
| 59 | + it("should respect custom digits parameter", () => { |
| 60 | + expect(formatNumber(1234, 0)).toBe("1k"); |
| 61 | + expect(formatNumber(1234, 1)).toBe("1.2k"); |
| 62 | + expect(formatNumber(1234, 2)).toBe("1.23k"); |
| 63 | + expect(formatNumber(1234, 3)).toBe("1.234k"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should handle precision for millions", () => { |
| 67 | + expect(formatNumber(1234567, 0)).toBe("1m"); |
| 68 | + expect(formatNumber(1234567, 1)).toBe("1.2m"); |
| 69 | + expect(formatNumber(1234567, 2)).toBe("1.23m"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should handle precision for billions", () => { |
| 73 | + expect(formatNumber(1234567890, 0)).toBe("1b"); |
| 74 | + expect(formatNumber(1234567890, 1)).toBe("1.2b"); |
| 75 | + expect(formatNumber(1234567890, 2)).toBe("1.23b"); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("trailing zeros cleanup", () => { |
| 80 | + it("should remove trailing zeros after decimal", () => { |
| 81 | + expect(formatNumber(1000)).toBe("1k"); |
| 82 | + expect(formatNumber(2000000)).toBe("2m"); |
| 83 | + expect(formatNumber(3000000000)).toBe("3b"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should keep significant decimals and remove trailing zeros", () => { |
| 87 | + expect(formatNumber(1100, 2)).toBe("1.1k"); |
| 88 | + expect(formatNumber(1010, 2)).toBe("1.01k"); |
| 89 | + expect(formatNumber(1001, 3)).toBe("1.001k"); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("negative numbers", () => { |
| 94 | + it("should handle negative thousands", () => { |
| 95 | + expect(formatNumber(-1000)).toBe("-1k"); |
| 96 | + expect(formatNumber(-1500)).toBe("-1.5k"); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should handle negative millions", () => { |
| 100 | + expect(formatNumber(-1000000)).toBe("-1m"); |
| 101 | + expect(formatNumber(-2500000)).toBe("-2.5m"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should handle negative billions", () => { |
| 105 | + expect(formatNumber(-1000000000)).toBe("-1b"); |
| 106 | + expect(formatNumber(-5500000000)).toBe("-5.5b"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should handle negative numbers less than 1000", () => { |
| 110 | + expect(formatNumber(-1)).toBe("-1"); |
| 111 | + expect(formatNumber(-100)).toBe("-100"); |
| 112 | + expect(formatNumber(-999)).toBe("-999"); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe("edge cases", () => { |
| 117 | + it("should handle zero", () => { |
| 118 | + expect(formatNumber(0)).toBe("0"); |
| 119 | + expect(formatNumber(-0)).toBe("0"); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should handle very small numbers", () => { |
| 123 | + expect(formatNumber(0.1)).toBe("0.1"); |
| 124 | + expect(formatNumber(0.01)).toBe("0"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should handle numbers just below thresholds", () => { |
| 128 | + expect(formatNumber(999)).toBe("999"); |
| 129 | + expect(formatNumber(999999)).toBe("1000k"); |
| 130 | + expect(formatNumber(999999999)).toBe("1000m"); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("invalid inputs", () => { |
| 135 | + it("should return '0' for NaN", () => { |
| 136 | + expect(formatNumber(NaN)).toBe("0"); |
| 137 | + }); |
| 138 | + |
| 139 | + it("should return '0' for non-number types", () => { |
| 140 | + expect(formatNumber("1000" as any)).toBe("0"); |
| 141 | + expect(formatNumber(null as any)).toBe("0"); |
| 142 | + expect(formatNumber(undefined as any)).toBe("0"); |
| 143 | + expect(formatNumber({} as any)).toBe("0"); |
| 144 | + expect(formatNumber([] as any)).toBe("0"); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe("real-world examples", () => { |
| 149 | + it("should format social media counts correctly", () => { |
| 150 | + expect(formatNumber(1234)).toBe("1.2k"); // 1.2k followers |
| 151 | + expect(formatNumber(45678)).toBe("45.7k"); // 45.7k likes |
| 152 | + expect(formatNumber(1234567)).toBe("1.2m"); // 1.2m views |
| 153 | + expect(formatNumber(9876543210)).toBe("9.9b"); // 9.9b population |
| 154 | + }); |
| 155 | + |
| 156 | + it("should format file sizes", () => { |
| 157 | + expect(formatNumber(1024, 0)).toBe("1k"); |
| 158 | + expect(formatNumber(1536, 1)).toBe("1.5k"); |
| 159 | + expect(formatNumber(1048576, 0)).toBe("1m"); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
0 commit comments