react-native-web-internals
Version:
React Native for Web
99 lines (98 loc) • 4.42 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
mod
));
var import_jsx_runtime = require("react/jsx-runtime"), import_react = require("@testing-library/react"), React = __toESM(require("react")), import_test_utils = require("react-dom/test-utils"), import__ = __toESM(require(".."));
describe("modules/useMergeRefs/index.js", function() {
function TestComponent(param) {
var { refs, ...rest } = param, mergedRef = (0, import__.default)(...refs);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
ref: mergedRef,
...rest
});
}
afterEach(import_react.cleanup), test("handles no refs", function() {
(0, import_test_utils.act)(function() {
(0, import_react.render)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TestComponent, {
refs: []
}));
});
}), test("merges any number of varying refs", function() {
var callbackRef1 = jest.fn(), callbackRef2 = jest.fn(), objectRef1 = /* @__PURE__ */ React.createRef(), objectRef2 = /* @__PURE__ */ React.createRef(), nullRef = null;
(0, import_test_utils.act)(function() {
(0, import_react.render)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TestComponent, {
refs: [
callbackRef1,
callbackRef2,
objectRef1,
objectRef2,
nullRef
]
}));
}), expect(callbackRef1).toHaveBeenCalledTimes(1), expect(callbackRef2).toHaveBeenCalledTimes(1), expect(objectRef1.current).toBeInstanceOf(HTMLDivElement), expect(objectRef2.current).toBeInstanceOf(HTMLDivElement);
}), test("ref is called when ref changes", function() {
var ref = jest.fn(), nextRef = jest.fn(), rerender;
(0, import_test_utils.act)(function() {
({ rerender } = (0, import_react.render)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TestComponent, {
refs: [
ref
]
})));
}), expect(ref).toHaveBeenCalled(), (0, import_test_utils.act)(function() {
rerender(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TestComponent, {
refs: [
nextRef
]
}));
}), expect(nextRef).toHaveBeenCalled();
}), test("ref is not called for each rerender", function() {
var ref = jest.fn(), rerender;
(0, import_test_utils.act)(function() {
({ rerender } = (0, import_react.render)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TestComponent, {
refs: [
ref
]
})));
}), expect(ref).toHaveBeenCalledTimes(1), (0, import_test_utils.act)(function() {
rerender(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TestComponent, {
refs: [
ref
]
}));
}), expect(ref).toHaveBeenCalledTimes(1);
}), test("ref is not called for props changes", function() {
var ref = jest.fn(), rerender;
(0, import_test_utils.act)(function() {
({ rerender } = (0, import_react.render)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TestComponent, {
id: "foo",
refs: [
ref
]
})));
}), expect(ref).toHaveBeenCalledTimes(1), (0, import_test_utils.act)(function() {
rerender(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TestComponent, {
id: "bar",
refs: [
ref
]
}));
}), expect(ref).toHaveBeenCalledTimes(1);
});
});
//# sourceMappingURL=index-test.js.map