From ad9f0c00d039a464506c8922e0f6d2dccc91c7c2 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 4 Mar 2019 20:49:18 +0100 Subject: [PATCH] Created ErrorHandler test --- src/common/ErrorHandler.js | 1 - test/common/ErrorHandler.test.js | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/common/ErrorHandler.test.js diff --git a/src/common/ErrorHandler.js b/src/common/ErrorHandler.js index ec069b2f..fb641a79 100644 --- a/src/common/ErrorHandler.js +++ b/src/common/ErrorHandler.js @@ -14,7 +14,6 @@ const ErrorHandler = ({ location }) => class ErrorHandler extends React.Componen } static getDerivedStateFromError() { - // Update state so the next render will show the fallback UI. return { hasError: true }; } diff --git a/test/common/ErrorHandler.test.js b/test/common/ErrorHandler.test.js new file mode 100644 index 00000000..58325a65 --- /dev/null +++ b/test/common/ErrorHandler.test.js @@ -0,0 +1,36 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import { Button } from 'reactstrap'; +import createErrorHandler from '../../src/common/ErrorHandler'; + +describe('', () => { + const window = { + location: { + reload: jest.fn(), + }, + }; + let wrapper; + + beforeEach(() => { + const ErrorHandler = createErrorHandler(window); + + wrapper = shallow(Foo} />); + }); + + 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); + }); +});