I have the following component with a react hook containing a nested async call written in typescript.
const sampleComponent: FunctionComponent = (): ReactElement => {
const [ detail, setDetail ] = useState<detailObject>(undefined);
useEffect(() => {
getConfigs();
}, [ detail != undefined ]);
const getConfigs = () => {
getConfig().then((configResp: any) => {
if(response.status === 200) {
getConfigDetail(configResp.data.id).then((response) => {
if(response.status === 200) {
setDetail(response.data)
}
})
}
})
}
return (
//Some UI related to the responses.
)
}
I test this with the following test.
const configList = jest.spyOn(api, "getConfigList");
const configDetail = jest.spyOn(api, "getConfig");
configList.mockImplementation(() => {
return Promise.resolve(configListRequestResponse); // these are sample responses
});
configDetail.mockImplementation(() => {
return Promise.resolve(configDetailsRequestResponse); // these are sample responses
})
test("Test proper rendering config component", async () => {
await act(async () => {
render(
<Provider store={ store }>
<sampleComponent/>
</Provider>
);
await waitFor(() => expect(configList).toHaveBeenCalledTimes(1));
await waitFor(() => expect(configDetail).toHaveBeenCalledTimes(1));
expect(screen.getByTestId("remote-configs")).toBeInTheDocument();
});
});
When I run this test, I get the following error.
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
> 95 | setDetail(response.data);
| ^
What am I doing wrong here?