|
| 1 | +const { |
| 2 | + mapOpenFoodFactsProduct, |
| 3 | + searchOpenFoodFacts, |
| 4 | + searchOpenFoodFactsByBarcodeFields, |
| 5 | +} = require("../integrations/openfoodfacts/openFoodFactsService"); |
| 6 | + |
| 7 | +global.fetch = jest.fn(); |
| 8 | + |
| 9 | +describe("OpenFoodFacts Language Handling", () => { |
| 10 | + beforeEach(() => { |
| 11 | + jest.clearAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + describe("mapOpenFoodFactsProduct (Fallback Logic)", () => { |
| 15 | + const mockProduct = { |
| 16 | + code: "8076809529419", |
| 17 | + product_name: "Pâtes spaghetti au blé complet integral 500g", |
| 18 | + product_name_en: "Integrale Whole Wheat Spaghetti", |
| 19 | + product_name_fr: "Pâtes spaghetti au blé complet", |
| 20 | + brands: "Barilla", |
| 21 | + nutriments: {}, |
| 22 | + }; |
| 23 | + |
| 24 | + it("should use language-specific name if available", () => { |
| 25 | + const result = mapOpenFoodFactsProduct(mockProduct, { language: "fr" }); |
| 26 | + expect(result.name).toBe("Pâtes spaghetti au blé complet"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should fall back to English if requested language name is missing", () => { |
| 30 | + const result = mapOpenFoodFactsProduct(mockProduct, { language: "de" }); |
| 31 | + expect(result.name).toBe("Integrale Whole Wheat Spaghetti"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should fall back to default product_name if both requested and English names are missing", () => { |
| 35 | + const productNoEn = { |
| 36 | + ...mockProduct, |
| 37 | + product_name_en: undefined, |
| 38 | + product_name_fr: undefined, |
| 39 | + }; |
| 40 | + const result = mapOpenFoodFactsProduct(productNoEn, { language: "fr" }); |
| 41 | + expect(result.name).toBe("Pâtes spaghetti au blé complet integral 500g"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should prioritize English even if it is the requested language", () => { |
| 45 | + const result = mapOpenFoodFactsProduct(mockProduct, { language: "en" }); |
| 46 | + expect(result.name).toBe("Integrale Whole Wheat Spaghetti"); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe("API Request URL generation", () => { |
| 51 | + it("should include product_name_${language} in the fields for search", async () => { |
| 52 | + fetch.mockResolvedValue({ |
| 53 | + ok: true, |
| 54 | + json: () => Promise.resolve({ products: [], count: 0 }), |
| 55 | + }); |
| 56 | + |
| 57 | + await searchOpenFoodFacts("spaghetti", 1, "fr"); |
| 58 | + |
| 59 | + expect(fetch).toHaveBeenCalledWith( |
| 60 | + expect.stringContaining("product_name_fr"), |
| 61 | + expect.any(Object) |
| 62 | + ); |
| 63 | + expect(fetch).toHaveBeenCalledWith( |
| 64 | + expect.stringContaining("product_name_en"), |
| 65 | + expect.any(Object) |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should include product_name_${language} in the fields for barcode lookup", async () => { |
| 70 | + fetch.mockResolvedValue({ |
| 71 | + ok: true, |
| 72 | + json: () => Promise.resolve({ status: 1, product: {} }), |
| 73 | + }); |
| 74 | + |
| 75 | + await searchOpenFoodFactsByBarcodeFields("12345678", undefined, "it"); |
| 76 | + |
| 77 | + expect(fetch).toHaveBeenCalledWith( |
| 78 | + expect.stringContaining("product_name_it"), |
| 79 | + expect.any(Object) |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should not duplicate product_name_en if language is en", async () => { |
| 84 | + fetch.mockResolvedValue({ |
| 85 | + ok: true, |
| 86 | + json: () => Promise.resolve({ status: 1, product: {} }), |
| 87 | + }); |
| 88 | + |
| 89 | + await searchOpenFoodFactsByBarcodeFields("12345678", undefined, "en"); |
| 90 | + |
| 91 | + const url = fetch.mock.calls[0][0]; |
| 92 | + const fields = new URL(url).searchParams.get("fields").split(","); |
| 93 | + const enOccurrences = fields.filter(f => f === "product_name_en").length; |
| 94 | + expect(enOccurrences).toBe(1); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments