Files
jiang13-bbs/frontend/lib/headerNav.test.ts

79 lines
2.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
DEFAULT_HEADER_NAV,
HEADER_NAV_BUILTIN_KEYS,
isHeaderNavCustom,
normalizeHeaderNav,
type HeaderNavItem,
} from "./headerNav.ts";
describe("normalizeHeaderNav", () => {
it("缺行/非法输入回落内置默认三项", () => {
assert.deepEqual(normalizeHeaderNav(undefined), DEFAULT_HEADER_NAV);
assert.deepEqual(normalizeHeaderNav(null), DEFAULT_HEADER_NAV);
assert.deepEqual(normalizeHeaderNav("home"), DEFAULT_HEADER_NAV);
});
it("默认内置项顺序与注册表一致", () => {
assert.equal(DEFAULT_HEADER_NAV.length, HEADER_NAV_BUILTIN_KEYS.length);
assert.deepEqual(
DEFAULT_HEADER_NAV.map((i) => (i.type === "builtin" ? i.key : "")),
[...HEADER_NAV_BUILTIN_KEYS],
);
});
it("显式空数组表示全部隐藏,不回落默认", () => {
assert.deepEqual(normalizeHeaderNav([]), []);
});
it("自定义顺序与内容原样保留", () => {
const items: HeaderNavItem[] = [
{ type: "builtin", key: "library" },
{ type: "custom", label: "关于", url: "/p/about", new_tab: false },
{ type: "builtin", key: "home" },
{ type: "custom", label: "官网", url: "https://example.com", new_tab: true },
];
assert.deepEqual(normalizeHeaderNav(items), items);
});
it("内置 key 去空白/小写容错、重复去重", () => {
assert.deepEqual(
normalizeHeaderNav([
{ type: "builtin", key: " HOME " },
{ type: "builtin", key: "home" },
{ type: "builtin", key: "library" },
]),
[
{ type: "builtin", key: "home" },
{ type: "builtin", key: "library" },
],
);
});
it("未知内置 key 与非法自定义项被剔除", () => {
assert.deepEqual(
normalizeHeaderNav([
{ type: "builtin", key: "nope" },
{ type: "custom", label: "", url: "/x" },
{ type: "custom", label: "坏地址", url: "javascript:alert(1)" },
{ type: "custom", label: "相对路径", url: "relative" },
{ type: "weird", key: "home" },
{ type: "custom", label: "合法", url: "/links" },
]),
[{ type: "custom", label: "合法", url: "/links", new_tab: false }],
);
});
it("自定义外链 new_tab 缺省为 true,站内为 false", () => {
const got = normalizeHeaderNav([
{ type: "custom", label: "外链", url: "https://example.com" },
{ type: "custom", label: "站内", url: "/p/about" },
]);
assert.equal(got.length, 2);
assert.ok(got.every(isHeaderNavCustom));
assert.equal((got[0] as { new_tab: boolean }).new_tab, true);
assert.equal((got[1] as { new_tab: boolean }).new_tab, false);
});
});