Closed JamieCaudill closed 1 year ago
//Write tests for a function that returns a filtered list of recipes based on recipe name from repository with everything about the recepies. // Meyby there should be a function with object named recepieInfo or archiveOfReceies/recepieRepository?
// function recipeRepository(recipeData, ingredientsData){ // const recipeInfo = { // id: recipeInfo.id, // image: recipeInfo.image, // ingredients: getRecipeIngredients(recipeData, ingredientsData), // instructions: recipeInfo.instructions, // name: recipeInfo.name, // tags: recipeInfo.tags // }; // return recipeInfo; // }; // recipeRepository(recipeData)
// from givin data along with recipes.js it looks like there is ingredients.js file that have ingredientsData. if we look in to recipes.js we see that ingredients should have a function with ingredients information object...see below:
// const recipeData = [ // { // recipeData.id // id: 595736,
// recipeData.image // image: "https://spoonacular.com/recipeImages/595736-556x370.jpg",
// recipeData.ingredients -> [] of {} with 2 key: value pair; one is id: number and the second is quantity: with {amount: number, unit: string} // ingredients: [ // { // id: 20081, // quantity: { // amount: 1.5, // unit: "c" // } // }, // { // id: 18372, // quantity: { // amount: 0.5, // unit: "tsp" // } // } // ... 4 more ingredients // ],
// recipeData.instractions -> [] of 6 {}; every instruction has a number to it. // instructions: [ // { // instruction: "In a large mixing bowl, whisk together the dry ingredients (flour, pudding mix, soda and salt). Set aside.In a large mixing bowl of a stand mixer, cream butter for 30 seconds. Gradually add granulated sugar and brown sugar and cream until light and fluffy.", // number: 1 // }, // { // instruction: "Add egg and vanilla and mix until combined.", // number: 2 // } //... 4 more instructions // ],
// recipeData.name - > string // name: "Loaded Chocolate Chip Pudding Cookie Cups",
// recipeData.tag -> [] // tags: [ // "antipasti", // "starter", // "snack", // "appetizer", // "antipasto", // "hor d'oeuvre" // ] // } // ...recipeData // ];
// therefore mayby function below should be added:
// function ingredienteRpository(){ // const ingredientsData = { // id: ingredientsData.id, // name: ingredientsData.name, // ingredientCost: ingredientsData.estimatedCostInCents // }; // return ingredientsData; // }; // ingredienteRpository();
// function filter by name. // we want to iterate through the recipeRepository and return a subset of data that is fitered by the selected name // .filter() // parameter of the recipesData (arr. of objects), refer to above recipeData example. // recipeData[index][name] // look at each recipe, if the recipe.name === name filter to a filterByName array.
// use toLowerCase() method; from mdn: Searching in array // Following example uses filter() to filter array content based on search criteria.
// const fruits = ["apple", "banana", "grapes", "mango", "orange"];
// /* // Filter array items based on search criteria (query) // */ // function filterItems(arr, query) { // return arr.filter((el) => el.toLowerCase().includes(query.toLowerCase())); // }
// console.log(filterItems(fruits, "ap")); // ['apple', 'grapes'] // console.log(filterItems(fruits, "an")); // ['banana', 'mango', 'orange']
// return filteredRecipesByName array:
//ES5
// function filterByName(recipeData, name) {
// var filteredRecipesByName = [];
// for(var i = 0; i < recipeData.length; i++){
// if(recipeData[i].name === name){
// filteredRecipesByName.includes(recipeData[i].name.toLowerCase());
// filteredRecipesByName.push(recipeData[i].name.toLowerCase());
// }else{
// return ${name} didn't match recipes database.
// };
// };
// return filteredRecipesByName;
// };
//ES6
// let filteredRecipesByName = recipeData.filter((recipe) => {
// return recipe.name.toLowerCase().includes(recipe.name.toLowerCase());
// });
// return filteredRecipesByName;
// Test.js // import { expect } from 'chai'; // import RecipeRepository from '../src/RecipeRepository'; // import recipeSampleData from '../src/data/recipes-sample'; // import ingredientsSampleData from '../src/data/ingredients-sample';
// describe('RecipeRepository', () => { // let sampleRecipes, recipeRepository;
// beforeEach('instantiate RecipeRepository', () => { // sampleRecipes = recipeSampleData; // recipeRepository = RecipeRepository(sampleRecipes, ingredientsSampleData); // });
// it('Should be a function', () => { // expect(RecipeRepository).to.be.a('function'); // }); // it('Should be an instance of RecipeRepository', () => { // expect(recipeRepository).to.be.an.instanceOf(RecipeRepository); // }); // recipeRepository(recipeData, ingredientsData);
// function recipeRepository(recipeData, ingredientsData){ // const recipeInfo = { // id: recipeInfo.id, // image: recipeInfo.image, // ingredients: getRecipeIngredients(recipeData, ingredientsData), // instructions: recipeInfo.instructions, // name: recipeInfo.name, // tags: recipeInfo.tags // }; // return recipeInfo; // };
// recipeRepository(recipeData, ingredientsData)
// it('should return a filtered list based on name', () => { // const snackRecipes = recipeRepository.filterByName('Loaded Chocolate Chip Pudding Cookie Cups'); // expect(snackRecipes[0].name).to.deep.equal(recipeRepository.recipes[0].name); // });
// it('should return a message if name not matching', () => {
// const snackRecipes = recipeRepository.filterByName(Name didn't match recipes database.
);
// expect(snackRecipes).to.deep.equal([]);
// });
// });
Parvin, could you please simplify your code? There is a lot going on here that I don't follow. All this issue needs is a simple test function that could be 10-20 lines of code, and a solution function that could be less than 10 lines of code.
Yes, absolutely!
//function filter by name // we want to iterate through the recipes and return a subset of data that is fitered by the selected name // .filter() // input: parameter of the recipesData (arr. of objects), and the name to filter // recipeData[index]["name"] // look at each recipe, if the recipe.name === selectedName filter to a selectName array // output: return selectedName array
// describe filterByName function // happy path: // it should be a function; it('Should be a function', () => {} // expect fiterByName to be a function // should return a filtered array list based on name; it('should return a filtered list based on name', () => {} // expect filterByName(recipeData, name) to deep equal [array of name recipes]
// sad path:
// it should return undefined if the name does not exist
//it('should return a message if name not matching', () => {}
// expect filterByName(recipeData, undefined) to return []
// expect Name didn't match recipes database.
?
This looks way way way better! Thank you Parvin!
Thank you for being patient with me and my English Jamie!