-
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.
Merge pull request #54 from hotungkhanh/kan-65/frontend-tests
Kan 65/frontend tests
- Loading branch information
Showing
14 changed files
with
1,492 additions
and
110 deletions.
There are no files selected for viewing
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
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
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,38 @@ | ||
import { afterEach, describe, it, expect } from 'vitest'; | ||
import { render, screen, cleanup } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/vitest'; | ||
import BackButton from '../../components/BackButton'; | ||
|
||
// Automatically clean up the DOM after each test | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
describe('BackButton', () => { | ||
it('renders as an active button by default', () => { | ||
// Render the default BackButton | ||
render(<BackButton />); | ||
|
||
const backButton = screen.getByRole('button'); | ||
|
||
// Check if the button is in the document | ||
expect(backButton).toBeInTheDocument(); | ||
|
||
// Check if the button is enabled (active by default) | ||
expect(backButton).toBeEnabled(); | ||
}); | ||
|
||
it('renders as a disabled button with props', () => { | ||
// Render the BackButton with the disabled prop set to true | ||
render(<BackButton disabled={true} />); | ||
|
||
const backButton = screen.getByRole('button'); | ||
|
||
// Check if the button is in the document | ||
expect(backButton).toBeInTheDocument(); | ||
|
||
// Check if the button is disabled | ||
expect(backButton).toBeDisabled(); | ||
}); | ||
|
||
}); |
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,30 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { render, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/vitest'; | ||
import DisplayFile from '../../components/DisplayFile'; | ||
|
||
describe('DisplayFile', () => { | ||
|
||
it('renders an empty Box when fileChosen is null', () => { | ||
const { container } = render(<DisplayFile fileChosen={null} />); | ||
|
||
// The Box is rendered, but nothing else | ||
const emptyBox = container.querySelector('.MuiBox-root'); | ||
expect(emptyBox).toBeInTheDocument(); | ||
expect(emptyBox).toBeEmptyDOMElement(); // Box should be empty when fileChosen is null | ||
}); | ||
|
||
it('renders the file name and FilePresentIcon when fileChosen is not null', () => { | ||
const mockFile = new File([''], 'example.txt', { type: 'text/plain' }); | ||
|
||
render(<DisplayFile fileChosen={mockFile} />); | ||
|
||
// Check that the file name is rendered | ||
const fileName = screen.getByText('example.txt'); | ||
expect(fileName).toBeInTheDocument(); | ||
|
||
// Check that the FilePresentIcon is rendered | ||
const fileIcon = screen.getByTestId('FilePresentIcon'); | ||
expect(fileIcon).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { afterEach, describe, it, expect, vi } from 'vitest'; | ||
import { render, screen, cleanup } from '@testing-library/react'; | ||
import LoadingButton from '../../components/LoadingButton'; | ||
import '@testing-library/jest-dom/vitest'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
// Automatically clean up the DOM after each test | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
describe('LoadingButton', () => { | ||
|
||
it('shows a disabled button with CircularProgress and no text when loading is true', () => { | ||
render( | ||
<LoadingButton | ||
loading={true} | ||
onClick={() => {}} | ||
text="Submit" | ||
/> | ||
); | ||
|
||
// Check that the button is disabled | ||
const button = screen.getByRole('button'); | ||
expect(button).toBeDisabled(); | ||
|
||
// Check that CircularProgress is present | ||
const circularProgress = screen.getByRole('progressbar'); | ||
expect(circularProgress).toBeInTheDocument(); | ||
|
||
// Check that the button does not display the text "Submit" | ||
expect(button).not.toHaveTextContent('Submit'); | ||
}); | ||
|
||
it('shows an enabled button with text and no CircularProgress when loading is false', () => { | ||
render( | ||
<LoadingButton | ||
loading={false} | ||
onClick={() => {}} | ||
text="Submit" | ||
/> | ||
); | ||
|
||
// Check that the button is enabled | ||
const button = screen.getByRole('button'); | ||
expect(button).toBeEnabled(); | ||
|
||
// Check that CircularProgress is not present | ||
const circularProgress = screen.queryByRole('progressbar'); | ||
expect(circularProgress).not.toBeInTheDocument(); | ||
|
||
// Check that the button displays the text "Submit" | ||
expect(button).toHaveTextContent('Submit'); | ||
}); | ||
|
||
it('triggers the onClick function when clicked and not loading', async () => { | ||
const handleClick = vi.fn(); | ||
|
||
render( | ||
<LoadingButton | ||
loading={false} | ||
onClick={handleClick} | ||
text="Submit" | ||
/> | ||
); | ||
|
||
// Simulate user clicking the button | ||
const button = screen.getByRole('button'); | ||
const user = userEvent.setup(); | ||
await user.click(button); | ||
|
||
// Ensure the onClick function is called | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
}); |
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,34 @@ | ||
import { afterEach, describe, it, expect } from 'vitest'; | ||
import { render, screen, cleanup } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/vitest'; // For jest-dom matchers | ||
import LoadingModal from '../../components/LoadingModel'; | ||
|
||
// Automatically clean up the DOM after each test | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
describe('LoadingModal', () => { | ||
it('renders the modal with CircularProgress when open is true', () => { | ||
render(<LoadingModal open={true} />); | ||
|
||
// Modal should be present | ||
const modal = screen.getByRole('presentation'); | ||
expect(modal).toBeInTheDocument(); | ||
|
||
// CircularProgress should be present inside the modal | ||
const progress = screen.getByRole('progressbar'); | ||
expect(progress).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render the modal or CircularProgress when open is false', () => { | ||
render(<LoadingModal open={false} />); | ||
|
||
// Modal and CircularProgress should not be present | ||
const modal = screen.queryByRole('presentation'); | ||
expect(modal).not.toBeInTheDocument(); | ||
|
||
const progress = screen.queryByRole('progressbar'); | ||
expect(progress).not.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { afterEach, describe, it, expect, vi } from 'vitest'; | ||
import { render, screen, cleanup } from '@testing-library/react'; | ||
import ProceedButton from '../../components/ProceedButton'; | ||
import { MemoryRouter, useNavigate } from 'react-router-dom'; | ||
import '@testing-library/jest-dom/vitest'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
// Automatically clean up the DOM after each test | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
// Mock useNavigate to test navigation behavior | ||
vi.mock(import('react-router-dom'), async (importOriginal) => { | ||
const actual = await importOriginal(); | ||
return { | ||
...actual, | ||
useNavigate: vi.fn(), // Mock useNavigate only | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); // Clear mocks after each test | ||
}); | ||
|
||
describe('ProceedButton', () => { | ||
it('renders a disabled button when fileChosen is null', () => { | ||
render( | ||
<MemoryRouter> | ||
<ProceedButton fileChosen={null} /> | ||
</MemoryRouter> | ||
); | ||
|
||
// Check that the button is disabled | ||
const button = screen.getByRole('button', { name: /proceed/i }); | ||
expect(button).toBeDisabled(); | ||
}); | ||
|
||
it('renders an enabled button and triggers navigation when fileChosen is provided', async () => { | ||
const navigateMock = vi.fn(); | ||
vi.mocked(useNavigate).mockReturnValue(navigateMock); | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<MemoryRouter> | ||
<ProceedButton fileChosen={new File(["content"], "testFile.txt")} /> | ||
</MemoryRouter> | ||
); | ||
|
||
// Check that the button is enabled | ||
const button = screen.getByRole('button', { name: /proceed/i }); | ||
expect(button).toBeEnabled(); | ||
|
||
// Simulate clicking the button | ||
await user.click(button); | ||
|
||
// Ensure that navigate is called with the correct route | ||
expect(navigateMock).toHaveBeenCalledWith('/seminfo/room'); | ||
}); | ||
}); |
Oops, something went wrong.