import { renderHook } from './../_node_modules/@testing-library/react/dist/@testing-library/react.esm.js';
import * as globals from "../utils/globals.js";
import { useResizeObserver } from "./useResizeObserver.js";
import { waitFor as waitForWrapper } from './../_node_modules/@testing-library/react/_node_modules/@testing-library/dom/dist/@testing-library/dom.esm.js';
const mockedGlobals = globals;
mockedGlobals.isBrowser = true;
mockedGlobals.supportsResizeObserver = true;
describe("useResizeObserver", () => {
  const observeMock = jest.fn();
  const unobserveMock = jest.fn();
  const disconnectMock = jest.fn();
  const resizeObserverMock = jest.fn(() => ({
    observe: observeMock,
    unobserve: unobserveMock,
    disconnect: disconnectMock
  }));
  global.ResizeObserver = resizeObserverMock;
  it("should observe the specified element", async () => {
    var _a;
    const callback = jest.fn();
    const options = { box: "content-box" };
    const refMock = { current: document.createElement("div") };
    const { result } = renderHook(() => useResizeObserver(callback, options, refMock));
    const { observer } = result.current;
    await waitForWrapper(() => expect(observer.current).toBeDefined());
    expect((_a = observer.current) == null ? void 0 : _a.observe).toHaveBeenCalledWith(refMock.current, options);
  });
  it("should unobserve the specified element", async () => {
    var _a;
    const callback = jest.fn();
    const refMock = { current: document.createElement("div") };
    const { unmount, result } = renderHook(() => useResizeObserver(callback, void 0, refMock));
    const { observer } = result.current;
    await waitForWrapper(() => expect(observer.current).toBeDefined());
    unmount();
    expect((_a = observer.current) == null ? void 0 : _a.unobserve).toHaveBeenCalledWith(refMock.current);
  });
  it("should disconnect the ResizeObserver", async () => {
    var _a;
    const callback = jest.fn();
    const refMock = { current: document.createElement("div") };
    const { unmount, result } = renderHook(() => useResizeObserver(callback, void 0, refMock));
    const { observer } = result.current;
    await waitForWrapper(() => expect(observer.current).toBeDefined());
    unmount();
    expect((_a = observer.current) == null ? void 0 : _a.disconnect).toHaveBeenCalled();
  });
  it("should handle callback passing correctly", async () => {
    const callback = jest.fn();
    const refMock = { current: document.createElement("div") };
    const { result } = renderHook(() => useResizeObserver(callback, void 0, refMock));
    const { observer } = result.current;
    await waitForWrapper(() => expect(observer.current).toBeDefined());
    expect(resizeObserverMock).toHaveBeenCalledWith(callback);
  });
  it("should not create an observer if ref is not defined", async () => {
    const callback = jest.fn();
    const { result } = renderHook(() => useResizeObserver(callback));
    await waitForWrapper(() => {
      var _a;
      return expect((_a = result.current) == null ? void 0 : _a.observer).toEqual({ current: void 0 });
    });
  });
  it("should not create an observer if not in browser", async () => {
    mockedGlobals.isBrowser = false;
    const callback = jest.fn();
    const refMock = { current: document.createElement("div") };
    const { result } = renderHook(() => useResizeObserver(callback, void 0, refMock));
    await waitForWrapper(() => {
      var _a;
      return expect((_a = result.current) == null ? void 0 : _a.observer).toEqual({ current: void 0 });
    });
    mockedGlobals.isBrowser = true;
  });
  it("should not create an observer if supportsResizeObserver not supported", async () => {
    mockedGlobals.supportsResizeObserver = false;
    const callback = jest.fn();
    const refMock = { current: document.createElement("div") };
    const { result } = renderHook(() => useResizeObserver(callback, void 0, refMock));
    await waitForWrapper(() => {
      var _a;
      return expect((_a = result.current) == null ? void 0 : _a.observer).toEqual({ current: void 0 });
    });
    mockedGlobals.supportsResizeObserver = true;
  });
});
//# sourceMappingURL=useResizeObserver.spec.js.map
