|
8 | 8 | parseSimpleMetaVar, |
9 | 9 | parseScalingMetaVar, |
10 | 10 | parseListMetaVar, |
| 11 | + parseFixedValue, |
| 12 | + parseQuantityInput, |
11 | 13 | extractMetadata, |
12 | 14 | findAndUpsertCookware, |
13 | 15 | findAndUpsertIngredient, |
@@ -370,6 +372,21 @@ describe("findAndUpsertIngredient", () => { |
370 | 372 | expect(ingredients_noqtt[0]!.quantity).toBe(undefined); |
371 | 373 | }); |
372 | 374 |
|
| 375 | + it("should insert new ingredient if referenced ingredient has a text quantity", () => { |
| 376 | + const ingredients: Ingredient[] = [ |
| 377 | + { |
| 378 | + name: "eggs", |
| 379 | + quantity: { type: "fixed", value: { type: "text", value: "one" } }, |
| 380 | + }, |
| 381 | + ]; |
| 382 | + const newIngredient: Ingredient = { |
| 383 | + name: "eggs", |
| 384 | + quantity: { type: "fixed", value: { type: "decimal", value: 1 } }, |
| 385 | + }; |
| 386 | + expect(findAndUpsertIngredient(ingredients, newIngredient, true)).toBe(1); |
| 387 | + expect(ingredients).toHaveLength(2); |
| 388 | + }); |
| 389 | + |
373 | 390 | it("should adopt quantity of new ingredient if referenced one has none", () => { |
374 | 391 | const ingredients: Ingredient[] = [{ name: "eggs" }]; |
375 | 392 | const newIngredient: Ingredient = { |
@@ -402,3 +419,23 @@ describe("findAndUpsertIngredient", () => { |
402 | 419 | ); |
403 | 420 | }); |
404 | 421 | }); |
| 422 | + |
| 423 | +describe("parseFixedValue", () => { |
| 424 | + it("parses non numerical value as text", () => { |
| 425 | + expect(parseFixedValue("1-ish")).toEqual({ type: "text", value: "1-ish" }); |
| 426 | + }); |
| 427 | + |
| 428 | + it("parses fractions as such", () => { |
| 429 | + expect(parseFixedValue("1/2")).toEqual({ |
| 430 | + type: "fraction", |
| 431 | + num: 1, |
| 432 | + den: 2, |
| 433 | + }); |
| 434 | + }); |
| 435 | + |
| 436 | + it("parses decimal values as such", () => { |
| 437 | + expect(parseFixedValue("1.5")).toEqual({ type: "decimal", value: 1.5 }); |
| 438 | + expect(parseFixedValue("0.1")).toEqual({ type: "decimal", value: 0.1 }); |
| 439 | + expect(parseFixedValue("1")).toEqual({ type: "decimal", value: 1 }); |
| 440 | + }); |
| 441 | +}); |
0 commit comments