Files
kandev/apps/web/hooks/use-user-display-settings.test.ts
T
2026-08-10 22:38:37 +01:00

94 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { UserSettingsState } from "@/lib/state/slices/settings/types";
import {
buildSettingsUpdatePayload,
isSettingsUnchanged,
normalizeHiddenStepIds,
} from "./use-user-display-settings";
function settings(tasksListShowDetails: boolean): UserSettingsState {
return {
loaded: true,
workspaceId: "workspace-1",
workflowId: null,
repositoryIds: [],
tasksListShowDetails,
} as unknown as UserSettingsState;
}
function settingsWithHidden(hiddenWorkflowStepIds: Record<string, string[]>): UserSettingsState {
return {
loaded: true,
workspaceId: "workspace-1",
workflowId: null,
repositoryIds: [],
tasksListShowDetails: false,
hiddenWorkflowStepIds,
} as unknown as UserSettingsState;
}
describe("isSettingsUnchanged", () => {
it("detects a details-only preference change", () => {
expect(isSettingsUnchanged(settings(true), settings(false))).toBe(false);
});
it("treats reordered hidden step ids for the same workflow as unchanged", () => {
const normalized = settingsWithHidden({ "wf-1": ["step-a", "step-b"] });
const current = settingsWithHidden({ "wf-1": ["step-b", "step-a"] });
expect(isSettingsUnchanged(normalized, current)).toBe(true);
});
it("detects an actual hidden step id change for a workflow", () => {
const normalized = settingsWithHidden({ "wf-1": ["step-a"] });
const current = settingsWithHidden({ "wf-1": ["step-b"] });
expect(isSettingsUnchanged(normalized, current)).toBe(false);
});
it("detects a hidden workflow being added or removed", () => {
const normalized = settingsWithHidden({ "wf-1": ["step-a"], "wf-2": ["step-c"] });
const current = settingsWithHidden({ "wf-1": ["step-a"] });
expect(isSettingsUnchanged(normalized, current)).toBe(false);
});
it("treats both empty hidden-step maps as unchanged", () => {
expect(isSettingsUnchanged(settingsWithHidden({}), settingsWithHidden({}))).toBe(true);
});
});
describe("normalizeHiddenStepIds", () => {
it("dedupes and sorts ids within each workflow", () => {
expect(normalizeHiddenStepIds({ "wf-1": ["step-b", "step-a", "step-b"] })).toEqual({
"wf-1": ["step-a", "step-b"],
});
});
it("drops a workflow entry whose id array becomes empty", () => {
expect(normalizeHiddenStepIds({ "wf-1": [], "wf-2": ["step-a"] })).toEqual({
"wf-2": ["step-a"],
});
});
it("returns an empty object for an empty input", () => {
expect(normalizeHiddenStepIds({})).toEqual({});
});
});
describe("buildSettingsUpdatePayload", () => {
// Guards the literal wire key: a silent rename here would break persistence
// with zero test failures anywhere else, since the E2E persistence spec
// drives the real UI and would only catch it after a full round trip.
it("sends hidden step ids under the kanban_hidden_step_ids wire key", () => {
const normalized = settingsWithHidden({ "wf-1": ["step-a", "step-b"] });
expect(buildSettingsUpdatePayload(normalized)).toMatchObject({
kanban_hidden_step_ids: { "wf-1": ["step-a", "step-b"] },
});
});
it("sends an empty hidden-step map as-is", () => {
const normalized = settingsWithHidden({});
expect(buildSettingsUpdatePayload(normalized)).toMatchObject({
kanban_hidden_step_ids: {},
});
});
});