"use strict";
const react_esm = require('./../_node_modules/@testing-library/react/dist/@testing-library/react.esm.cjs');
const useDebounce = require("./useDebounce.cjs");
const dom_esm = require('./../_node_modules/@testing-library/react/_node_modules/@testing-library/dom/dist/@testing-library/dom.esm.cjs');
describe("useDebounce", () => {
  beforeEach(() => {
    jest.useRealTimers();
  });
  it("debounces a value being passed in after a given time", async () => {
    jest.useFakeTimers();
    const ms = 600;
    const value = "test";
    const middleValue = "fish";
    const newValue = "value";
    const onChange = jest.fn(() => null);
    const { result } = react_esm.renderHook(() => useDebounce.useDebounce(ms, value, onChange));
    expect(result.current[0]).toBe(value);
    expect(result.current[2]).toBe(value);
    react_esm.act(() => {
      result.current[1](middleValue);
      jest.advanceTimersByTime(ms - 1);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[0]).toBe(middleValue);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[2]).toBe(value);
    });
    react_esm.act(() => {
      result.current[1](newValue);
      jest.advanceTimersByTime(ms - 1);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[0]).toBe(newValue);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[2]).toBe(value);
    });
    react_esm.act(() => {
      jest.advanceTimersByTime(1);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[0]).toBe(newValue);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[2]).toBe(newValue);
    });
    expect(onChange).toHaveBeenCalledWith(newValue);
  });
  it("resets a value back to its original value if a reset is called", async () => {
    const ms = 600;
    const value = "test";
    const newValue = "value";
    const onChange = jest.fn(() => null);
    const { result } = react_esm.renderHook(() => useDebounce.useDebounce(ms, value, onChange));
    expect(result.current[0]).toBe(value);
    expect(result.current[2]).toBe(value);
    react_esm.act(() => {
      result.current[1](newValue);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[0]).toBe(newValue);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[2]).toBe(newValue);
    });
    react_esm.act(() => {
      result.current[3]();
    });
    await dom_esm.waitFor(() => {
      expect(result.current[0]).toBe(value);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[2]).toBe(value);
    });
  });
  it("clears the timer on unmount", async () => {
    jest.useFakeTimers();
    const clearSpy = jest.spyOn(global, "clearTimeout");
    const ms = 600;
    const value = "test";
    const newValue = "value";
    const onChange = jest.fn(() => null);
    const { result, unmount } = react_esm.renderHook(() => useDebounce.useDebounce(ms, value, onChange));
    expect(result.current[0]).toBe(value);
    expect(result.current[2]).toBe(value);
    react_esm.act(() => {
      result.current[1](newValue);
      jest.advanceTimersByTime(ms - 1);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[0]).toBe(newValue);
    });
    await dom_esm.waitFor(() => {
      expect(result.current[2]).toBe(value);
    });
    unmount();
    expect(clearSpy).toHaveBeenCalled();
  });
});
describe("useDebounceEffect", () => {
  it("debounces an effect being triggered by a dependency change to a given time", async () => {
    jest.useFakeTimers();
    const ms = 600;
    let value = "test";
    const newValue = "value";
    const onChange = jest.fn(() => null);
    const { rerender } = react_esm.renderHook(() => useDebounce.useDebounceEffect(onChange, ms, [value]));
    react_esm.act(() => {
      value = newValue;
      jest.advanceTimersByTime(ms - 1);
    });
    rerender();
    expect(onChange).not.toHaveBeenCalled();
    react_esm.act(() => {
      jest.advanceTimersByTime(1);
    });
    rerender();
    await dom_esm.waitFor(() => {
      expect(onChange).toHaveBeenCalled();
    });
  });
});
//# sourceMappingURL=useDebounce.spec.cjs.map
