Testing ErrorFallback Component

Rex Ye
2 min readJan 6, 2022

--

Testing (Series)
Understanding Unit Test From The Best Book On The Topic
Testing Ag-Grid React’s Custom Cell Renderer Component
Testing ErrorFallback Component
Testing useDebouncedValue Hook
Testing a HTTP Client Adaptor Hook useClient
Testing a HTTP Error Handler Utility Hook
Testing a Generic Fetch Item List hook with Mock Service
Introducing @mockapi/msw, mock an API server for your entities without writing any code with Mock Service Worker
Testing a Generic Save Item Hook with MSW and @mockapi/msw
Testing a Text Field component integrating Mui’s Text Field with React Hook Form

Subject Under Test(sut)

An error fallback component to be passed to ErrorBoundary component from react-error-boundary.

Behaviours

  1. In development mode, it shows error track trace
  2. In production mode, it hides error track trace
  3. It shows a Reset button, and clicking it calls resetErrorBoundary action passed by parent ErrorBoundary component.

Notes

  1. The renderInErrorBoundary helper method renders a bomb component inside an actual ErrorBoundary, no mocks. It shows how the sut should be used.
  2. getByText itself implicitly asserts if the element is in the document because if not found, it will throw an error. However, I asserted anyway for clarity
  3. queryByTestId is used to assert if the error stack trace is rendered in the DOM under production, it returns null if not found, contrary to getByTestId, which would throw an error when the element is not found.
  4. @epic/enviornments is mocked out to override the value to test production mode behaviour.
  5. The renderStandalone helper method renders the component as any other React component, for the sole purpose of testing the behaviour of the Reset button. Because the resetErrorBoundary is implicitly passed to the sut, and I don’t know a way to mock a React component prop yet. The test shouldn’t care about it anyway.

--

--

Rex Ye
Rex Ye

Written by Rex Ye

.net, Angular, React. Creator of epicerp.app

No responses yet