forked from pnprathima/Recipe_Recommender
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for Navbar, Login and BookmarkRecipe component
- Loading branch information
1 parent
ffee8fe
commit 700db7d
Showing
4 changed files
with
148 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import Nav from '../Navbar'; | ||
|
||
jest.mock('@chakra-ui/react', () => { | ||
return { | ||
Box: ({ children, ...props }) => <div {...props}>{children}</div>, | ||
Flex: ({ children, ...props }) => <div {...props}>{children}</div>, | ||
Avatar: ({ dataTestId = 'mock-avatar', ...props }) => <div data-testid={dataTestId} {...props} />, | ||
Text: (props) => <span {...props}>{props.children}</span>, | ||
Button: ({ children, ...props }) => <button {...props}>{children}</button>, | ||
Menu: ({ children }) => <div>{children}</div>, | ||
MenuButton: ({ children }) => <button>{children}</button>, | ||
MenuList: ({ children }) => <div>{children}</div>, | ||
MenuItem: ({ children, onClick }) => <div onClick={onClick}>{children}</div>, | ||
MenuDivider: () => <hr />, | ||
Stack: ({ children, ...props }) => <div {...props}>{children}</div>, | ||
Center: ({ children, ...props }) => <div style={{ textAlign: 'center' }} {...props}>{children}</div>, | ||
Heading: ({ children, ...props }) => <h1 {...props}>{children}</h1>, | ||
}; | ||
}); | ||
|
||
describe('Navbar component', () => { | ||
const mockHandleBookMarks = jest.fn(); | ||
const mockHandleLogout = jest.fn(); | ||
const mockToggleLoginModal = jest.fn(); | ||
|
||
test('renders logo and app name', () => { | ||
render(<Nav />); | ||
const logoImg = screen.getByRole('img'); | ||
const appName = screen.getByText('CookSmart'); | ||
|
||
expect(logoImg).toBeInTheDocument(); | ||
expect(appName).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders login button when user is not logged in', () => { | ||
render(<Nav toggleLoginModal={mockToggleLoginModal} userLoggedIn={false} />); | ||
const loginButton = screen.getByRole('button', { name: /login/i }); | ||
|
||
expect(loginButton).toBeInTheDocument(); | ||
}); | ||
|
||
test('calls toggleLoginModal when login button is clicked', () => { | ||
render(<Nav toggleLoginModal={mockToggleLoginModal} userLoggedIn={false} />); | ||
const loginButton = screen.getByRole('button', { name: /login/i }); | ||
fireEvent.click(loginButton); | ||
|
||
expect(mockToggleLoginModal).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('displays user avatar and email when logged in', () => { | ||
render( | ||
<Nav | ||
userLoggedIn={true} | ||
currentUser={{ email: 'test@example.com' }} | ||
handleBookMarks={mockHandleBookMarks} | ||
handleLogout={mockHandleLogout} | ||
/> | ||
); | ||
|
||
const avatar = screen.getAllByTestId('mock-avatar'); | ||
fireEvent.click(avatar[0]); | ||
|
||
expect(screen.getByText('test@example.com')).toBeInTheDocument(); | ||
}); | ||
|
||
test('calls handleBookMarks when Bookmarks menu item is clicked', () => { | ||
render( | ||
<Nav | ||
userLoggedIn={true} | ||
currentUser={{ email: 'test@example.com' }} | ||
handleBookMarks={mockHandleBookMarks} | ||
handleLogout={mockHandleLogout} | ||
/> | ||
); | ||
|
||
fireEvent.click(screen.getAllByTestId('mock-avatar')[0]); | ||
const bookmarksItem = screen.getByText(/bookmarks/i); | ||
fireEvent.click(bookmarksItem); | ||
|
||
expect(mockHandleBookMarks).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('calls handleLogout when Logout menu item is clicked', () => { | ||
render( | ||
<Nav | ||
userLoggedIn={true} | ||
currentUser={{ email: 'test@example.com' }} | ||
handleBookMarks={mockHandleBookMarks} | ||
handleLogout={mockHandleLogout} | ||
/> | ||
); | ||
|
||
fireEvent.click(screen.getAllByTestId('mock-avatar')[0]); | ||
const logoutItem = screen.getByText(/logout/i); | ||
fireEvent.click(logoutItem); | ||
|
||
expect(mockHandleLogout).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
Code/frontend/src/components/__tests__/SearchBlock.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import SearchBlock from '../SearchBlock'; | ||
import { fetchBookmarkedRecipes, fetchBookmarkedIngredients, bookmarkRecipe, unbookmarkRecipe, isRecipeBookmarked } from '../../service/firestoreService'; | ||
import { useAuth } from '../../contexts/authContext/index'; | ||
import axios from 'axios'; | ||
import jsPDF from 'jspdf'; | ||
|
||
jest.setTimeout(20000); | ||
|
||
jest.mock('axios'); | ||
jest.mock('jspdf'); | ||
jest.mock('../../contexts/authContext/index', () => ({ | ||
useAuth: jest.fn(), | ||
})); | ||
jest.mock('../../service/firestoreService', () => ({ | ||
bookmarkRecipe: jest.fn(), | ||
unbookmarkRecipe: jest.fn(), | ||
isRecipeBookmarked: jest.fn(), | ||
})); | ||
|
||
describe('SearchBlock Component', () => { | ||
beforeEach(() => { | ||
useAuth.mockReturnValue({ userLoggedIn: true }); | ||
axios.post.mockResolvedValue({ | ||
data: { | ||
recipes: [ | ||
{ name: 'Pasta', ingredients: ['Tomato', 'Basil'], tags: ['Italian'], time: '30 minutes' }, | ||
{ name: 'Salad', ingredients: ['Lettuce', 'Carrot'], tags: ['Healthy'], time: '10 minutes' } | ||
], | ||
ingredients: ['Tomato', 'Basil'] | ||
} | ||
}); | ||
jsPDF.mockImplementation(() => ({ | ||
save: jest.fn(), | ||
})); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('renders correctly', () => { | ||
render(<SearchBlock />); | ||
expect(screen.getByPlaceholderText(/Add ingredients or search by name/i)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters