Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [110] add error pages #149

Merged
merged 4 commits into from
Aug 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: [100] add test case
Cihatata authored and cihat committed Aug 30, 2024
commit 09dbd1647a8e982dab1f12885b2582db3a560bff
1 change: 1 addition & 0 deletions frontend/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom';
5 changes: 4 additions & 1 deletion frontend/src/scenes/errors/403.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from 'react'
import { ErrorComponent } from './Error'
import { NAVIGATE_403_URL, TEXT_403 } from './constant'
import { useNavigate } from 'react-router-dom';

export const Error403 = () => {
const navigate = useNavigate();

return (
<ErrorComponent text={TEXT_403} navigateUrl={NAVIGATE_403_URL} />
<ErrorComponent text={TEXT_403} errorAction={() => navigate(NAVIGATE_403_URL)} />
)
}
5 changes: 4 additions & 1 deletion frontend/src/scenes/errors/404.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from 'react'
import { ErrorComponent } from './Error';
import { NAVIGATE_404_URL, TEXT_404 } from './constant';
import { useNavigate } from 'react-router';

export const Error404 = () => {
const navigate = useNavigate();

return (
<ErrorComponent text={TEXT_404} navigateUrl={NAVIGATE_404_URL} />
<ErrorComponent text={TEXT_404} errorAction={() => navigate(NAVIGATE_404_URL)} />
)
}
8 changes: 3 additions & 5 deletions frontend/src/scenes/errors/Error.jsx
Original file line number Diff line number Diff line change
@@ -2,10 +2,8 @@ import React from 'react'
import PropTypes from "prop-types";
import styles from './Error.module.scss';
import Button from '../../components/Button/Button';
import { useNavigate } from 'react-router';

export const ErrorComponent = ({ text, navigateUrl }) => {
const navigate = useNavigate()
export const ErrorComponent = ({ text, errorAction }) => {
const errorButtonStyle = {
borderRadius: '8px',
marginTop: '58px',
@@ -22,7 +20,7 @@ export const ErrorComponent = ({ text, navigateUrl }) => {
</div>
<Button
style={errorButtonStyle}
onClick={() => navigate(navigateUrl)}
onClick={errorAction}
text='Go to the main dashboard'
/>
</div>
@@ -31,5 +29,5 @@ export const ErrorComponent = ({ text, navigateUrl }) => {

ErrorComponent.propTypes = {
text: PropTypes.string,
navigateUrl: PropTypes.string
errorAction: PropTypes.func,
}
39 changes: 39 additions & 0 deletions frontend/src/tests/components/Error/Error.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import React from 'react';
import { ErrorComponent } from '../../../scenes/errors/Error';

describe('ErrorComponent', () => {
const mockErrorAction = vi.fn();

it('renders the error message and text', () => {
const errorMessage = 'Page not found';

render(<ErrorComponent text={errorMessage} errorAction={mockErrorAction} />);

expect(screen.getByText('We cannot find this page')).toBeInTheDocument();
expect(screen.getByText(errorMessage)).toBeInTheDocument();
});

it('calls errorAction when button is clicked', () => {
render(<ErrorComponent text="Error occurred" errorAction={mockErrorAction} />);

const button = screen.getByRole('button');

fireEvent.click(button);

expect(mockErrorAction).toHaveBeenCalled();
});

it('applies custom styles to the button', () => {
render(<ErrorComponent text="Error occurred" errorAction={mockErrorAction} />);

const button = screen.getByRole('button');

expect(button).toHaveStyle('border-radius: 8px');
expect(button).toHaveStyle('margin-top: 58px');
expect(button).toHaveStyle('font-size: 13px');
expect(button).toHaveStyle('line-height: 24px');
expect(button).toHaveStyle('padding: 5px 27px');
});
});
1 change: 1 addition & 0 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
setupFiles: ["./setupTests.js"],
include: ['src/tests/**/*.test.jsx']
},
});