import { describe, test, expect, vi, beforeEach } from "vitest"; import { parseIngredientText } from "./use-recipe-ingredients"; import type { RecipeIngredient } from "~/lib/api/types/recipe"; import { useLocales } from "../use-locales"; vi.mock("../use-locales"); describe(parseIngredientText.name, () => { beforeEach(() => { vi.clearAllMocks(); vi.mocked(useLocales).mockReturnValue({ locales: [{ value: "en-US", pluralFoodHandling: "always" }], locale: { value: "en-US", pluralFoodHandling: "always" }, } as any); }); const createRecipeIngredient = (overrides: Partial): RecipeIngredient => ({ quantity: 1, food: { id: "1", name: "Item 1", }, unit: { id: "1", name: "cup", }, ...overrides, }); test("adds note section if note present", () => { const ingredient = createRecipeIngredient({ note: "custom note" }); expect(parseIngredientText(ingredient)).toContain("custom note"); }); test("ingredient text with fraction", () => { const ingredient = createRecipeIngredient({ quantity: 1.5, unit: { fraction: true, id: "1", name: "cup" } }); expect(parseIngredientText(ingredient, 1, true)).contain("11").and.to.contain("2"); }); test("ingredient text with fraction when unit is null", () => { const ingredient = createRecipeIngredient({ quantity: 1.5, unit: undefined }); expect(parseIngredientText(ingredient, 1, true)).contain("11").and.to.contain("2"); }); test("ingredient text with fraction no formatting", () => { const ingredient = createRecipeIngredient({ quantity: 1.5, unit: { fraction: true, id: "1", name: "cup" } }); const result = parseIngredientText(ingredient, 1, false); expect(result).not.contain("<"); expect(result).not.contain(">"); expect(result).contain("1 1/2"); }); test("sanitizes html", () => { const ingredient = createRecipeIngredient({ note: "" }); expect(parseIngredientText(ingredient)).not.toContain("