I'm trying to test a custom hook that uses useState and useEffect together with a setTimeout that simulates a delay loading some data. Simplified
const useCustomHook = (id: number) => {
const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState(false);
const [value, setValue] = React.useState<string>();
React.useEffect(() => {
const dummy = ["foo", "bar", "baz"];
// simulate remote call with delay
setTimeout(() => {
id < 3 ? setValue(dummy[id]) : setError(true);
setLoading(false);
}, 1500);
}, [id]);
return [loading, error, value];
};
const App = () => {
const [loading, error, value] = useCustomHook(1);
if (loading) { return <div>Loading...</div>; }
if (error) { return <div>Error</div>; }
return <h1>{value}</h1>;
};
https://codesandbox.io/s/react-typescript-z1z2b
How would you test all possible states (loading, error and value) of this hook with Jest and Enzyme?
Thanks in advance!!!