"use strict";

var _react = _interopRequireDefault(require("react"));
var _mergeRefs = require("./mergeRefs");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
describe("mergeRefs", function () {
  describe("assignRef", function () {
    it("handles null refs gracefully", function () {
      expect(function () {
        return (0, _mergeRefs.assignRef)(null, "value");
      }).not.toThrow();
      expect(function () {
        return (0, _mergeRefs.assignRef)(undefined, "value");
      }).not.toThrow();
    });
    it("calls function refs with value", function () {
      var functionRef = jest.fn();
      var value = "test value";
      (0, _mergeRefs.assignRef)(functionRef, value);
      expect(functionRef).toHaveBeenCalledWith(value);
    });
    it("assigns value to object refs", function () {
      var objectRef = /*#__PURE__*/_react["default"].createRef();
      var value = "test value";
      (0, _mergeRefs.assignRef)(objectRef, value);
      expect(objectRef.current).toBe(value);
    });
    it("throws error for invalid object refs", function () {
      var invalidRef = {
        current: null
      };
      Object.defineProperty(invalidRef, "current", {
        set: function set() {
          throw new Error("Cannot set current");
        }
      });
      expect(function () {
        return (0, _mergeRefs.assignRef)(invalidRef, "value");
      }).toThrow("Cannot assign value 'value' to ref '[object Object]'");
    });
    it("handles different value types", function () {
      var functionRef = jest.fn();
      var objectRef = /*#__PURE__*/_react["default"].createRef();
      var values = [null, undefined, 0, "", false, {}, []];
      values.forEach(function (value) {
        (0, _mergeRefs.assignRef)(functionRef, value);
        (0, _mergeRefs.assignRef)(objectRef, value);
        expect(functionRef).toHaveBeenCalledWith(value);
        expect(objectRef.current).toBe(value);
        functionRef.mockClear();
      });
    });
  });
  describe("mergeRefs", function () {
    it("returns a function", function () {
      var merged = (0, _mergeRefs.mergeRefs)();
      expect(typeof merged).toBe("function");
    });
    it("calls all provided refs", function () {
      var ref1 = jest.fn();
      var ref2 = jest.fn();
      var ref3 = /*#__PURE__*/_react["default"].createRef();
      var merged = (0, _mergeRefs.mergeRefs)(ref1, ref2, ref3);
      var value = "test element";
      merged(value);
      expect(ref1).toHaveBeenCalledWith(value);
      expect(ref2).toHaveBeenCalledWith(value);
      expect(ref3.current).toBe(value);
    });
    it("handles empty refs list", function () {
      var merged = (0, _mergeRefs.mergeRefs)();
      expect(function () {
        return merged("value");
      }).not.toThrow();
    });
    it("handles null and undefined refs", function () {
      var validRef = jest.fn();
      var merged = (0, _mergeRefs.mergeRefs)(null, validRef, undefined);
      merged("value");
      expect(validRef).toHaveBeenCalledWith("value");
    });
    it("handles mixed ref types", function () {
      var functionRef = jest.fn();
      var objectRef = /*#__PURE__*/_react["default"].createRef();
      var nullRef = null;
      var merged = (0, _mergeRefs.mergeRefs)(functionRef, objectRef, nullRef);
      var element = document.createElement("div");
      merged(element);
      expect(functionRef).toHaveBeenCalledWith(element);
      expect(objectRef.current).toBe(element);
    });
    it("continues executing even if one ref throws", function () {
      var goodRef = jest.fn();
      var badRef = function badRef() {
        throw new Error("Bad ref");
      };
      var anotherGoodRef = jest.fn();
      var merged = (0, _mergeRefs.mergeRefs)(goodRef, badRef, anotherGoodRef);
      expect(function () {
        return merged("value");
      }).toThrow("Bad ref");
      expect(goodRef).toHaveBeenCalledWith("value");
    });
    it("works with React component refs", function () {
      var Component = /*#__PURE__*/_react["default"].forwardRef(function (props, ref) {
        return /*#__PURE__*/_react["default"].createElement("div", {
          ref: ref
        });
      });
      var ref1 = /*#__PURE__*/_react["default"].createRef();
      var ref2 = /*#__PURE__*/_react["default"].createRef();
      var merged = (0, _mergeRefs.mergeRefs)(ref1, ref2);
      merged("element");
      expect(ref1.current).toBe("element");
      expect(ref2.current).toBe("element");
    });
    it("handles callback refs that update state", function () {
      var setElement1 = jest.fn();
      var setElement2 = jest.fn();
      var merged = (0, _mergeRefs.mergeRefs)(setElement1, setElement2);
      var element = "test element";
      merged(element);
      expect(setElement1).toHaveBeenCalledWith(element);
      expect(setElement2).toHaveBeenCalledWith(element);
    });
  });
});