CI / Static checks (push) Has been cancelled
CI / Production build (push) Has been cancelled
CI / API integration (push) Has been cancelled
CI / Taro builds (acheng-campus-food) (push) Has been cancelled
CI / Taro builds (atelier-solopreneur) (push) Has been cancelled
CI / Taro builds (linjing-editorial) (push) Has been cancelled
CI / Browser regression (push) Has been cancelled
76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
import { createElement } from "react";
|
|
import { renderToString } from "react-dom/server";
|
|
import { MemoryRouter, Route, Routes, resolvePath } from "react-router-dom";
|
|
import { AdminOverviewRedirect, AdminRouter } from "../src/app/AdminRouter.ts";
|
|
import {
|
|
canCreateBrand,
|
|
createBrandFallbackPath,
|
|
createBrandRouteElement,
|
|
} from "../src/app/route-access.ts";
|
|
|
|
test("后台父路由正确匹配所有菜单及深层链接", () => {
|
|
const pages = [
|
|
"overview",
|
|
"publication",
|
|
"brand",
|
|
"brands",
|
|
"brands/new",
|
|
"page-scheme",
|
|
"trust",
|
|
"services",
|
|
"inquiries",
|
|
"workspace",
|
|
"capabilities",
|
|
"members",
|
|
];
|
|
for (const page of pages) {
|
|
const html = renderToString(
|
|
createElement(
|
|
MemoryRouter,
|
|
{ initialEntries: [`/admin/${page}`] },
|
|
createElement(
|
|
AdminRouter,
|
|
null,
|
|
createElement(
|
|
Routes,
|
|
null,
|
|
...pages.map((key) =>
|
|
createElement(Route, { key, path: key, element: createElement("h1", null, key) }),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
assert.equal(html, `<h1>${page}</h1>`);
|
|
}
|
|
});
|
|
|
|
test("默认与无效地址的概览重定向为绝对地址且替换历史记录", () => {
|
|
const redirect = AdminOverviewRedirect();
|
|
assert.equal(redirect.props.replace, true);
|
|
for (const pathname of [
|
|
"/",
|
|
"/admin",
|
|
"/admin/unknown",
|
|
`/admin/publication${"/overview".repeat(100)}`,
|
|
]) {
|
|
assert.equal(resolvePath(redirect.props.to, pathname).pathname, "/admin/overview");
|
|
}
|
|
});
|
|
|
|
test("只有品牌负责人可以进入创建品牌路由", () => {
|
|
assert.equal(canCreateBrand("owner"), true);
|
|
for (const role of ["admin", "editor", "platform-admin"]) {
|
|
assert.equal(canCreateBrand(role), false);
|
|
}
|
|
|
|
const content = createElement("h1", null, "create-brand");
|
|
assert.equal(createBrandRouteElement("owner", content), content);
|
|
|
|
const denied = createBrandRouteElement("admin", content);
|
|
assert.equal(denied.props.to, createBrandFallbackPath);
|
|
assert.equal(denied.props.replace, true);
|
|
});
|