import { renderHook, act } from "testUtilities";
import useDebounce from ".";
jest.useFakeTimers();
describe("useDebounce", function () {
  afterEach(function () {
    jest.clearAllTimers();
  });
  it("calls callback after delay", function () {
    var callback = jest.fn();
    renderHook(function () {
      return useDebounce(callback, 1000);
    });
    expect(callback).not.toHaveBeenCalled();
    act(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
  it("does not call callback before delay", function () {
    var callback = jest.fn();
    renderHook(function () {
      return useDebounce(callback, 1000);
    });
    act(function () {
      jest.advanceTimersByTime(500);
    });
    expect(callback).not.toHaveBeenCalled();
  });
  it("cancels previous timeout when deps change", function () {
    var callback = jest.fn();
    var deps = ["initial"];
    var _renderHook = renderHook(function (_ref) {
        var deps = _ref.deps;
        return useDebounce(callback, 1000, deps);
      }, {
        initialProps: {
          deps: deps
        }
      }),
      rerender = _renderHook.rerender;
    act(function () {
      jest.advanceTimersByTime(500);
    });
    deps = ["changed"];
    rerender({
      deps: deps
    });
    act(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
  it("cancels previous timeout when delay changes", function () {
    var callback = jest.fn();
    var delay = 1000;
    var _renderHook2 = renderHook(function (_ref2) {
        var delay = _ref2.delay;
        return useDebounce(callback, delay);
      }, {
        initialProps: {
          delay: delay
        }
      }),
      rerender = _renderHook2.rerender;
    act(function () {
      jest.advanceTimersByTime(500);
    });
    delay = 2000;
    rerender({
      delay: delay
    });
    act(function () {
      jest.advanceTimersByTime(1500);
    });
    expect(callback).not.toHaveBeenCalled();
    act(function () {
      jest.advanceTimersByTime(500);
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
  it("uses latest callback when deps change", function () {
    var firstCallback = jest.fn();
    var secondCallback = jest.fn();
    var callback = firstCallback;
    var deps = ["initial"];
    var _renderHook3 = renderHook(function (_ref3) {
        var callback = _ref3.callback,
          deps = _ref3.deps;
        return useDebounce(callback, 1000, deps);
      }, {
        initialProps: {
          callback: callback,
          deps: deps
        }
      }),
      rerender = _renderHook3.rerender;
    callback = secondCallback;
    deps = ["changed"];
    rerender({
      callback: callback,
      deps: deps
    });
    act(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(firstCallback).not.toHaveBeenCalled();
    expect(secondCallback).toHaveBeenCalledTimes(1);
  });
  it("handles zero delay", function () {
    var callback = jest.fn();
    renderHook(function () {
      return useDebounce(callback, 0);
    });
    act(function () {
      jest.runAllTimers();
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
  it("handles undefined delay", function () {
    var callback = jest.fn();
    renderHook(function () {
      return useDebounce(callback, undefined);
    });
    act(function () {
      jest.runAllTimers();
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
  it("cleans up timeout on unmount", function () {
    var callback = jest.fn();
    var _renderHook4 = renderHook(function () {
        return useDebounce(callback, 1000);
      }),
      unmount = _renderHook4.unmount;
    unmount();
    act(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(callback).not.toHaveBeenCalled();
  });
  it("handles empty deps array", function () {
    var callback = jest.fn();
    renderHook(function () {
      return useDebounce(callback, 1000, []);
    });
    act(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
});