UNPKG

react-native-web-internals

Version:

React Native for Web

72 lines (71 loc) 2.34 kB
import * as ReactDOM from "react-dom"; import { act } from "react-dom/test-utils"; import useStable from ".."; import { jsx } from "react/jsx-runtime"; function createRoot(rootNode) { return { render(element) { ReactDOM.render(element, rootNode); } }; } describe("useStable", () => { let root, rootNode, spy = {}; const TestComponent = ({ initialValueCallback }) => { const value = useStable(initialValueCallback); return spy.value = value, null; }; beforeEach(() => { spy = {}, rootNode = document.createElement("div"), document.body.appendChild(rootNode), root = createRoot(rootNode); }), afterEach(() => { root.render(null), document.body.removeChild(rootNode), rootNode = null, root = null; }), test("correctly sets the initial value", () => { const initialValueCallback = () => 5; act(() => { root.render(/* @__PURE__ */jsx(TestComponent, { initialValueCallback })); }), expect(spy.value).toBe(5); }), test("does not change the value", () => { let counter = 0; const initialValueCallback = () => counter++; act(() => { root.render(/* @__PURE__ */jsx(TestComponent, { initialValueCallback })); }), expect(spy.value).toBe(0), act(() => { root.render(/* @__PURE__ */jsx(TestComponent, { initialValueCallback })); }), expect(spy.value).toBe(0); }), test("only calls the callback once", () => { let counter = 0; const initialValueCallback = () => counter++; act(() => { root.render(/* @__PURE__ */jsx(TestComponent, { initialValueCallback })); }), expect(counter).toBe(1), act(() => { root.render(/* @__PURE__ */jsx(TestComponent, { initialValueCallback })); }), expect(counter).toBe(1); }), test("does not change the value if the callback initially returns null", () => { let counter = 0; const initialValueCallback = () => counter === 0 ? (counter++, null) : counter++; act(() => { root.render(/* @__PURE__ */jsx(TestComponent, { initialValueCallback })); }), expect(spy.value).toBe(null), act(() => { root.render(/* @__PURE__ */jsx(TestComponent, { initialValueCallback })); }), expect(spy.value).toBe(null); }); }); //# sourceMappingURL=index-test.mjs.map