Pane 2 mit Edit, Delete, sort, etc.
Node.js build / build (20.x) (push) Waiting to run
Node.js build / build (22.x) (push) Waiting to run

This commit is contained in:
Lars committed 2026-01-16 19:25:55 +01:00
1 parent 8ba098c780
commit 684217bdf4
6 files changed
+1070 -274

No files matched your search

+383
View File
@@ -0,0 +1,383 @@
import { describe, it, expect } from "vitest";
import {
createLoopState,
setDraftField,
isDraftDirty,
commitDraft,
startEdit,
deleteItem,
moveItemUp,
moveItemDown,
clearDraft,
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
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,
};
clearDraft(state);
expect(state.draft).toEqual({ field1: "value1" });
expect(state.editIndex).toBe(0);
});
});
});