UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

132 lines 5.72 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__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: true }) : target, mod)); var import_react_test_renderer = __toESM(require("react-test-renderer"), 1); var import_vitest = require("vitest"); var import_ScrollBehavior = require("./ScrollBehavior.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); const subscriptions = import_vitest.vi.hoisted(() => ({ loading: void 0, root: void 0 })); import_vitest.vi.mock("../router/lastAction", () => ({ setLastAction: import_vitest.vi.fn() })); import_vitest.vi.mock("../router/router", () => ({ subscribeToLoadingState: subscriber => { subscriptions.loading = subscriber; return () => { subscriptions.loading = void 0; }; }, subscribeToRootState: subscriber => { subscriptions.root = subscriber; return () => { subscriptions.root = void 0; }; } })); globalThis.IS_REACT_ACT_ENVIRONMENT = true; let renderer; let location; let scrollTo; (0, import_vitest.beforeEach)(() => { process.env.VITE_ENVIRONMENT = "client"; location = { pathname: "/", search: "", hash: "" }; scrollTo = import_vitest.vi.fn(); const windowMock = Object.assign(new EventTarget(), { location, scrollTo, scrollY: 500 }); const storage = /* @__PURE__ */new Map(); import_vitest.vi.stubGlobal("window", windowMock); import_vitest.vi.stubGlobal("document", { getElementById: import_vitest.vi.fn() }); import_vitest.vi.stubGlobal("sessionStorage", { getItem: key => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, value), removeItem: key => storage.delete(key) }); (0, import_react_test_renderer.act)(() => { renderer = import_react_test_renderer.default.create(/* @__PURE__ */(0, import_jsx_runtime.jsx)(import_ScrollBehavior.ScrollBehavior, {})); }); }); (0, import_vitest.afterEach)(() => { (0, import_react_test_renderer.act)(() => renderer?.unmount()); renderer = void 0; import_vitest.vi.useRealTimers(); import_vitest.vi.unstubAllGlobals(); delete process.env.VITE_ENVIRONMENT; }); (0, import_vitest.describe)("ScrollBehavior initial navigation", () => { (0, import_vitest.it)("does not scroll when the navigator publishes its initial state after mount", () => { (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); (0, import_vitest.expect)(scrollTo).not.toHaveBeenCalled(); }); (0, import_vitest.it)("scrolls to the top when the first state notification is a real navigation", () => { location.pathname = "/compat/react-native-gesture-handler"; (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); (0, import_vitest.expect)(scrollTo).toHaveBeenCalledWith(0, 0); }); (0, import_vitest.it)("handles a query change as a first navigation", () => { location.search = "?platform=ios"; (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); (0, import_vitest.expect)(scrollTo).toHaveBeenCalledWith(0, 0); }); }); (0, import_vitest.describe)("ScrollBehavior groups", () => { (0, import_vitest.it)("preserves position between routes in the same group", () => { import_vitest.vi.useFakeTimers(); const unregister = (0, import_ScrollBehavior.registerScrollGroup)("/compat/react-native-gesture-handler"); (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); location.pathname = "/compat/react-native-gesture-handler"; (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); scrollTo.mockClear(); (0, import_react_test_renderer.act)(() => subscriptions.loading?.("loading")); location.pathname = "/compat/react-native-gesture-handler/pan-gesture"; (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); import_vitest.vi.runAllTimers(); (0, import_vitest.expect)(scrollTo).toHaveBeenCalledWith(0, 500); unregister(); import_vitest.vi.useRealTimers(); }); (0, import_vitest.it)("does not match a different route whose slug has the same prefix", () => { const unregister = (0, import_ScrollBehavior.registerScrollGroup)("/compat/react-native"); (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); location.pathname = "/compat/react-native/core"; (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); scrollTo.mockClear(); (0, import_react_test_renderer.act)(() => subscriptions.loading?.("loading")); location.pathname = "/compat/react-native-gesture-handler"; (0, import_react_test_renderer.act)(() => subscriptions.root?.({})); (0, import_vitest.expect)(scrollTo).toHaveBeenCalledWith(0, 0); unregister(); }); });