2022-05-11 20:18:43 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2020-08-29 11:53:02 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-05-03 21:01:40 +03:00
|
|
|
import { ErrorHandler as createErrorHandler } from '../../src/common/ErrorHandler';
|
|
|
|
|
|
|
|
const ComponentWithError = () => {
|
|
|
|
throw new Error('Error!!');
|
|
|
|
};
|
2019-03-04 22:49:18 +03:00
|
|
|
|
|
|
|
describe('<ErrorHandler />', () => {
|
2022-05-03 21:01:40 +03:00
|
|
|
const reload = jest.fn();
|
2020-08-29 11:53:02 +03:00
|
|
|
const window = Mock.of<Window>({
|
2022-05-03 21:01:40 +03:00
|
|
|
location: { reload },
|
2020-08-29 11:53:02 +03:00
|
|
|
});
|
2022-05-03 21:01:40 +03:00
|
|
|
const cons = Mock.of<Console>({ error: jest.fn() });
|
|
|
|
const ErrorHandler = createErrorHandler(window, cons);
|
2019-03-04 22:49:18 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-05-03 21:01:40 +03:00
|
|
|
jest.spyOn(console, 'error').mockImplementation(() => {}); // Silence react errors
|
2019-03-04 22:49:18 +03:00
|
|
|
});
|
2022-05-03 21:01:40 +03:00
|
|
|
afterEach(jest.resetAllMocks);
|
2019-03-04 22:49:18 +03:00
|
|
|
|
|
|
|
it('renders children when no error has occurred', () => {
|
2022-05-03 21:01:40 +03:00
|
|
|
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', () => {
|
2022-05-03 21:01:40 +03:00
|
|
|
render(<ErrorHandler children={<ComponentWithError />} />);
|
|
|
|
|
|
|
|
expect(screen.getByText('Oops! This is awkward :S')).toBeInTheDocument();
|
|
|
|
expect(screen.getByRole('button')).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
it('reloads page on button click', async () => {
|
|
|
|
const user = userEvent.setup();
|
2022-05-03 21:01:40 +03:00
|
|
|
render(<ErrorHandler children={<ComponentWithError />} />);
|
2019-03-04 22:49:18 +03:00
|
|
|
|
2022-05-03 21:01:40 +03:00
|
|
|
expect(reload).not.toHaveBeenCalled();
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByRole('button'));
|
2022-05-03 21:01:40 +03:00
|
|
|
expect(reload).toHaveBeenCalled();
|
2019-03-04 22:49:18 +03:00
|
|
|
});
|
|
|
|
});
|