I have a parent component with a button that when clicked launches a modal window child component. A request is made from the child component when it first renders, and then response data is saved to local state, e.g.
useEffect(() => {
const response = await getData(id);
setData(response.data)
...
}, []);
In the parent component I am testing if the modal window launches - to do this I can just test that the dom element is present after the button is clicked, great. However when the modal window launches the request is made from the child component resulting in the error:
Error: connect ECONNREFUSED 127.0.0.1:80
There seem to be two options:
1/ Use msw
to provide a mock endpoint for the request. However due to the state being updated it results in the following error:
Warning: An update to Component inside a test was not wrapped in act(...)
2/ Mock the entire child component using jest.mock()
. This is the logical thing to do as I am testing the parent component and I do not care about the child. However from what I have read this is not the RTL way of doing things.
This seems like it must really common testing scenario, but I haven't found a solution so any advice on the best way to do this would be appreciated.