one
Version:
One is a new React Framework that makes Vite serve both native and web.
168 lines (167 loc) • 5.65 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React from "react";
import TestRenderer, { act } from "react-test-renderer";
import { describe, expect, it } from "vitest";
import { usePathname } from "../hooks.native.js";
import { Root } from "../Root.native.js";
import { Slot } from "../views/Navigator.native.js";
var _globalThis;
var _requestAnimationFrame;
var _globalThis1;
var _cancelAnimationFrame;
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
(_requestAnimationFrame = (_globalThis = globalThis).requestAnimationFrame) !== null && _requestAnimationFrame !== void 0 ? _requestAnimationFrame : _globalThis.requestAnimationFrame = function (cb) {
return setTimeout(function () {
return cb(0);
}, 0);
};
(_cancelAnimationFrame = (_globalThis1 = globalThis).cancelAnimationFrame) !== null && _cancelAnimationFrame !== void 0 ? _cancelAnimationFrame : _globalThis1.cancelAnimationFrame = function (id) {
return clearTimeout(id);
};
var routerRoot = "app";
function makeRoutes(modules) {
var _globalThis___vxrnVersion;
globalThis["__vxrnVersion"] = ((_globalThis___vxrnVersion = globalThis["__vxrnVersion"]) !== null && _globalThis___vxrnVersion !== void 0 ? _globalThis___vxrnVersion : 0) + 1;
var routes = {};
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = Object.entries(modules)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var [file, module] = _step.value;
routes[`/${routerRoot}/${file}`] = module;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return routes;
}
var slotLayout = {
default: function () {
return /* @__PURE__ */_jsx(Slot, {});
}
};
var leaf = function (label) {
return {
default: function () {
return label;
}
};
};
function routeTreeWithGate(gate) {
var Gate = gate;
return makeRoutes({
"_layout.tsx": slotLayout,
"(app)/_layout.tsx": {
default: function () {
return /* @__PURE__ */_jsx(Gate, {
children: /* @__PURE__ */_jsx(Slot, {})
});
}
},
"(app)/index.tsx": leaf("app-index"),
"(app)/project/_layout.tsx": slotLayout,
"(app)/project/[projectId]/_layout.tsx": slotLayout,
"(app)/project/[projectId]/[sessionId]/_layout.tsx": slotLayout,
"(app)/project/[projectId]/[sessionId]/index.tsx": leaf("session"),
"(site)/_layout.tsx": slotLayout,
"(site)/pricing.tsx": leaf("pricing")
});
}
function renderAt(path, routes) {
var renderer;
act(function () {
renderer = TestRenderer.create(/* @__PURE__ */_jsx(Root, {
path,
routes,
routerRoot,
isClient: false
}));
});
return renderer;
}
function bodyText(renderer) {
var collect = function (node) {
var _node_children;
return typeof node === "string" ? node : Array.isArray(node) ? node.map(collect).join("") : node ? collect((_node_children = node.children) !== null && _node_children !== void 0 ? _node_children : []) : "";
};
return collect(renderer.toJSON());
}
describe("a layout that renders something other than its <Slot />", function () {
it("does not move the pathname while the URL stands still", function () {
var pathnames = [];
var setBlocked;
function Probe() {
var pathname = usePathname();
if (pathnames[pathnames.length - 1] !== pathname) pathnames.push(pathname);
return null;
}
function Gate(param) {
var {
children
} = param;
var [blocked, set] = React.useState(true);
setBlocked = set;
return /* @__PURE__ */_jsxs(_Fragment, {
children: [/* @__PURE__ */_jsx(Probe, {}), blocked ? "placeholder" : children]
});
}
var renderer = renderAt("/project/p1/main", routeTreeWithGate(Gate));
expect(bodyText(renderer)).toBe("placeholder");
act(function () {
return setBlocked(false);
});
expect(bodyText(renderer)).toBe("session");
act(function () {
return setBlocked(true);
});
expect(bodyText(renderer)).toBe("placeholder");
act(function () {
return setBlocked(false);
});
expect(pathnames).toEqual(["/project/p1/main"]);
});
it("settles when the gate itself reads the pathname", async function () {
var renders = 0;
var requestBlock;
function Gate(param) {
var {
children
} = param;
var pathname = usePathname();
var [blockRequested, setBlockRequested] = React.useState(false);
requestBlock = function () {
return setBlockRequested(true);
};
renders++;
if (renders > 100) {
throw new Error(`gate rendered ${renders} times without settling`);
}
var blocked = blockRequested && pathname.startsWith("/project/");
return /* @__PURE__ */_jsx(_Fragment, {
children: blocked ? "blocked" : children
});
}
var renderer = renderAt("/project/p1/main", routeTreeWithGate(Gate));
await act(async function () {});
expect(bodyText(renderer)).toBe("session");
await act(async function () {
return requestBlock();
});
await act(async function () {});
expect(bodyText(renderer)).toBe("blocked");
expect(renders).toBeLessThan(20);
});
});
//# sourceMappingURL=conditionalSlotPathname.test.native.js.map