"use strict";

var _testUtilities = require("testUtilities");
var _ = _interopRequireDefault(require("."));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
jest.useFakeTimers();
describe("useDebounce", function () {
  afterEach(function () {
    jest.clearAllTimers();
  });
  it("calls callback after delay", function () {
    var callback = jest.fn();
    (0, _testUtilities.renderHook)(function () {
      return (0, _["default"])(callback, 1000);
    });
    expect(callback).not.toHaveBeenCalled();
    (0, _testUtilities.act)(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
  it("does not call callback before delay", function () {
    var callback = jest.fn();
    (0, _testUtilities.renderHook)(function () {
      return (0, _["default"])(callback, 1000);
    });
    (0, _testUtilities.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 = (0, _testUtilities.renderHook)(function (_ref) {
        var deps = _ref.deps;
        return (0, _["default"])(callback, 1000, deps);
      }, {
        initialProps: {
          deps: deps
        }
      }),
      rerender = _renderHook.rerender;
    (0, _testUtilities.act)(function () {
      jest.advanceTimersByTime(500);
    });
    deps = ["changed"];
    rerender({
      deps: deps
    });
    (0, _testUtilities.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 = (0, _testUtilities.renderHook)(function (_ref2) {
        var delay = _ref2.delay;
        return (0, _["default"])(callback, delay);
      }, {
        initialProps: {
          delay: delay
        }
      }),
      rerender = _renderHook2.rerender;
    (0, _testUtilities.act)(function () {
      jest.advanceTimersByTime(500);
    });
    delay = 2000;
    rerender({
      delay: delay
    });
    (0, _testUtilities.act)(function () {
      jest.advanceTimersByTime(1500);
    });
    expect(callback).not.toHaveBeenCalled();
    (0, _testUtilities.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 = (0, _testUtilities.renderHook)(function (_ref3) {
        var callback = _ref3.callback,
          deps = _ref3.deps;
        return (0, _["default"])(callback, 1000, deps);
      }, {
        initialProps: {
          callback: callback,
          deps: deps
        }
      }),
      rerender = _renderHook3.rerender;
    callback = secondCallback;
    deps = ["changed"];
    rerender({
      callback: callback,
      deps: deps
    });
    (0, _testUtilities.act)(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(firstCallback).not.toHaveBeenCalled();
    expect(secondCallback).toHaveBeenCalledTimes(1);
  });
  it("handles zero delay", function () {
    var callback = jest.fn();
    (0, _testUtilities.renderHook)(function () {
      return (0, _["default"])(callback, 0);
    });
    (0, _testUtilities.act)(function () {
      jest.runAllTimers();
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
  it("handles undefined delay", function () {
    var callback = jest.fn();
    (0, _testUtilities.renderHook)(function () {
      return (0, _["default"])(callback, undefined);
    });
    (0, _testUtilities.act)(function () {
      jest.runAllTimers();
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
  it("cleans up timeout on unmount", function () {
    var callback = jest.fn();
    var _renderHook4 = (0, _testUtilities.renderHook)(function () {
        return (0, _["default"])(callback, 1000);
      }),
      unmount = _renderHook4.unmount;
    unmount();
    (0, _testUtilities.act)(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(callback).not.toHaveBeenCalled();
  });
  it("handles empty deps array", function () {
    var callback = jest.fn();
    (0, _testUtilities.renderHook)(function () {
      return (0, _["default"])(callback, 1000, []);
    });
    (0, _testUtilities.act)(function () {
      jest.advanceTimersByTime(1000);
    });
    expect(callback).toHaveBeenCalledTimes(1);
  });
});