521 lines
15 KiB
TypeScript
521 lines
15 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import {
|
|
createLoopState,
|
|
setDraftField,
|
|
isDraftDirty,
|
|
commitDraft,
|
|
startEdit,
|
|
deleteItem,
|
|
moveItemUp,
|
|
moveItemDown,
|
|
clearDraft,
|
|
setActiveItemStepIndex,
|
|
itemNextStep,
|
|
itemPrevStep,
|
|
resetItemWizard,
|
|
type LoopRuntimeState,
|
|
} from "../../interview/loopState";
|
|
|
|
describe("loopState", () => {
|
|
describe("createLoopState", () => {
|
|
it("should create initial empty state", () => {
|
|
const state = createLoopState();
|
|
expect(state.items).toEqual([]);
|
|
expect(state.draft).toEqual({});
|
|
expect(state.editIndex).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("setDraftField", () => {
|
|
it("should set a field in draft", () => {
|
|
const state = createLoopState();
|
|
const newState = setDraftField(state, "field1", "value1");
|
|
expect(newState.draft.field1).toBe("value1");
|
|
expect(newState.items).toEqual([]);
|
|
expect(newState.editIndex).toBeNull();
|
|
});
|
|
|
|
it("should update existing field", () => {
|
|
const state = createLoopState();
|
|
let newState = setDraftField(state, "field1", "value1");
|
|
newState = setDraftField(newState, "field1", "value2");
|
|
expect(newState.draft.field1).toBe("value2");
|
|
});
|
|
|
|
it("should not mutate original state", () => {
|
|
const state = createLoopState();
|
|
setDraftField(state, "field1", "value1");
|
|
expect(state.draft).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("isDraftDirty", () => {
|
|
it("should return false for empty draft", () => {
|
|
expect(isDraftDirty({})).toBe(false);
|
|
});
|
|
|
|
it("should return true for non-empty string", () => {
|
|
expect(isDraftDirty({ text: "content" })).toBe(true);
|
|
});
|
|
|
|
it("should return false for empty string", () => {
|
|
expect(isDraftDirty({ text: "" })).toBe(false);
|
|
expect(isDraftDirty({ text: " " })).toBe(false);
|
|
});
|
|
|
|
it("should return true for boolean true", () => {
|
|
expect(isDraftDirty({ flag: true })).toBe(true);
|
|
});
|
|
|
|
it("should return false for boolean false", () => {
|
|
expect(isDraftDirty({ flag: false })).toBe(false);
|
|
});
|
|
|
|
it("should return true for non-zero number", () => {
|
|
expect(isDraftDirty({ count: 5 })).toBe(true);
|
|
expect(isDraftDirty({ count: -1 })).toBe(true);
|
|
});
|
|
|
|
it("should return false for zero", () => {
|
|
expect(isDraftDirty({ count: 0 })).toBe(false);
|
|
});
|
|
|
|
it("should return true for non-empty array", () => {
|
|
expect(isDraftDirty({ items: [1, 2] })).toBe(true);
|
|
});
|
|
|
|
it("should return false for empty array", () => {
|
|
expect(isDraftDirty({ items: [] })).toBe(false);
|
|
});
|
|
|
|
it("should return true for non-empty object", () => {
|
|
expect(isDraftDirty({ obj: { key: "value" } })).toBe(true);
|
|
});
|
|
|
|
it("should return false for empty object", () => {
|
|
expect(isDraftDirty({ obj: {} })).toBe(false);
|
|
});
|
|
|
|
it("should ignore null and undefined", () => {
|
|
expect(isDraftDirty({ nullVal: null, undefinedVal: undefined })).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("commitDraft", () => {
|
|
it("should add new item when editIndex is null", () => {
|
|
const state = createLoopState();
|
|
let newState = setDraftField(state, "field1", "value1");
|
|
newState = commitDraft(newState);
|
|
|
|
expect(newState.items.length).toBe(1);
|
|
expect(newState.items[0]).toEqual({ field1: "value1" });
|
|
expect(newState.draft).toEqual({});
|
|
expect(newState.editIndex).toBeNull();
|
|
});
|
|
|
|
it("should update existing item when editIndex is set", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ field1: "old" }],
|
|
draft: { field1: "new" },
|
|
editIndex: 0,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = commitDraft(state);
|
|
|
|
expect(newState.items.length).toBe(1);
|
|
expect(newState.items[0]).toEqual({ field1: "new" });
|
|
expect(newState.draft).toEqual({});
|
|
expect(newState.editIndex).toBeNull();
|
|
});
|
|
|
|
it("should not commit if draft is not dirty", () => {
|
|
const state = createLoopState();
|
|
const newState = commitDraft(state);
|
|
|
|
expect(newState.items.length).toBe(0);
|
|
expect(newState.draft).toEqual({});
|
|
});
|
|
|
|
it("should not mutate original state", () => {
|
|
const state = createLoopState();
|
|
let newState = setDraftField(state, "field1", "value1");
|
|
commitDraft(newState);
|
|
expect(newState.draft).toEqual({ field1: "value1" });
|
|
});
|
|
});
|
|
|
|
describe("startEdit", () => {
|
|
it("should load item into draft and set editIndex", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ field1: "value1", field2: "value2" }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = startEdit(state, 0);
|
|
|
|
expect(newState.draft).toEqual({ field1: "value1", field2: "value2" });
|
|
expect(newState.editIndex).toBe(0);
|
|
expect(newState.items.length).toBe(1);
|
|
});
|
|
|
|
it("should handle invalid index", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ field1: "value1" }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = startEdit(state, 5);
|
|
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
|
|
it("should not mutate original state", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ field1: "value1" }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
startEdit(state, 0);
|
|
expect(state.draft).toEqual({});
|
|
expect(state.editIndex).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("deleteItem", () => {
|
|
it("should delete item at index", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }, { id: 3 }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = deleteItem(state, 1);
|
|
|
|
expect(newState.items.length).toBe(2);
|
|
expect(newState.items[0]).toEqual({ id: 1 });
|
|
expect(newState.items[1]).toEqual({ id: 3 });
|
|
});
|
|
|
|
it("should clear draft if editing deleted item", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }],
|
|
draft: { id: 2 },
|
|
editIndex: 1,
|
|
activeItemStepIndex: 1,
|
|
};
|
|
|
|
const newState = deleteItem(state, 1);
|
|
|
|
expect(newState.items.length).toBe(1);
|
|
expect(newState.editIndex).toBeNull();
|
|
expect(newState.draft).toEqual({});
|
|
});
|
|
|
|
it("should adjust editIndex when deleting before edited item", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }, { id: 3 }],
|
|
draft: { id: 3 },
|
|
editIndex: 2,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = deleteItem(state, 1);
|
|
|
|
expect(newState.items.length).toBe(2);
|
|
expect(newState.editIndex).toBe(1); // Adjusted from 2 to 1
|
|
});
|
|
|
|
it("should handle invalid index", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = deleteItem(state, 5);
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
});
|
|
|
|
describe("moveItemUp", () => {
|
|
it("should move item up", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }, { id: 3 }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemUp(state, 1);
|
|
|
|
expect(newState.items[0]).toEqual({ id: 2 });
|
|
expect(newState.items[1]).toEqual({ id: 1 });
|
|
expect(newState.items[2]).toEqual({ id: 3 });
|
|
});
|
|
|
|
it("should adjust editIndex when moving edited item", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }],
|
|
draft: { id: 2 },
|
|
editIndex: 1,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemUp(state, 1);
|
|
|
|
expect(newState.items[0]).toEqual({ id: 2 });
|
|
expect(newState.editIndex).toBe(0);
|
|
});
|
|
|
|
it("should adjust editIndex when moving item before edited item", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }, { id: 3 }],
|
|
draft: { id: 3 },
|
|
editIndex: 2,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemUp(state, 1);
|
|
|
|
// After moving item 1 up: [id:2, id:1, id:3]
|
|
expect(newState.items[0]).toEqual({ id: 2 });
|
|
expect(newState.items[1]).toEqual({ id: 1 });
|
|
expect(newState.items[2]).toEqual({ id: 3 }); // Item 3 stays at index 2
|
|
expect(newState.editIndex).toBe(2); // Still editing item 3, still at index 2
|
|
});
|
|
|
|
it("should not move first item", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemUp(state, 0);
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
|
|
it("should handle invalid index", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemUp(state, 5);
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
});
|
|
|
|
describe("moveItemDown", () => {
|
|
it("should move item down", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }, { id: 3 }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemDown(state, 1);
|
|
|
|
expect(newState.items[0]).toEqual({ id: 1 });
|
|
expect(newState.items[1]).toEqual({ id: 3 });
|
|
expect(newState.items[2]).toEqual({ id: 2 });
|
|
});
|
|
|
|
it("should adjust editIndex when moving edited item", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }],
|
|
draft: { id: 1 },
|
|
editIndex: 0,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemDown(state, 0);
|
|
|
|
expect(newState.items[0]).toEqual({ id: 2 });
|
|
expect(newState.items[1]).toEqual({ id: 1 });
|
|
expect(newState.editIndex).toBe(1);
|
|
});
|
|
|
|
it("should not move last item", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }, { id: 2 }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemDown(state, 1);
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
|
|
it("should handle invalid index", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 0,
|
|
};
|
|
|
|
const newState = moveItemDown(state, 5);
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
});
|
|
|
|
describe("clearDraft", () => {
|
|
it("should clear draft and reset editIndex", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ id: 1 }],
|
|
draft: { field1: "value1" },
|
|
editIndex: 0,
|
|
activeItemStepIndex: 2,
|
|
};
|
|
|
|
const newState = clearDraft(state);
|
|
|
|
expect(newState.draft).toEqual({});
|
|
expect(newState.editIndex).toBeNull();
|
|
expect(newState.items.length).toBe(1);
|
|
});
|
|
|
|
it("should not mutate original state", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [],
|
|
draft: { field1: "value1" },
|
|
editIndex: 0,
|
|
activeItemStepIndex: 1,
|
|
};
|
|
|
|
clearDraft(state);
|
|
expect(state.draft).toEqual({ field1: "value1" });
|
|
expect(state.editIndex).toBe(0);
|
|
expect(state.activeItemStepIndex).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe("subwizard navigation", () => {
|
|
it("should set active item step index", () => {
|
|
const state = createLoopState();
|
|
const newState = setActiveItemStepIndex(state, 2);
|
|
expect(newState.activeItemStepIndex).toBe(2);
|
|
expect(newState.items).toEqual([]);
|
|
});
|
|
|
|
it("should not set negative index", () => {
|
|
const state = createLoopState();
|
|
const newState = setActiveItemStepIndex(state, -1);
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
|
|
it("should move to next step", () => {
|
|
const state = createLoopState();
|
|
let newState = itemNextStep(state, 3);
|
|
expect(newState.activeItemStepIndex).toBe(1);
|
|
|
|
newState = itemNextStep(newState, 3);
|
|
expect(newState.activeItemStepIndex).toBe(2);
|
|
});
|
|
|
|
it("should not move beyond last step", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 2,
|
|
};
|
|
|
|
const newState = itemNextStep(state, 3);
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
|
|
it("should move to previous step", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 2,
|
|
};
|
|
|
|
let newState = itemPrevStep(state);
|
|
expect(newState.activeItemStepIndex).toBe(1);
|
|
|
|
newState = itemPrevStep(newState);
|
|
expect(newState.activeItemStepIndex).toBe(0);
|
|
});
|
|
|
|
it("should not move before first step", () => {
|
|
const state = createLoopState();
|
|
const newState = itemPrevStep(state);
|
|
expect(newState).toBe(state); // Should return unchanged
|
|
});
|
|
|
|
it("should reset item wizard to first step", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 3,
|
|
};
|
|
|
|
const newState = resetItemWizard(state);
|
|
expect(newState.activeItemStepIndex).toBe(0);
|
|
});
|
|
|
|
it("should reset to first step when starting edit", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [{ field1: "value1" }],
|
|
draft: {},
|
|
editIndex: null,
|
|
activeItemStepIndex: 2,
|
|
};
|
|
|
|
const newState = startEdit(state, 0);
|
|
expect(newState.activeItemStepIndex).toBe(0);
|
|
expect(newState.editIndex).toBe(0);
|
|
});
|
|
|
|
it("should reset to first step when committing draft", () => {
|
|
const state: LoopRuntimeState = {
|
|
items: [],
|
|
draft: { field1: "value1" },
|
|
editIndex: null,
|
|
activeItemStepIndex: 2,
|
|
};
|
|
|
|
const newState = commitDraft(state);
|
|
expect(newState.activeItemStepIndex).toBe(0);
|
|
expect(newState.items.length).toBe(1);
|
|
});
|
|
|
|
it("should allow commit:on_next even when not on last step", () => {
|
|
// Test that commit logic works regardless of activeItemStepIndex
|
|
const state: LoopRuntimeState = {
|
|
items: [],
|
|
draft: { field1: "value1", field2: "value2" },
|
|
editIndex: null,
|
|
activeItemStepIndex: 1, // Not on last step
|
|
};
|
|
|
|
// Draft is dirty, should commit
|
|
expect(isDraftDirty(state.draft)).toBe(true);
|
|
|
|
const newState = commitDraft(state);
|
|
expect(newState.items.length).toBe(1);
|
|
expect(newState.activeItemStepIndex).toBe(0);
|
|
expect(newState.draft).toEqual({});
|
|
});
|
|
});
|
|
});
|