mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-10 02:07:26 +03:00
Migrated ErrorHandler test to react testing library
This commit is contained in:
parent
e128b847be
commit
bc3bc8dd8a
3 changed files with 31 additions and 27 deletions
|
@ -6,10 +6,10 @@ interface ErrorHandlerState {
|
||||||
hasError: boolean;
|
hasError: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ErrorHandlerCreator = (
|
export const ErrorHandler = (
|
||||||
{ location }: Window,
|
{ location }: Window,
|
||||||
{ error }: Console,
|
{ error }: Console,
|
||||||
) => class ErrorHandler extends Component<any, ErrorHandlerState> {
|
) => class extends Component<any, ErrorHandlerState> {
|
||||||
public constructor(props: object) {
|
public constructor(props: object) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = { hasError: false };
|
this.state = { hasError: false };
|
||||||
|
@ -44,5 +44,3 @@ const ErrorHandlerCreator = (
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ErrorHandlerCreator;
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import MainHeader from '../MainHeader';
|
||||||
import Home from '../Home';
|
import Home from '../Home';
|
||||||
import MenuLayout from '../MenuLayout';
|
import MenuLayout from '../MenuLayout';
|
||||||
import AsideMenu from '../AsideMenu';
|
import AsideMenu from '../AsideMenu';
|
||||||
import ErrorHandler from '../ErrorHandler';
|
import { ErrorHandler } from '../ErrorHandler';
|
||||||
import ShlinkVersionsContainer from '../ShlinkVersionsContainer';
|
import ShlinkVersionsContainer from '../ShlinkVersionsContainer';
|
||||||
import { ConnectDecorator } from '../../container/types';
|
import { ConnectDecorator } from '../../container/types';
|
||||||
import { withoutSelectedServer } from '../../servers/helpers/withoutSelectedServer';
|
import { withoutSelectedServer } from '../../servers/helpers/withoutSelectedServer';
|
||||||
|
|
|
@ -1,38 +1,44 @@
|
||||||
import { shallow, ShallowWrapper } from 'enzyme';
|
import { fireEvent, render, screen } from '@testing-library/react';
|
||||||
import { Button } from 'reactstrap';
|
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import createErrorHandler from '../../src/common/ErrorHandler';
|
import { ErrorHandler as createErrorHandler } from '../../src/common/ErrorHandler';
|
||||||
import { SimpleCard } from '../../src/utils/SimpleCard';
|
|
||||||
|
const ComponentWithError = () => {
|
||||||
|
throw new Error('Error!!');
|
||||||
|
};
|
||||||
|
|
||||||
describe('<ErrorHandler />', () => {
|
describe('<ErrorHandler />', () => {
|
||||||
|
const reload = jest.fn();
|
||||||
const window = Mock.of<Window>({
|
const window = Mock.of<Window>({
|
||||||
location: {
|
location: { reload },
|
||||||
reload: jest.fn(),
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const console = Mock.of<Console>({ error: jest.fn() });
|
const cons = Mock.of<Console>({ error: jest.fn() });
|
||||||
let wrapper: ShallowWrapper;
|
const ErrorHandler = createErrorHandler(window, cons);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const ErrorHandler = createErrorHandler(window, console);
|
jest.spyOn(console, 'error').mockImplementation(() => {}); // Silence react errors
|
||||||
|
|
||||||
wrapper = shallow(<ErrorHandler children={<span>Foo</span>} />);
|
|
||||||
});
|
});
|
||||||
|
afterEach(jest.resetAllMocks);
|
||||||
afterEach(() => wrapper.unmount());
|
|
||||||
|
|
||||||
it('renders children when no error has occurred', () => {
|
it('renders children when no error has occurred', () => {
|
||||||
expect(wrapper.text()).toEqual('Foo');
|
render(<ErrorHandler children={<span>Foo</span>} />);
|
||||||
expect(wrapper.find(Button)).toHaveLength(0);
|
|
||||||
|
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', () => {
|
it('renders error page when error has occurred', () => {
|
||||||
wrapper.setState({ hasError: true });
|
render(<ErrorHandler children={<ComponentWithError />} />);
|
||||||
|
|
||||||
expect(wrapper.find(SimpleCard).contains('Oops! This is awkward :S')).toEqual(true);
|
expect(screen.getByText('Oops! This is awkward :S')).toBeInTheDocument();
|
||||||
expect(wrapper.find(SimpleCard).contains(
|
expect(screen.getByRole('button')).toBeInTheDocument();
|
||||||
'It seems that something went wrong. Try refreshing the page or just click this button.',
|
});
|
||||||
)).toEqual(true);
|
|
||||||
expect(wrapper.find(Button)).toHaveLength(1);
|
it('reloads page on button click', () => {
|
||||||
|
render(<ErrorHandler children={<ComponentWithError />} />);
|
||||||
|
|
||||||
|
expect(reload).not.toHaveBeenCalled();
|
||||||
|
fireEvent.click(screen.getByRole('button'));
|
||||||
|
expect(reload).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue