shlink-web-client/test/common/ErrorHandler.test.tsx

45 lines
1.5 KiB
TypeScript
Raw Normal View History

import { fireEvent, render, screen } from '@testing-library/react';
import { Mock } from 'ts-mockery';
import { ErrorHandler as createErrorHandler } from '../../src/common/ErrorHandler';
const ComponentWithError = () => {
throw new Error('Error!!');
};
2019-03-04 22:49:18 +03:00
describe('<ErrorHandler />', () => {
const reload = jest.fn();
const window = Mock.of<Window>({
location: { reload },
});
const cons = Mock.of<Console>({ error: jest.fn() });
const ErrorHandler = createErrorHandler(window, cons);
2019-03-04 22:49:18 +03:00
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {}); // Silence react errors
2019-03-04 22:49:18 +03:00
});
afterEach(jest.resetAllMocks);
2019-03-04 22:49:18 +03:00
it('renders children when no error has occurred', () => {
render(<ErrorHandler children={<span>Foo</span>} />);
expect(screen.getByText('Foo')).toBeInTheDocument();
expect(screen.queryByText('Oops! This is awkward :S')).not.toBeInTheDocument();
expect(screen.queryByRole('button')).not.toBeInTheDocument();
2019-03-04 22:49:18 +03:00
});
it('renders error page when error has occurred', () => {
render(<ErrorHandler children={<ComponentWithError />} />);
expect(screen.getByText('Oops! This is awkward :S')).toBeInTheDocument();
expect(screen.getByRole('button')).toBeInTheDocument();
});
it('reloads page on button click', () => {
render(<ErrorHandler children={<ComponentWithError />} />);
2019-03-04 22:49:18 +03:00
expect(reload).not.toHaveBeenCalled();
fireEvent.click(screen.getByRole('button'));
expect(reload).toHaveBeenCalled();
2019-03-04 22:49:18 +03:00
});
});