one
Version:
One is a new React Framework that makes Vite serve both native and web.
74 lines (73 loc) • 2.48 kB
JavaScript
import TestRenderer, { act } from "react-test-renderer";
import { describe, expect, it } from "vitest";
import { Protected } from "../views/Protected.mjs";
import { Screen } from "../views/Screen.mjs";
import { useFilterScreenChildren } from "./withLayoutContext.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
function readGuardedRedirects(children) {
let result;
function Capture() {
result = useFilterScreenChildren(children);
return null;
}
act(() => {
TestRenderer.create(/* @__PURE__ */jsx(Capture, {}));
});
return result.guardedRedirects;
}
describe("Protected redirectTo", () => {
it("associates a failed guard redirect with each protected screen", () => {
const redirects = readGuardedRedirects(/* @__PURE__ */jsxs(Protected, {
guard: false,
redirectTo: "/login",
children: [/* @__PURE__ */jsx(Screen, {
name: "dashboard"
}), /* @__PURE__ */jsx(Screen, {
name: "settings"
})]
}));
expect(Array.from(redirects)).toEqual([["dashboard", "/login"], ["settings", "/login"]]);
});
it("uses the innermost failing guard redirect", () => {
const redirects = readGuardedRedirects(/* @__PURE__ */jsx(Protected, {
guard: false,
redirectTo: "/login",
children: /* @__PURE__ */jsx(Protected, {
guard: false,
redirectTo: "/subscribe",
children: /* @__PURE__ */jsx(Screen, {
name: "premium"
})
})
}));
expect(redirects.get("premium")).toBe("/subscribe");
});
it("inherits a failed parent redirect through a passing child guard", () => {
const redirects = readGuardedRedirects(/* @__PURE__ */jsx(Protected, {
guard: false,
redirectTo: "/login",
children: /* @__PURE__ */jsx(Protected, {
guard: true,
children: /* @__PURE__ */jsx(Screen, {
name: "account"
})
})
}));
expect(redirects.get("account")).toBe("/login");
});
it("ignores redirectTo on a passing parent guard", () => {
const redirects = readGuardedRedirects(/* @__PURE__ */jsx(Protected, {
guard: true,
redirectTo: "/login",
children: /* @__PURE__ */jsx(Protected, {
guard: false,
children: /* @__PURE__ */jsx(Screen, {
name: "account"
})
})
}));
expect(redirects.has("account")).toBe(true);
expect(redirects.get("account")).toBeUndefined();
});
});
//# sourceMappingURL=withLayoutContext.test.mjs.map