Skip to content

Commit

Permalink
Added tests for ErrorBoundary component
Browse files Browse the repository at this point in the history
  • Loading branch information
lilia1891 committed Sep 22, 2023
1 parent 82ddee1 commit 60c1167
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 85 deletions.
52 changes: 0 additions & 52 deletions tests/common/Accordion.test.js

This file was deleted.

16 changes: 16 additions & 0 deletions tests/common/ErrorBoundary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { render, screen } from '../test-utils';
import ErrorBoundary from '../../components/common/ErrorBoundary';

describe('ErrorBoundary component', () => {
it('renders children when there is no error', () => {
const testText = 'Child component';
render(
<ErrorBoundary>
<div>{testText}</div>
</ErrorBoundary>
);
const childComponent = screen.getByText(testText);
expect(childComponent).toBeInTheDocument();
});
});
61 changes: 28 additions & 33 deletions tests/common/ErrorMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,37 @@ describe('ErrorMessage component', () => {
const errorMessage = getByText('Test Error');
expect(errorMessage).toBeInTheDocument();
});
});

it('throws an error with the status code on the server', () => {
const statusCode = 404;
const message = 'Not Found';
const serverRender = () => {
expect(() => {
render(<ErrorMessage message={message} statusCode={statusCode} />);
}).toThrowError(new Error(message));
};

expect(serverRender).toThrow();
});
it('throws an error with the status code on the server', () => {
const statusCode = 404;
const message = 'Not Found';
const serverRender = () => {
expect(() => {
render(<ErrorMessage message={message} statusCode={statusCode} />);
}).toThrowError(new Error(message));
};

it('doesn not throw any errors on the client side', () => {
const statusCode = 404;
const message = 'Not Found';
const clientRender = () => {
expect(() =>
render(<ErrorMessage message={message} statusCode={statusCode} />)
).not.toThrow();
};
clientRender();
});
expect(serverRender).toThrow();
});

const consoleErrorSpy = jest.spyOn(console, 'error');
beforeEach(() => {
consoleErrorSpy.mockClear();
});
it('doesn not throw any errors on the client side', () => {
const statusCode = 404;
const message = 'Not Found';
const clientRender = () => {
expect(() =>
render(<ErrorMessage message={message} statusCode={statusCode} />)
).not.toThrow();
};
clientRender();
});

it('logs a prop type error for invalid props', () => {
render(<ErrorMessage message={123} />);
expect(consoleErrorSpy).toHaveBeenCalled();
});
const consoleErrorSpy = jest.spyOn(console, 'error');
beforeEach(() => {
consoleErrorSpy.mockClear();
});

it('does not log a prop type error for valid props', () => {
render(<ErrorMessage message="Test Error" />);
expect(consoleErrorSpy).not.toHaveBeenCalled();
it('does not log a prop type error for valid props', () => {
render(<ErrorMessage message="Test Error" />);
expect(consoleErrorSpy).not.toHaveBeenCalled();
});
});

0 comments on commit 60c1167

Please sign in to comment.