react-native-web-internals
Version:
React Native for Web
82 lines (81 loc) • 2.63 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import "react";
import * as ReactDOM from "react-dom";
import { act } from "react-dom/test-utils";
import useStable from "../index.native.js";
function createRoot(rootNode) {
return {
render(element) {
ReactDOM.render(element, rootNode);
}
};
}
describe("useStable", function () {
var root,
rootNode,
spy = {},
TestComponent = function (param) {
var {
initialValueCallback
} = param,
value = useStable(initialValueCallback);
return spy.value = value, null;
};
beforeEach(function () {
spy = {}, rootNode = document.createElement("div"), document.body.appendChild(rootNode), root = createRoot(rootNode);
}), afterEach(function () {
root.render(null), document.body.removeChild(rootNode), rootNode = null, root = null;
}), test("correctly sets the initial value", function () {
var initialValueCallback = function () {
return 5;
};
act(function () {
root.render(/* @__PURE__ */_jsx(TestComponent, {
initialValueCallback
}));
}), expect(spy.value).toBe(5);
}), test("does not change the value", function () {
var counter = 0,
initialValueCallback = function () {
return counter++;
};
act(function () {
root.render(/* @__PURE__ */_jsx(TestComponent, {
initialValueCallback
}));
}), expect(spy.value).toBe(0), act(function () {
root.render(/* @__PURE__ */_jsx(TestComponent, {
initialValueCallback
}));
}), expect(spy.value).toBe(0);
}), test("only calls the callback once", function () {
var counter = 0,
initialValueCallback = function () {
return counter++;
};
act(function () {
root.render(/* @__PURE__ */_jsx(TestComponent, {
initialValueCallback
}));
}), expect(counter).toBe(1), act(function () {
root.render(/* @__PURE__ */_jsx(TestComponent, {
initialValueCallback
}));
}), expect(counter).toBe(1);
}), test("does not change the value if the callback initially returns null", function () {
var counter = 0,
initialValueCallback = function () {
return counter === 0 ? (counter++, null) : counter++;
};
act(function () {
root.render(/* @__PURE__ */_jsx(TestComponent, {
initialValueCallback
}));
}), expect(spy.value).toBe(null), act(function () {
root.render(/* @__PURE__ */_jsx(TestComponent, {
initialValueCallback
}));
}), expect(spy.value).toBe(null);
});
});
//# sourceMappingURL=index-test.native.js.map