UNPKG

reshow-build

Version:
87 lines 3.24 kB
import _classCallCheck from "reshow-runtime/es/helpers/classCallCheck"; import _createClass from "reshow-runtime/es/helpers/createClass"; import _possibleConstructorReturn from "reshow-runtime/es/helpers/possibleConstructorReturn"; import _isNativeReflectConstruct from "reshow-runtime/es/helpers/isNativeReflectConstruct"; import _getPrototypeOf from "reshow-runtime/es/helpers/getPrototypeOf"; import _inherits from "reshow-runtime/es/helpers/inherits"; import _defineProperty from "reshow-runtime/es/helpers/defineProperty"; import _objectSpread from "reshow-runtime/es/helpers/objectSpread2"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } import { useRef, PureComponent, forwardRef, useImperativeHandle } from "react"; import { expect } from "chai"; import { render } from "reshow-unit"; import { mergeRef } from "../mergeRef.mjs"; import { jsx as _jsx } from "react/jsx-runtime"; describe("Test merge ref", function () { it("test hook", function () { var CompRoot = /*#__PURE__*/forwardRef(function (props, ref) { var lastEl = useRef(); var expose = { el: function el() { return lastEl.current; } }; useImperativeHandle(ref, function () { return expose; }, []); return /*#__PURE__*/_jsx(Comp, _objectSpread({ refCb: lastEl }, props)); }); var childRef; var Comp = function Comp(props) { var lastEl = useRef(); childRef = lastEl; return /*#__PURE__*/_jsx("div", { ref: function ref(el) { return mergeRef(el, [lastEl, props.refCb]); } }); }; var wrap = render(/*#__PURE__*/_jsx(CompRoot, {}), { instance: true }); expect(wrap.instance().el().nodeName).to.equal("DIV"); expect(childRef.current.nodeName).to.equal("DIV"); }); it("test callback", function () { var CompRoot = /*#__PURE__*/function (_PureComponent) { function CompRoot() { var _this; _classCallCheck(this, CompRoot); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, CompRoot, [].concat(args)); _defineProperty(_this, "handleRefCb", function (el) { return _this.el = el; }); return _this; } _inherits(CompRoot, _PureComponent); return _createClass(CompRoot, [{ key: "render", value: function render() { return /*#__PURE__*/_jsx(Comp, { refCb: this.handleRefCb }); } }]); }(PureComponent); var childRef; var Comp = function Comp(props) { return /*#__PURE__*/_jsx("span", { ref: function ref(el) { return mergeRef(el, [function (el) { return childRef = el; }, props.refCb]); } }); }; var wrap = render(/*#__PURE__*/_jsx(CompRoot, {}), { instance: true }); expect(wrap.instance().el.nodeName).to.equal("SPAN"); expect(childRef.nodeName).to.equal("SPAN"); }); });