From ffee8fe99b1514b6a04ac95123677e35d03dbcd0 Mon Sep 17 00:00:00 2001 From: Rutvik Vishwas Kulkarni Date: Fri, 1 Nov 2024 16:12:45 -0400 Subject: [PATCH] Update existing tests and added tests --- Code/frontend/jest.config.js | 6 + Code/frontend/package.json | 6 + Code/frontend/src/App.test.js | 52 +-- Code/frontend/src/components/AddRecipe.js | 231 ------------- Code/frontend/src/components/Form.js | 316 ------------------ Code/frontend/src/components/Header.js | 10 - Code/frontend/src/components/Login.js | 1 + Code/frontend/src/components/Recipe.js | 78 ----- Code/frontend/src/components/RecipeCard.js | 50 --- Code/frontend/src/components/RecipeList.js | 66 ---- .../frontend/src/components/SearchByRecipe.js | 41 --- .../src/components/__tests__/Form.test.js | 29 -- .../src/components/__tests__/Login.test.js | 33 +- .../components/__tests__/RecipeCard.test.js | 23 -- .../components/__tests__/RecipeList.test.js | 69 ---- .../src/service/firestoreService.test.js | 147 ++++++++ 16 files changed, 222 insertions(+), 936 deletions(-) create mode 100644 Code/frontend/jest.config.js delete mode 100644 Code/frontend/src/components/AddRecipe.js delete mode 100644 Code/frontend/src/components/Form.js delete mode 100644 Code/frontend/src/components/Header.js delete mode 100644 Code/frontend/src/components/Recipe.js delete mode 100644 Code/frontend/src/components/RecipeCard.js delete mode 100644 Code/frontend/src/components/RecipeList.js delete mode 100644 Code/frontend/src/components/SearchByRecipe.js delete mode 100644 Code/frontend/src/components/__tests__/Form.test.js delete mode 100644 Code/frontend/src/components/__tests__/RecipeCard.test.js delete mode 100644 Code/frontend/src/components/__tests__/RecipeList.test.js create mode 100644 Code/frontend/src/service/firestoreService.test.js diff --git a/Code/frontend/jest.config.js b/Code/frontend/jest.config.js new file mode 100644 index 00000000..c576fa08 --- /dev/null +++ b/Code/frontend/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + testEnvironment: 'node', + transform: { + '^.+\\.jsx?$': 'babel-jest', + }, +}; \ No newline at end of file diff --git a/Code/frontend/package.json b/Code/frontend/package.json index 1365d334..bf5fa0a1 100644 --- a/Code/frontend/package.json +++ b/Code/frontend/package.json @@ -4,6 +4,7 @@ "private": true, "dependencies": { "@chakra-ui/react": "^2.10.3", + "@chakra-ui/utils": "^2.2.2", "@emotion/react": "^11.13.3", "@emotion/styled": "^11.13.0", "@testing-library/jest-dom": "^5.14.1", @@ -27,6 +28,7 @@ "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", + "test:firestore": "react-scripts test src/service/firestoreService.test.js", "eject": "react-scripts eject" }, "eslintConfig": { @@ -46,5 +48,9 @@ "last 1 firefox version", "last 1 safari version" ] + }, + "devDependencies": { + "@firebase/testing": "^0.20.11", + "jest": "^27.5.1" } } diff --git a/Code/frontend/src/App.test.js b/Code/frontend/src/App.test.js index 7cd6cfb4..53cd75f5 100644 --- a/Code/frontend/src/App.test.js +++ b/Code/frontend/src/App.test.js @@ -1,26 +1,36 @@ -import { render, screen } from "@testing-library/react"; +import { render, screen, fireEvent } from "@testing-library/react"; import App from "./App"; -test("renders learn react link", () => { - render(); - const linkElement = screen.getByText(/Recipe/i); - expect(linkElement).toBeInTheDocument(); -}); - -test("renders learn react link", () => { - render(); - const linkElement = screen.getByText(/Recommender/i); - expect(linkElement).toBeInTheDocument(); -}); +jest.mock("./components/Navbar", () => (props) =>
Navbar
); +jest.mock("./components/Login", () => (props) => ( +
+ + +
+)); +jest.mock("./components/BookMarksRecipeList", () => () =>
Bookmarks
); +jest.mock("./components/SearchBlock", () => () =>
Search Block
); +jest.mock("./firebase/auth", () => ({ + doSignInWithEmailAndPassword: jest.fn(), + doCreateUserWithEmailAndPassword: jest.fn(), + doSignOut: jest.fn(), +})); +jest.mock("./contexts/authContext/index", () => ({ + useAuth: () => ({ + currentUser: null, + userLoggedIn: false, + }), + AuthProvider: ({ children }) =>
{children}
, +})); -test("renders learn react link", () => { - render(); - const linkElement = screen.getByText(/Email/i); - expect(linkElement).toBeInTheDocument(); -}); +describe("App component", () => { + test("renders Navbar", () => { + render(); + expect(screen.getByText(/Navbar/i)).toBeInTheDocument(); + }); -test("renders learn react link", () => { - render(); - const linkElement = screen.getByText(/Ingredient/i); - expect(linkElement).toBeInTheDocument(); + test("renders Search Block by default", () => { + render(); + expect(screen.getByText(/Search Block/i)).toBeInTheDocument(); + }); }); diff --git a/Code/frontend/src/components/AddRecipe.js b/Code/frontend/src/components/AddRecipe.js deleted file mode 100644 index 5bccb843..00000000 --- a/Code/frontend/src/components/AddRecipe.js +++ /dev/null @@ -1,231 +0,0 @@ -import React from "react"; -import {Box, HStack, Text, Input, InputGroup, InputRightElement, Button, VStack, Textarea, Badge, Alert, AlertIcon} from "@chakra-ui/react"; -import recipeDB from "../apis/recipeDB"; - -const AddRecipe = () => { - - const [recipe, setRecipe] = React.useState({ - recipeName: "", - cookingTime: 0, - dietType: "", - recipeRating: 0, - cuisine: "", - recipeURL: "", - imageURL: "", - instructions: "", - ingredientCount: 0, - ingredients: [], - restaurants: [], - locations: [] - }); - - const [ingredientCount, setIngredientCount] = React.useState(0); - - const addIngredient = () => { - const ingredient = document.getElementById("ingredients").value; - setRecipe(prevValue => { - return { - ...prevValue, - ingredients: [...prevValue.ingredients, ingredient], - ingredientCount: prevValue.ingredientCount + 1 - } - }) - document.getElementById("ingredients").value = ""; - }; - - const addRestaurant = () => { - const restaurant = document.getElementById("restaurant").value; - setRecipe(prevValue => { - return { - ...prevValue, - restaurants: [...prevValue.restaurants, restaurant] - } - }) - document.getElementById("restaurant").value = ""; - }; - - const addLocation = () => { - const location = document.getElementById("location").value; - setRecipe(prevValue => { - return { - ...prevValue, - locations: [...prevValue.locations, location] - } - }) - document.getElementById("location").value = ""; - }; - - - const handleChange = (event) => { - setRecipe(prevValue => { - return { - ...prevValue, - [event.target.id]: event.target.value - } - }) - } - - const addRecipe = () => { - recipeDB.post("/recipes/addRecipe", recipe) - .then(res => { - console.log(res.data); - // clear all fields - setRecipe({ - recipeName: "", - cookingTime: 0, - dietType: "", - recipeRating: 0, - cuisine: "", - recipeURL: "", - imageURL: "", - instructions: "", - ingredientCount: 0, - ingredients: [], - restaurants: [], - locations: [] - }); - document.getElementById("recipeName").value = ""; - document.getElementById("cookingTime").value = ""; - document.getElementById("dietType").value = ""; - document.getElementById("recipeRating").value = ""; - document.getElementById("cuisine").value = ""; - document.getElementById("recipeURL").value = ""; - document.getElementById("imageURL").value = ""; - document.getElementById("instructions").value = ""; - - // Alert user that recipe was added - - - Recipe Added! - - }) - .catch(err => console.log(err)); - } - - const ingredientPrintHandler = () => { - const ingredientList = recipe.ingredients; - - var ingredient_list = ingredientList.map((ingredient) => - - {ingredient} - - ); - - return
    {ingredient_list}
; - } - - const ingredientRemoveHandler = (event) => { - const ingredient = event.target.id; - const ingredientList = recipe.ingredients; - const index = ingredientList.indexOf(ingredient); - if (index > -1) { - ingredientList.splice(index, 1); - } - setRecipe(prevValue => { - return { - ...prevValue, - ingredients: ingredientList - } - }) - } - - const restaurantPrintHandler = () => { - const restaurantList = recipe.restaurants; - - const restaurant_list = restaurantList.map((restaurant) => - - {restaurant} - - ); - - return
    {restaurant_list}
; - } - - const restaurantRemoveHandler = (event) => { - const restaurant = event.target.id; - const restaurantList = recipe.restaurants; - const index = restaurantList.indexOf(restaurant); - if (index > -1) { - restaurantList.splice(index, 1); - } - setRecipe(prevValue => { - return { - ...prevValue, - restaurants: restaurantList - } - }) - } - - const locationPrintHandler = () => { - const locationList = recipe.locations; - - const location_list = locationList.map((location) => - - {location} - - ); - } - - const locationRemoveHandler = (event) => { - const location = event.target.id; - const locationList = recipe.locations; - const index = locationList.indexOf(location); - if (index > -1) { - locationList.splice(index, 1); - } - setRecipe(prevValue => { - return { - ...prevValue, - locations: locationList - } - }) - } - - return ( - <> - - Add New Recipe - - - - - - - - - - - - - - - - - - - - {ingredientPrintHandler()} - - - - - - {restaurantPrintHandler()} - - - - - - - {locationPrintHandler()} - - -