|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import t, { f } from "../i18n"; |
| 4 | + |
| 5 | +describe("f()", () => { |
| 6 | + describe("number input", () => { |
| 7 | + it("preserves sub-micro values", () => { |
| 8 | + expect(f(0.000_000_9)).toStrictEqual({ en: "0.0000009", es: "0,0000009" }); |
| 9 | + }); |
| 10 | + |
| 11 | + it("preserves 6 fractional digits", () => { |
| 12 | + expect(f(0.000_001)).toStrictEqual({ en: "0.000001", es: "0,000001" }); |
| 13 | + }); |
| 14 | + |
| 15 | + it("formats regular decimals", () => { |
| 16 | + expect(f(99.973)).toStrictEqual({ en: "99.973", es: "99,973" }); |
| 17 | + }); |
| 18 | + |
| 19 | + it("formats integers", () => { |
| 20 | + expect(f(5)).toStrictEqual({ en: "5", es: "5" }); |
| 21 | + }); |
| 22 | + |
| 23 | + it("formats thousands with separator", () => { |
| 24 | + expect(f(1000)).toStrictEqual({ en: "1,000", es: "1.000" }); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("string input", () => { |
| 29 | + it("formats regular decimals", () => { |
| 30 | + expect(f("99.973")).toStrictEqual({ en: "99.973", es: "99,973" }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("preserves sub-micro values and trims trailing zeros", () => { |
| 34 | + expect(f("0.00000090")).toStrictEqual({ en: "0.0000009", es: "0,0000009" }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("formats integers", () => { |
| 38 | + expect(f("5")).toStrictEqual({ en: "5", es: "5" }); |
| 39 | + }); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe("t()", () => { |
| 44 | + it("returns en and es translations with no options", () => { |
| 45 | + const result = t("Card purchase"); |
| 46 | + expect(result).toStrictEqual({ en: "Card purchase", es: "Compra con tarjeta" }); // cspell:ignore Compra tarjeta |
| 47 | + }); |
| 48 | + |
| 49 | + it("interpolates plain string values into both languages", () => { |
| 50 | + const result = t("{{localAmount}} at {{merchantName}}. Paid with USDC", { |
| 51 | + localAmount: "$1,234.56", |
| 52 | + merchantName: "Store", |
| 53 | + }); |
| 54 | + expect(result).toStrictEqual({ |
| 55 | + en: "$1,234.56 at Store. Paid with USDC", |
| 56 | + es: "$1,234.56 en Store. Pagado con USDC", // cspell:ignore Pagado |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("resolves per-language objects in interpolation values", () => { |
| 61 | + const result = t("{{localAmount}} at {{merchantName}}. Paid with USDC", { |
| 62 | + localAmount: { en: "$1,234.56", es: "$ 1.234,56" }, |
| 63 | + merchantName: "Store", |
| 64 | + }); |
| 65 | + expect(result).toStrictEqual({ |
| 66 | + en: "$1,234.56 at Store. Paid with USDC", |
| 67 | + es: "$ 1.234,56 en Store. Pagado con USDC", // cspell:ignore Pagado |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("mixes per-language and plain values", () => { |
| 72 | + const result = t("{{localAmount}} at {{merchantName}}. Paid with credit", { |
| 73 | + localAmount: { en: "A", es: "B" }, |
| 74 | + merchantName: "Store", |
| 75 | + }); |
| 76 | + expect(result).toStrictEqual({ |
| 77 | + en: "A at Store. Paid with credit", |
| 78 | + es: "B en Store. Pagado con crédito", // cspell:ignore Pagado crédito |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments