I would love some help on this. I've been working on it for too long to deem not asking a question.
Thank you in advance for your time and attention,
I have scoured the internet and tried many things but I am still having no luck - here's the issue.
All in all - I am following along with https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning
Using react-testing-library in tandem with jest - and running into two issues
Warning: An update to Login inside a test was not wrapped in act(...).- Jest isn’t picking up that the mock is being called
I really think I have everything wired up correctly - unsure what I am missing. Inside the mock I can put a console.log and can confirm that the mock is indeed being called.
/*
Test
*/
it('should call props.fetchSignIn with the username and password', async () => {
const promise = Promise.resolve({
email: 'test@circulate.social',
firstName: 'Mike',
lastName: 'A',
});
const fetchSignIn = jest.fn(() => promise);
const { queryByTestId, queryByPlaceholderText } = renderLogin({
fetchSignIn,
});
const emailInput = queryByPlaceholderText('joedoe@gmail.com');
const passwordInput = queryByPlaceholderText('password');
const submitButton = queryByTestId('submitButton');
fireEvent.change(emailInput, {
target: { value: 'mike@circulate.social' },
});
fireEvent.change(passwordInput, { target: { value: 'Password1!' } });
fireEvent.click(submitButton);
// I expected this to fail from the parameters being wrong
expect(fetchSignIn).toHaveBeenCalledWith('asf');
await act(() => promise);
});
/*
Functional Component using the `useState(...)` hooks
onFormFinished is the form `onSubmit` handler
*/
const handleSignIn = async (
email: string,
password: string
): Promise<UserContextType['user']> => {
setIsLoginInFlight(true);
try {
const result = await props.fetchSignIn(email, password);
setIsLoginInFlight(false);
return result;
} catch (error) {
// Other logic removed
};
const onFormFinish = async (values: FormValues): Promise<void> => {
const { email, password } = values;
const signInResult = await handleSignIn(email, password);
// Other logic removed
};
Two things are happening
1 - That warning
2 - Test is failing because fetchSignIn was never called
What is expected
The test to fail due to fetchSignIn being called, but the parameters being wrong
Any input or ask for clarification is welcome.
Thank you