import React, { useState } from 'react' import { render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import ErrorBoundary from './error-boundary' const consoleErrorSpy = jest .spyOn(global.console, 'error') .mockImplementation(() => {}) const HappyPathUI = () => { const [count, setCount] = useState(0) if (count > 0) { throw new Error('Anything') } return ( ) } const ErrorUI = ({ error }: { error?: unknown }) => { return
message: {(error as Error).message}
} it('shows only on error', async () => { render( ) expect(screen.getByTestId('happy-path-ui')).toBeVisible() expect(screen.queryByTestId('error-ui')).toBeNull() await userEvent.click(screen.getByText('Throw error')) expect(screen.queryByTestId('happy-path-ui')).toBeNull() expect(screen.getByTestId('error-ui')).toBeVisible() expect(screen.getByText('message: Anything')).toBeVisible() expect(consoleErrorSpy.mock.calls).toEqual([ [ expect.objectContaining({ detail: expect.objectContaining({ message: 'Anything' }), type: 'unhandled exception' }) ], [ expect.objectContaining({ detail: expect.objectContaining({ message: 'Anything' }), type: 'unhandled exception' }) ], [ expect.stringMatching( 'The above error occurred in the component:' ) ] ]) })