diff --git a/apps/client/src/widgets/view_widgets/table_view/columns.spec.ts b/apps/client/src/widgets/view_widgets/table_view/columns.spec.ts new file mode 100644 index 000000000..64d5bc4b3 --- /dev/null +++ b/apps/client/src/widgets/view_widgets/table_view/columns.spec.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from "vitest"; +import { restoreExistingData } from "./columns"; +import type { ColumnDefinition } from "tabulator-tables"; + +describe("restoreExistingData", () => { + it("should restore existing column data", () => { + const newDefs: ColumnDefinition[] = [ + { field: "title", title: "Title", editor: "input" }, + { field: "noteId", title: "Note ID", visible: false } + ]; + const oldDefs: ColumnDefinition[] = [ + { field: "title", title: "Title", width: 300, visible: true }, + { field: "noteId", title: "Note ID", width: 200, visible: true } + ]; + const restored = restoreExistingData(newDefs, oldDefs); + expect(restored[0].width).toBe(300); + expect(restored[1].width).toBe(200); + }); + + it("restores order of columns", () => { + const newDefs: ColumnDefinition[] = [ + { field: "title", title: "Title", editor: "input" }, + { field: "noteId", title: "Note ID", visible: false } + ]; + const oldDefs: ColumnDefinition[] = [ + { field: "noteId", title: "Note ID", width: 200, visible: true }, + { field: "title", title: "Title", width: 300, visible: true } + ]; + const restored = restoreExistingData(newDefs, oldDefs); + expect(restored[0].field).toBe("noteId"); + expect(restored[1].field).toBe("title"); + }); + + it("inserts new columns at given position", () => { + const newDefs: ColumnDefinition[] = [ + { field: "title", title: "Title", editor: "input" }, + { field: "noteId", title: "Note ID", visible: false }, + { field: "newColumn", title: "New Column", editor: "input" } + ]; + const oldDefs: ColumnDefinition[] = [ + { field: "title", title: "Title", width: 300, visible: true }, + { field: "noteId", title: "Note ID", width: 200, visible: true } + ]; + const restored = restoreExistingData(newDefs, oldDefs, 0); + expect(restored[0].field).toBe("newColumn"); + expect(restored[1].field).toBe("title"); + expect(restored[2].field).toBe("noteId"); + }); +}); diff --git a/apps/client/src/widgets/view_widgets/table_view/columns.ts b/apps/client/src/widgets/view_widgets/table_view/columns.ts index ad1fa334f..d70cd8aca 100644 --- a/apps/client/src/widgets/view_widgets/table_view/columns.ts +++ b/apps/client/src/widgets/view_widgets/table_view/columns.ts @@ -92,14 +92,15 @@ export function buildColumnDefinitions(info: AttributeDefinitionInformation[], m return columnDefs; } -function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinition[], position?: number) { +export function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinition[], position?: number) { + const existingColumns: ColumnDefinition[] = [] const byField = new Map; for (const def of oldDefs) { byField.set(def.field ?? "", def); + existingColumns.push(def); } const newColumns: ColumnDefinition[] = []; - const existingColumns: ColumnDefinition[] = [] for (const newDef of newDefs) { const oldDef = byField.get(newDef.field ?? ""); if (!oldDef) { @@ -107,7 +108,6 @@ function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinit } else { newDef.width = oldDef.width; newDef.visible = oldDef.visible; - existingColumns.push(newDef); } }