diff --git a/frontend/composables/recipes/use-recipe-ingredients.test.ts b/frontend/composables/recipes/use-recipe-ingredients.test.ts index 73d163e48409..b84a68741631 100644 --- a/frontend/composables/recipes/use-recipe-ingredients.test.ts +++ b/frontend/composables/recipes/use-recipe-ingredients.test.ts @@ -34,6 +34,12 @@ describe(parseIngredientText.name, () => { expect(parseIngredientText(ingredient, false, 1, true)).contain("1 1").and.to.contain("2"); }); + test("ingredient text with fraction when unit is null", () => { + const ingredient = createRecipeIngredient({ quantity: 1.5, unit: undefined }); + + expect(parseIngredientText(ingredient, false, 1, true)).contain("1 1").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, false, 1, false); diff --git a/frontend/composables/recipes/use-recipe-ingredients.ts b/frontend/composables/recipes/use-recipe-ingredients.ts index a31872652fa5..ae228ef6472c 100644 --- a/frontend/composables/recipes/use-recipe-ingredients.ts +++ b/frontend/composables/recipes/use-recipe-ingredients.ts @@ -53,7 +53,9 @@ export function useParsedIngredientText(ingredient: RecipeIngredient, disableAmo // casting to number is required as sometimes quantity is a string if (quantity && Number(quantity) !== 0) { - if (unit?.fraction) { + if (unit && !unit.fraction) { + returnQty = (quantity * scale).toString(); + } else { const fraction = frac(quantity * scale, 10, true); if (fraction[0] !== undefined && fraction[0] > 0) { returnQty += fraction[0]; @@ -64,8 +66,6 @@ export function useParsedIngredientText(ingredient: RecipeIngredient, disableAmo ` ${fraction[1]}${fraction[2]}` : ` ${fraction[1]}/${fraction[2]}`; } - } else { - returnQty = (quantity * scale).toString(); } } diff --git a/mealie/schema/recipe/recipe_ingredient.py b/mealie/schema/recipe/recipe_ingredient.py index 7609fa5486e4..2c8aea785181 100644 --- a/mealie/schema/recipe/recipe_ingredient.py +++ b/mealie/schema/recipe/recipe_ingredient.py @@ -187,7 +187,7 @@ class RecipeIngredientBase(MealieModel): qty: float | Fraction # decimal - if not self.unit or not self.unit.fraction: + if self.unit and not self.unit.fraction: qty = round(self.quantity or 0, INGREDIENT_QTY_PRECISION) if qty.is_integer(): return str(int(qty)) diff --git a/tests/integration_tests/user_recipe_tests/test_recipe_ingredients.py b/tests/integration_tests/user_recipe_tests/test_recipe_ingredients.py index 2ab7a37ecf66..9aadb9a20fda 100644 --- a/tests/integration_tests/user_recipe_tests/test_recipe_ingredients.py +++ b/tests/integration_tests/user_recipe_tests/test_recipe_ingredients.py @@ -2,8 +2,11 @@ from uuid import uuid4 import pytest -from mealie.schema.recipe.recipe_ingredient import IngredientFood, IngredientUnit, RecipeIngredient -from tests.utils.factories import random_string +from mealie.schema.recipe.recipe_ingredient import ( + IngredientFood, + IngredientUnit, + RecipeIngredient, +) @pytest.mark.parametrize( @@ -19,6 +22,12 @@ from tests.utils.factories import random_string @pytest.mark.parametrize( ["unit", "expect_display_fraction", "expected_unit_singular_string", "expected_unit_plural_string"], [ + [ + None, + True, + "", + "", + ], [ IngredientUnit( id=uuid4(), @@ -154,7 +163,7 @@ def test_ingredient_display( quantity: float | None, quantity_display_decimal: str, quantity_display_fraction: str, - unit: IngredientUnit, + unit: IngredientUnit | None, food: IngredientFood, note: str, use_food: bool,