-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
React Component Testing for ImageCard and SideBar added
- Loading branch information
1 parent
c7608da
commit 34a631a
Showing
7 changed files
with
6,507 additions
and
3,797 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "presets": ["@babel/preset-env"] } |
Large diffs are not rendered by default.
Oops, something went wrong.
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,4 @@ | ||
import renderer from 'react-test-renderer'; | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; |
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
45 changes: 45 additions & 0 deletions
45
view_generator_serv/src/components/__test__/ImageCard.test.jsx
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,45 @@ | ||
// ImageCard.test.jsx | ||
import React from 'react'; | ||
import { render, fireEvent, screen } from '@testing-library/react'; | ||
import ImageCard from '../ImageCard'; | ||
|
||
const mockProps = { | ||
id: '1', | ||
imageName: 'Test Image', | ||
imageUri: 'http://test.com/image.jpg', | ||
imageSize: 1024, | ||
onRename: jest.fn(), | ||
onDownload: jest.fn(), | ||
onDelete: jest.fn(), | ||
}; | ||
|
||
describe('ImageCard', () => { | ||
beforeEach(() => { | ||
render(<ImageCard {...mockProps} />); | ||
}); | ||
|
||
it('renders image name', () => { | ||
expect(screen.getByText(mockProps.imageName)).toBeTruthy(); | ||
}); | ||
|
||
it('renders image size', () => { | ||
expect(screen.getByText('1.00 KB')).toBeTruthy(); | ||
}); | ||
|
||
it('calls onRename when save is clicked', () => { | ||
fireEvent.click(screen.getByText('Rename')); | ||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'New Name' } }); | ||
fireEvent.click(screen.getByText('Save')); | ||
expect(mockProps.onRename).toHaveBeenCalledWith(mockProps.id, 'New Name'); | ||
}); | ||
|
||
it('calls onDownload when download is clicked', () => { | ||
fireEvent.click(screen.getByText('Download')); | ||
expect(mockProps.onDownload).toHaveBeenCalledWith(mockProps.id); | ||
}); | ||
|
||
it('calls onDelete when delete is clicked', () => { | ||
fireEvent.click(screen.getByText('Delete')); | ||
expect(mockProps.onDelete).toHaveBeenCalledWith(mockProps.id); | ||
}); | ||
}); |
70 changes: 70 additions & 0 deletions
70
view_generator_serv/src/components/__test__/Sidebar.test.jsx
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,70 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import '@testing-library/jest-dom'; | ||
import { BrowserRouter as Router } from 'react-router-dom'; | ||
import { useAuthContext } from '../../hooks/useAuthContext'; | ||
import { useLogout } from '../../hooks/useLogout'; | ||
import Sidebar from '../Sidebar'; | ||
import axios from 'axios'; | ||
|
||
jest.mock('../../hooks/useAuthContext'); | ||
jest.mock('../../hooks/useLogout'); | ||
jest.mock('axios'); | ||
|
||
describe('Sidebar', () => { | ||
beforeEach(() => { | ||
useAuthContext.mockReturnValue({ | ||
user: { | ||
_id: '123', | ||
username: 'testuser', | ||
email: '[email protected]' | ||
} | ||
}); | ||
|
||
useLogout.mockReturnValue({ | ||
logout: jest.fn() | ||
}); | ||
|
||
axios.get.mockResolvedValue({ | ||
data: { | ||
usedStorage: 5000000, | ||
totalStorage: 10000000 | ||
} | ||
}); | ||
}); | ||
|
||
it('renders user information correctly', async () => { | ||
const { getByText } = render(<Router><Sidebar /></Router>); | ||
await waitFor(() => expect(getByText('testuser')).toBeInTheDocument()); | ||
expect(getByText('[email protected]')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders storage usage correctly', async () => { | ||
render(<Router><Sidebar /></Router>); | ||
|
||
// Find the Progress component using its aria-label | ||
const progressBar = screen.getByRole("progressbar"); | ||
expect(progressBar).toBeInTheDocument(); | ||
|
||
// Wait until the aria-valuenow attribute is "50" | ||
await waitFor(() => { | ||
expect(progressBar).toHaveAttribute('aria-valuenow', '50'); | ||
}); | ||
}); | ||
|
||
it('handles error when fetching storage data', async () => { | ||
const error = new Error('Network Error'); | ||
axios.get.mockRejectedValue(error); | ||
console.error = jest.fn(); | ||
|
||
render(<Router><Sidebar /></Router>); | ||
await waitFor(() => expect(console.error).toHaveBeenCalledWith(error)); | ||
}); | ||
|
||
it('calls logout function when sign out button is clicked', async () => { | ||
const { getByText } = render(<Router><Sidebar /></Router>); | ||
fireEvent.click(getByText('Sign Out')); | ||
expect(useLogout().logout).toHaveBeenCalled(); | ||
}); | ||
}); |