one
Version:
One is a new React Framework that makes Vite serve both native and web.
96 lines • 4.39 kB
JavaScript
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_Protected = require("../views/Protected.cjs");
var import_Screen = require("../views/Screen.cjs");
var import_withLayoutContext = require("./withLayoutContext.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
function readGuardedRedirects(children) {
let result;
function Capture() {
result = (0, import_withLayoutContext.useFilterScreenChildren)(children);
return null;
}
(0, import_react_test_renderer.act)(() => {
import_react_test_renderer.default.create(/* @__PURE__ */(0, import_jsx_runtime.jsx)(Capture, {}));
});
return result.guardedRedirects;
}
(0, import_vitest.describe)("Protected redirectTo", () => {
(0, import_vitest.it)("associates a failed guard redirect with each protected screen", () => {
const redirects = readGuardedRedirects(/* @__PURE__ */(0, import_jsx_runtime.jsxs)(import_Protected.Protected, {
guard: false,
redirectTo: "/login",
children: [/* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Screen.Screen, {
name: "dashboard"
}), /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Screen.Screen, {
name: "settings"
})]
}));
(0, import_vitest.expect)(Array.from(redirects)).toEqual([["dashboard", "/login"], ["settings", "/login"]]);
});
(0, import_vitest.it)("uses the innermost failing guard redirect", () => {
const redirects = readGuardedRedirects(/* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Protected.Protected, {
guard: false,
redirectTo: "/login",
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Protected.Protected, {
guard: false,
redirectTo: "/subscribe",
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Screen.Screen, {
name: "premium"
})
})
}));
(0, import_vitest.expect)(redirects.get("premium")).toBe("/subscribe");
});
(0, import_vitest.it)("inherits a failed parent redirect through a passing child guard", () => {
const redirects = readGuardedRedirects(/* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Protected.Protected, {
guard: false,
redirectTo: "/login",
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Protected.Protected, {
guard: true,
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Screen.Screen, {
name: "account"
})
})
}));
(0, import_vitest.expect)(redirects.get("account")).toBe("/login");
});
(0, import_vitest.it)("ignores redirectTo on a passing parent guard", () => {
const redirects = readGuardedRedirects(/* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Protected.Protected, {
guard: true,
redirectTo: "/login",
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Protected.Protected, {
guard: false,
children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_Screen.Screen, {
name: "account"
})
})
}));
(0, import_vitest.expect)(redirects.has("account")).toBe(true);
(0, import_vitest.expect)(redirects.get("account")).toBeUndefined();
});
});