mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-24 01:48:18 +03:00
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { Mock } from 'ts-mockery';
|
|
import { ErrorHandler as createErrorHandler } from '../../src/common/ErrorHandler';
|
|
|
|
const ComponentWithError = () => {
|
|
throw new Error('Error!!');
|
|
};
|
|
|
|
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);
|
|
|
|
beforeEach(() => {
|
|
jest.spyOn(console, 'error').mockImplementation(() => {}); // Silence react errors
|
|
});
|
|
afterEach(jest.resetAllMocks);
|
|
|
|
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();
|
|
});
|
|
|
|
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', async () => {
|
|
const user = userEvent.setup();
|
|
render(<ErrorHandler children={<ComponentWithError />} />);
|
|
|
|
expect(reload).not.toHaveBeenCalled();
|
|
await user.click(screen.getByRole('button'));
|
|
expect(reload).toHaveBeenCalled();
|
|
});
|
|
});
|