Description
@testing-library/react
version: 13.1.1- Testing Framework and version: jest 27.5.1
- DOM Environment: jest-dom 27.5.1
Relevant code or config:
const handleSavedCardPayment = () => {
const paymentInfo = {
amount: formDataRef.current.amount,
userId,
accountId: formDataRef.current.selectedCardAccountId,
};
dispatch(makePayment(paymentInfo))
.then(handlePaymentSuccess)
.catch(noop) // already caught by thunk
.finally(() => {
formDataRef.current = null;
setSubmitting(false);
});
};
What you did:
NOTE: This error only shows up after updating to react 18 and testing library to 13.1.1, this was not an issue in earlier version.
setSubmitting toggles the button disability. When the promise is complete, it turns the re-enables the button. so the test is doing a waitFor button to be re-enabled.
the promise looks more like this
dispatch(makePayment(paymentInfo)) returns a promise, with a catch inside.
so the whole promise chain looks like this
promisedFunction().then(() => do something).catch(error => show error). then(() => componentLevel).catch(do nothing here).finally(reset state)
What happened:
the "act" error is thrown, even though i've added a waitFor and that is passing to prove that that statement has already been rendered.
await waitFor(() => expect(screen.getByRole('button', { name: /submit/i })).not.toBeDisabled());
Problem description:
set state in finally seems to cause testing library to think act is incomplete, even though it is.
currently solving the error by putting the set state inside of catch
However, there's the WET code, i had to put the set state into handlePaymentSuccess too, to achieve the same results.