import { describe, expect, it } from "vitest"; import { localized } from "../index"; const recipe = { title: "Ratatouille du jardin", title_en: "Garden ratatouille", title_de: "", steps: ["Couper", "Cuire"], steps_en: ["Cut", "Cook"], steps_de: [] as string[], chef_tip: null as string | null, }; describe("localized", () => { it("reads the field of the language", () => { expect(localized(recipe, "title", "en")).toBe("Garden ratatouille"); expect(localized(recipe, "steps", "en-GB")).toEqual(["Cut", "Cook"]); }); it("falls back to the main language's field", () => { // The main language has no suffix, a language without translation neither. expect(localized(recipe, "title", "fr")).toBe("Ratatouille du jardin"); expect(localized(recipe, "title", "it")).toBe("Ratatouille du jardin"); // An empty translation is no translation. expect(localized(recipe, "title", "de")).toBe("Ratatouille du jardin"); expect(localized(recipe, "steps", "de")).toEqual(["Couper", "Cuire"]); expect(localized(recipe, "chef_tip", "en")).toBeNull(); }); });