Answer the question
In order to leave comments, you need to log in
What tests should be done for the useDebounce hook?
There is a hook:
export const useDebounce = (value: any, delay: number) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
};
Answer the question
In order to leave comments, you need to log in
Offhand - check 4 points:
1) The starting value is available immediately
2) Resetting the value by timer (that is, at the time delay-epsilon it is old, and at delay + epsilon it is already new)
3) Check that if before the end of the timeout for the value v2 came still a new value v3, the timer is restarted, the value remains v1, according to the timer it will be v3. In general, what is debounce.
4) Reset the timer when the component is unmounted.
in addition to jest, use renderHook from @testing-library/react-hooks
and do human typing with generics.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question