From bc3bc8dd8abebdd3d442ee599f1229463ab2b381 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 3 May 2022 20:01:40 +0200 Subject: [PATCH] Migrated ErrorHandler test to react testing library --- src/common/ErrorHandler.tsx | 6 ++-- src/common/services/provideServices.ts | 2 +- test/common/ErrorHandler.test.tsx | 50 ++++++++++++++------------ 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/common/ErrorHandler.tsx b/src/common/ErrorHandler.tsx index c2c67faa..0a23968b 100644 --- a/src/common/ErrorHandler.tsx +++ b/src/common/ErrorHandler.tsx @@ -6,10 +6,10 @@ interface ErrorHandlerState { hasError: boolean; } -const ErrorHandlerCreator = ( +export const ErrorHandler = ( { location }: Window, { error }: Console, -) => class ErrorHandler extends Component { +) => class extends Component { public constructor(props: object) { super(props); this.state = { hasError: false }; @@ -44,5 +44,3 @@ const ErrorHandlerCreator = ( return children; } }; - -export default ErrorHandlerCreator; diff --git a/src/common/services/provideServices.ts b/src/common/services/provideServices.ts index 598661a3..9dfddaea 100644 --- a/src/common/services/provideServices.ts +++ b/src/common/services/provideServices.ts @@ -5,7 +5,7 @@ import MainHeader from '../MainHeader'; import Home from '../Home'; import MenuLayout from '../MenuLayout'; import AsideMenu from '../AsideMenu'; -import ErrorHandler from '../ErrorHandler'; +import { ErrorHandler } from '../ErrorHandler'; import ShlinkVersionsContainer from '../ShlinkVersionsContainer'; import { ConnectDecorator } from '../../container/types'; import { withoutSelectedServer } from '../../servers/helpers/withoutSelectedServer'; diff --git a/test/common/ErrorHandler.test.tsx b/test/common/ErrorHandler.test.tsx index e3c741a0..69e72e47 100644 --- a/test/common/ErrorHandler.test.tsx +++ b/test/common/ErrorHandler.test.tsx @@ -1,38 +1,44 @@ -import { shallow, ShallowWrapper } from 'enzyme'; -import { Button } from 'reactstrap'; +import { fireEvent, render, screen } from '@testing-library/react'; import { Mock } from 'ts-mockery'; -import createErrorHandler from '../../src/common/ErrorHandler'; -import { SimpleCard } from '../../src/utils/SimpleCard'; +import { ErrorHandler as createErrorHandler } from '../../src/common/ErrorHandler'; + +const ComponentWithError = () => { + throw new Error('Error!!'); +}; describe('', () => { + const reload = jest.fn(); const window = Mock.of({ - location: { - reload: jest.fn(), - }, + location: { reload }, }); - const console = Mock.of({ error: jest.fn() }); - let wrapper: ShallowWrapper; + const cons = Mock.of({ error: jest.fn() }); + const ErrorHandler = createErrorHandler(window, cons); beforeEach(() => { - const ErrorHandler = createErrorHandler(window, console); - - wrapper = shallow(Foo} />); + jest.spyOn(console, 'error').mockImplementation(() => {}); // Silence react errors }); - - afterEach(() => wrapper.unmount()); + afterEach(jest.resetAllMocks); it('renders children when no error has occurred', () => { - expect(wrapper.text()).toEqual('Foo'); - expect(wrapper.find(Button)).toHaveLength(0); + render(Foo} />); + + 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', () => { - wrapper.setState({ hasError: true }); + render(} />); - expect(wrapper.find(SimpleCard).contains('Oops! This is awkward :S')).toEqual(true); - expect(wrapper.find(SimpleCard).contains( - 'It seems that something went wrong. Try refreshing the page or just click this button.', - )).toEqual(true); - expect(wrapper.find(Button)).toHaveLength(1); + expect(screen.getByText('Oops! This is awkward :S')).toBeInTheDocument(); + expect(screen.getByRole('button')).toBeInTheDocument(); + }); + + it('reloads page on button click', () => { + render(} />); + + expect(reload).not.toHaveBeenCalled(); + fireEvent.click(screen.getByRole('button')); + expect(reload).toHaveBeenCalled(); }); });