xnimorz / use-debounce

A debounce hook for react

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do you mock useDebounce?

DoryZi opened this issue · comments

All Mocking attempts create errors:
import useDebounce from 'use-debounce'
jest.mock('use-debounce')

TypeError: (0 , _useDebounce.useDebounce) is not a function or its return value is not iterable
trying to target just that mock also fails:

jest.mock('use-debounce',() => {
  return {
    useDebounce: jest.fn(value => [value])
  }
})

also gives the same error.
using mock timer didn't work either.

commented

Hi @DoryZi
Thanks for the question!

Could you provide a small repo with a problem to help me find out what is your case?

In general, it could be mocked by simple jest.mock. I've prepared a small repo with a simple mocking: https://codesandbox.io/s/eloquent-bassi-hpmct?file=/src/app.spec.js
However, codesandbox doesn't support jest.mock as well as the rest jest functions like useFakeTimers, so you can download the repo and then run yarn; yarn test to check how it works:
image

image

(the issue with codesandbox: codesandbox/codesandbox-client#513)

TRy looking at it now.

commented

jest.mock works good for this example too.

jest.mock("use-debounce", () => {
  return {
    useDebounce: () => {}, // your mock
    useDebouncedCallback: () => {} // your mock,
  };
});
commented

I'm closing this issue for now. If you still have problems, please send me a small repro with the problem, I'll check it.