menu

Testing Library

Simple and complete DOM testing utilities that encourage good testing practices.

Channels
Team

Mocking a promise to catch for errors in onBlur?

April 29, 2019 at 6:22pm

Mocking a promise to catch for errors in onBlur?

April 29, 2019 at 6:22pm (Edited 3 months ago)

I have a React component with an input. onBlur of the input it fires an action that will resolve and let the user continue or it will fail and send an error that is displayed on the screen. The error is caught by the promise and then set in state of the component to be used in a couple places. I'm trying to test for the failure and that the data-testId for the error component is in the document.

How can I setup the test so that I can pass it an error to see what happens? Do I need to mock the promise with the error I want? Thanks. @kentcdodds


May 16, 2019 at 4:07am

Hi @mjvalade, this is a good question!

Kent has one example that demonstrates data fetching testing titled async.js. The file is located at src/__tests__/async.js.

You can find the demo here:

Let us know if that helps!

  • reply
  • like