debounce.spec.js 809 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import debounce from "../debounce"
  2. describe("debounce", () => {
  3. test("doesn't call function right after calling", () => {
  4. const fn = jest.fn()
  5. const debFunc = debounce(fn, 100)
  6. debFunc()
  7. expect(fn).not.toHaveBeenCalled()
  8. })
  9. test("calls the function after the given timeout", () => {
  10. const fn = jest.fn()
  11. jest.useFakeTimers()
  12. const debFunc = debounce(fn, 100)
  13. debFunc()
  14. jest.runAllTimers()
  15. expect(fn).toHaveBeenCalled()
  16. // expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 100)
  17. })
  18. test("calls the function only one time within the timeframe", () => {
  19. const fn = jest.fn()
  20. const debFunc = debounce(fn, 1000)
  21. for (let i = 0; i < 100; i++) debFunc()
  22. jest.runAllTimers()
  23. expect(fn).toHaveBeenCalledTimes(1)
  24. })
  25. })