2023-09-30 11:20:28 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-09-30 11:20:28 +03:00
|
|
|
import type { PropsWithChildren, ReactNode } from 'react';
|
2023-09-05 10:08:42 +03:00
|
|
|
import { ErrorHandler as BaseErrorHandler } from '../../src/common/ErrorHandler';
|
2023-09-30 11:20:28 +03:00
|
|
|
import { checkAccessibility } from '../__helpers__/accessibility';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2022-05-03 21:01:40 +03:00
|
|
|
|
|
|
|
const ComponentWithError = () => {
|
|
|
|
throw new Error('Error!!');
|
|
|
|
};
|
2019-03-04 22:49:18 +03:00
|
|
|
|
|
|
|
describe('<ErrorHandler />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const reload = vi.fn();
|
2023-09-05 10:08:42 +03:00
|
|
|
const location = fromPartial<Window['location']>({ reload });
|
2023-05-27 12:57:26 +03:00
|
|
|
const cons = fromPartial<Console>({ error: vi.fn() });
|
2023-09-05 10:08:42 +03:00
|
|
|
const ErrorHandler = (props: PropsWithChildren) => <BaseErrorHandler console={cons} location={location} {...props} />;
|
2023-09-30 11:20:28 +03:00
|
|
|
const setUp = (children: ReactNode = 'Error') => renderWithEvents(<ErrorHandler>{children}</ErrorHandler>);
|
2019-03-04 22:49:18 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-05-27 12:57:26 +03:00
|
|
|
vi.spyOn(console, 'error').mockImplementation(() => {}); // Silence react errors
|
2019-03-04 22:49:18 +03:00
|
|
|
});
|
|
|
|
|
2023-09-30 11:20:28 +03:00
|
|
|
it('passes a11y checks', () => checkAccessibility(setUp()));
|
|
|
|
|
2019-03-04 22:49:18 +03:00
|
|
|
it('renders children when no error has occurred', () => {
|
2023-09-30 11:20:28 +03:00
|
|
|
setUp(<span>Foo</span>);
|
2022-05-03 21:01:40 +03:00
|
|
|
|
|
|
|
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', () => {
|
2023-09-30 11:20:28 +03:00
|
|
|
setUp(<ComponentWithError />);
|
2022-05-03 21:01:40 +03:00
|
|
|
|
|
|
|
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 () => {
|
2023-09-30 11:20:28 +03:00
|
|
|
const { user } = setUp(<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
|
|
|
});
|
|
|
|
});
|