I am learning react testing library and jest. For now I'm writing a component to update data from the database. The code looks like this:
const UseUpdateData = ({id, status}) => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const [updatedStatus, setUpdatedStatus] = useState(status);
const update = async () => {
setError(false);
setLoading(true);
try {
const updateBillStatusTo =
updatedStatus === "PAID" ? "PENDING" : "PAID";
const response = await fetch(...);
const updatedBillStatus = response.data;
setLoading(false);
setUpdatedStatus(updatedBillStatus);
} catch {
setLoading(false);
setError(true);
}
};
return { loading, error, updatedStatus, update };
};
return default UseUpdateData;
The update function will be called from another component when a button is clicked. When I write test to this component, I only get 50% coverage on Branch. Is this component usage is wrong? That's why I can't get 100% coverage?