1
0
Fork 0
polaris-task-force/tests/int/sidebar-order.int.spec.ts
Z8MB1E 0ca35ec251 feat(sidebar): add server-persisted section reorder with customize mode
- preferences.display.sidebarOrder json field on Users + named migration
  add_sidebar_order_preference + regenerated types
- New pure helper src/lib/sidebar/order.ts (section ids, labels, default
  order, normalizeSidebarOrder) modeled on lib/dashboard/layout.ts, with
  tests/int/sidebar-order.int.spec.ts covering fallback/dupes/missing ids
- AppSidebar renders sections from the normalized order; feature-flagged-off
  sections skip their slot and keep stored positions
- Customize mode: dnd-kit sortable grips on group headers, save-on-drop via
  new updateSidebarOrder action, Reset clears the preference, Done exits;
  hidden while the sidebar is collapsed
- updateSidebarOrder sanitizes server-side and rewrites full preferences
- Fix sibling clobbering: saveDashboardLayout now preserves sidebarOrder,
  and updatePreferences now preserves both sidebarOrder and dashboard.layout
2026-10-12 13:46:48 -04:00

75 lines
2.6 KiB
TypeScript

/**
* Sidebar order preference tests. Pure module (src/lib/sidebar/order.ts),
* no database access needed; lives under tests/int per repo convention for
* vitest specs.
*/
import { describe, expect, it } from "vitest";
import {
DEFAULT_SIDEBAR_ORDER,
SIDEBAR_SECTION_IDS,
isSidebarSectionId,
normalizeSidebarOrder,
} from "@/lib/sidebar/order";
describe("normalizeSidebarOrder", () => {
it("falls back to the default order for non-array input", () => {
expect(normalizeSidebarOrder(undefined)).toEqual([...DEFAULT_SIDEBAR_ORDER]);
expect(normalizeSidebarOrder(null)).toEqual([...DEFAULT_SIDEBAR_ORDER]);
expect(normalizeSidebarOrder("platform")).toEqual([...DEFAULT_SIDEBAR_ORDER]);
expect(normalizeSidebarOrder({ 0: "platform" })).toEqual([...DEFAULT_SIDEBAR_ORDER]);
});
it("keeps known ids in the user's order", () => {
const custom = ["personal", "platform", "map"];
const result = normalizeSidebarOrder(custom);
expect(result.slice(0, 3)).toEqual(["personal", "platform", "map"]);
});
it("drops unknown and duplicate ids", () => {
const result = normalizeSidebarOrder([
"platform",
"not-a-section",
42,
null,
"platform",
"map",
]);
expect(result.filter((id) => id === "platform")).toEqual(["platform"]);
expect(result).toContain("map");
expect(result).not.toContain("not-a-section");
});
it("appends missing sections in default order so the list is always complete", () => {
const result = normalizeSidebarOrder(["personal"]);
expect(result[0]).toBe("personal");
expect(result.slice(1)).toEqual(DEFAULT_SIDEBAR_ORDER.filter((id) => id !== "personal"));
});
it("always returns every section id exactly once", () => {
const inputs: unknown[] = [
[],
["commerce", "commerce"],
[...SIDEBAR_SECTION_IDS].reverse(),
["bogus", ...SIDEBAR_SECTION_IDS, "bogus"],
];
for (const input of inputs) {
const result = normalizeSidebarOrder(input);
expect(result).toHaveLength(SIDEBAR_SECTION_IDS.length);
expect(new Set(result).size).toBe(SIDEBAR_SECTION_IDS.length);
for (const id of SIDEBAR_SECTION_IDS) {
expect(result).toContain(id);
}
}
});
});
describe("isSidebarSectionId", () => {
it("accepts only known section ids", () => {
expect(isSidebarSectionId("platform")).toBe(true);
expect(isSidebarSectionId("personal")).toBe(true);
expect(isSidebarSectionId("Platform")).toBe(false);
expect(isSidebarSectionId(undefined)).toBe(false);
expect(isSidebarSectionId(7)).toBe(false);
});
});