Refactored ServerError to infer error message based on provided server type guards

This commit is contained in:
Alejandro Celaya
2020-08-29 10:53:02 +02:00
parent f40ad91ea9
commit 8cc0695ee9
14 changed files with 177 additions and 186 deletions

View File

@@ -0,0 +1,38 @@
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import { Button } from 'reactstrap';
import { Mock } from 'ts-mockery';
import createErrorHandler from '../../src/common/ErrorHandler';
describe('<ErrorHandler />', () => {
const window = Mock.of<Window>({
location: {
reload: jest.fn(),
},
});
const console = Mock.of<Console>({ error: jest.fn() });
let wrapper: ShallowWrapper;
beforeEach(() => {
const ErrorHandler = createErrorHandler(window, console);
wrapper = shallow(<ErrorHandler children={<span>Foo</span>} />);
});
afterEach(() => wrapper.unmount());
it('renders children when no error has occurred', () => {
expect(wrapper.text()).toEqual('Foo');
expect(wrapper.find(Button)).toHaveLength(0);
});
it('renders error page when error has occurred', () => {
wrapper.setState({ hasError: true });
expect(wrapper.text()).toContain('Oops! This is awkward :S');
expect(wrapper.text()).toContain(
'It seems that something went wrong. Try refreshing the page or just click this button.',
);
expect(wrapper.find(Button)).toHaveLength(1);
});
});