L
L
lexstile2022-04-12 22:21:05
Unit testing
lexstile, 2022-04-12 22:21:05

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;
};

What are the mandatory tests for him to write?
Perhaps there are examples on jest? (would be very grateful)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2022-04-13
@lexstile

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 question

Ask a Question

731 491 924 answers to any question