I am making an axios call in the initial render of my component with a useEffect, however I am having trouble actually testing the contents of my component after I have returned a 200 response with content from that call...
Component.jsx
const MyComp = () => {
const [data, setData] = useState(null);
useEffect(() => {
axios.get('/my-url').then(data => setData(data.data));
}, []);
return (
<div>
{ data && <div data-testid="data">{data}</div> }
</div>
);
}
Component.spec.js
test('should render data when loaded', async () => {
mockAxios.onGet("/my-url").reply(200, { data: 'hello' });
rendered = render(<MyComp />);
await waitFor(() => rendered.getByTestId("data"));
expect(rendered.getByTestId("data")).toBeTruthy();
expect(rendered.getByTestId("data").textContent).toBe("hello");
});
after this test runs, I get the error:
Unable to find an element by: [data-testid="data"]