Skip to content

Commit 05897a7

Browse files
committed
fix(ShoppingList): only require choices expected for the selected variant when adding a recipe
1 parent 08f4ae6 commit 05897a7

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

src/classes/shopping_list.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,18 +579,20 @@ export class ShoppingList {
579579
recipe: Recipe,
580580
choices?: import("../types").RecipeChoices,
581581
): string | undefined {
582+
const activeVariant = choices?.variant;
583+
const expectedAlternatives = recipe.getChoicesForVariant(activeVariant);
582584
const missingItems: string[] = [];
583585
const missingGroups: string[] = [];
584586

585587
// Check for inline alternatives without choices
586-
for (const itemId of recipe.choices.ingredientItems.keys()) {
588+
for (const itemId of expectedAlternatives.ingredientItems.keys()) {
587589
if (!choices?.ingredientItems?.has(itemId)) {
588590
missingItems.push(itemId);
589591
}
590592
}
591593

592594
// Check for grouped alternatives without choices
593-
for (const groupId of recipe.choices.ingredientGroups.keys()) {
595+
for (const groupId of expectedAlternatives.ingredientGroups.keys()) {
594596
// v8 ignore else -- @preserve: detection if
595597
if (!choices?.ingredientGroups?.has(groupId)) {
596598
missingGroups.push(groupId);

test/shopping_list.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,57 @@ Mix @|milk|milk{200%ml} or @|milk|almond milk{100%ml}
740740
/Recipe has unresolved alternatives.*ingredientGroups.*milk/,
741741
);
742742
});
743+
744+
it("should only require choices expected for the selected variant", () => {
745+
const shoppingList = new ShoppingList();
746+
const recipeWithVariantLinkedChoices = new Recipe(`
747+
---
748+
servings: 1
749+
---
750+
[*] Add @milk{200%ml}|oat milk{200%ml}.
751+
752+
[vegan] Add @water{100%ml}|broth{100%ml}.
753+
754+
[*] Use @|protein|chicken{200%g} or @|protein|turkey{200%g}.
755+
756+
[vegan] Use @|protein|tofu{200%g} or @|protein|tempeh{200%g}.
757+
`);
758+
759+
expect(() =>
760+
shoppingList.addRecipe(recipeWithVariantLinkedChoices, {
761+
choices: {
762+
variant: "vegan",
763+
ingredientItems: new Map([["ingredient-item-1", 1]]),
764+
ingredientGroups: new Map([["protein", 1]]),
765+
},
766+
}),
767+
).not.toThrow();
768+
});
769+
770+
it("should only require choices expected for the default variant", () => {
771+
const shoppingList = new ShoppingList();
772+
const recipeWithVariantLinkedChoices = new Recipe(`
773+
---
774+
servings: 1
775+
---
776+
[*] Add @milk{200%ml}|oat milk{200%ml}.
777+
778+
[vegan] Add @water{100%ml}|broth{100%ml}[for vegan].
779+
780+
[*] Use @|protein|chicken{200%g} or @|protein|turkey{200%g}.
781+
782+
[vegan] Use @|protein|tofu{200%g}[for vegan] or @|protein|tempeh{200%g}[for vegan].
783+
`);
784+
785+
expect(() =>
786+
shoppingList.addRecipe(recipeWithVariantLinkedChoices, {
787+
choices: {
788+
ingredientItems: new Map([["ingredient-item-0", 1]]),
789+
ingredientGroups: new Map([["protein", 1]]),
790+
},
791+
}),
792+
).not.toThrow();
793+
});
743794
});
744795

745796
describe("Association with CategoryConfig", () => {

0 commit comments

Comments
 (0)