Files

46 lines
1.8 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const { parseYield, scaleIngredient, scaleIngredientWithStatus, scaleRecipe } = require("../lib/recipe-scaler");
test("extracts the number of portions from a recipe yield", () => {
assert.equal(parseYield("6 servings"), 6);
assert.equal(parseYield(4), 4);
assert.equal(parseYield("as needed"), null);
});
test("scales leading whole, fractional, and mixed quantities", () => {
assert.equal(scaleIngredient("2 cups oats", 2), "4 cups oats");
assert.equal(scaleIngredient("½ cup milk", 2), "1 cup milk");
assert.equal(scaleIngredient("1 1/2 tsp salt", 2), "3 tsp salt");
assert.equal(scaleIngredient("Salt to taste", 2), "Salt to taste");
});
test("returns a scaled copy of a recipe", () => {
const original = {
name: "Oatmeal Cups",
recipeYield: "6 servings",
recipeIngredient: ["2 cups oats", "Salt to taste"]
};
const scaled = scaleRecipe(original, 9);
assert.equal(scaled.recipeYield, "9 servings");
assert.deepEqual(scaled.recipeIngredient, ["3 cups oats", "Salt to taste"]);
assert.equal(original.recipeYield, "6 servings");
assert.equal(scaled.originalRecipeYield, "6 servings");
assert.deepEqual(scaled.recipeIngredientStatus, ["scaled", "unquantified"]);
});
test("reports ingredient scaling confidence", () => {
assert.deepEqual(scaleIngredientWithStatus("2 cups oats", 1.5), {
value: "3 cups oats", status: "scaled"
});
assert.deepEqual(scaleIngredientWithStatus("Salt to taste", 2), {
value: "Salt to taste", status: "unquantified"
});
assert.deepEqual(scaleIngredientWithStatus("1-2 tbsp oil", 2), {
value: "1-2 tbsp oil", status: "unscalable"
});
assert.deepEqual(scaleIngredientWithStatus("1 cup milk", 0.7), {
value: "0.7 cup milk", status: "approximate"
});
});