This is my code. I want to test the text in the component once the state is updated. But In the console It's showing loading component.
I'm using Jest and Enzyme.
This is App.js code
import logo from "./logo.svg";
import "./App.css";
import React, { useEffect } from "react";
function App() {
const [isLoading, setIsLoading] = React.useState(true);
const [isDataLoaded, setIsDataLoaded] = React.useState(false);
useEffect(() => {
if (isDataLoaded) {
setIsLoading(false);
}
}, [isDataLoaded]);
useEffect(() => {
setTimeout(() => setIsDataLoaded(true), 4000);
}, []);
return (
<div className="App">
{isLoading && <div>Loading....</div>}
{!isLoading && (
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
)}
</div>
);
}
export default App;
This is App.test.js code
import React from "react";
import { mount } from "enzyme";
import App from "./App";
let wrapper;
const setup = (state = {}) => {
const mockUseState = jest.fn().mockReturnValue([{ ...state }, jest.fn()]);
React.useState = mockUseState;
return mount(<App />);
};
// beforeEach(() => {
// wrapper = setup();
// });
// afterEach(() => {
// wrapper.unmount();
// });
describe("App", () => {
test("renders without error", () => {
wrapper = setup();
expect(wrapper.find("App").exists()).toBe(true);
});
test("renders learn react link", () => {
wrapper = setup({ isLoading: false });
wrapper.setProps();
console.log(wrapper.debug(), "console output")
expect(wrapper.find("App-link").text()).toBe("Learn React");
});
});