I'm looking for the simplest solution to this problem using the popular React Testing Library. I've looked at a lot of links out there and nothing seems to fit..
Here is the component in it's simplest form below:
import React, { useState, useEffect } from "react";
function App() {
const [loggedIn, setLoggedIn] = useState(false)
useEffect(() => {
setTimeout(() => setLoggedIn(true), 1000)
}, [])
return (
<button data-testid="login-button">{loggedIn ? "Log Out" : "Log In"}</button>
);
}
export default App;
And below is the test for the 'no wait' state of the button but would like to know how to implement 'waitFor' within the same test for the change in text in the button after 1 second..
import { render, screen, waitFor } from "@testing-library/react";
import App from "./App";
test('Check the text "Log out" is eventually there', () => {
render(<App />);
const LoginButton = screen.getByText(/Log/)
expect(LoginButton).toHaveTextContent(/Log In/i);
/*How to use waitFor here*/
});
I know we can mock the return from an API call easily and for this problem we probably have to return a promise before we can test using 'waitFor'. How can we use 'WaitFor' in the example test file above?