From ca7cff45c976b88938e89d5fdff55b1f237775bb Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 28 Feb 2025 19:55:02 +0200 Subject: [PATCH] test(calendar): relation as custom title --- src/public/app/test/easy-froca.ts | 41 ++++++++++++++----- src/public/app/test/setup.ts | 6 +++ .../view_widgets/calendar_view.spec.ts | 13 ++++++ 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/public/app/test/easy-froca.ts b/src/public/app/test/easy-froca.ts index 7e214579c..48dd5f245 100644 --- a/src/public/app/test/easy-froca.ts +++ b/src/public/app/test/easy-froca.ts @@ -3,9 +3,10 @@ import FNote from "../entities/fnote.js"; import froca from "../services/froca.js"; import FAttribute from "../entities/fattribute.js"; -type AttributeDefinitions = { [key in `#${string}`]: string; } +type AttributeDefinitions = { [key in `#${string}`]: string; }; +type RelationDefinitions = { [key in `~${string}`]: string; }; -interface NoteDefinition extends AttributeDefinitions { +interface NoteDefinition extends AttributeDefinitions, RelationDefinitions { id?: string | undefined; title: string; } @@ -42,21 +43,41 @@ export function buildNotes(notes: NoteDefinition[]) { let position = 0; for (const [ key, value ] of Object.entries(noteDef)) { + const attributeId = utils.randomString(12); + const name = key.substring(1); + + let attribute: FAttribute | null = null; if (key.startsWith("#")) { - const attributeId = utils.randomString(12); - const attribute = new FAttribute(froca, { + attribute = new FAttribute(froca, { noteId: note.noteId, - attributeId: attributeId, + attributeId, type: "label", - name: key.substring(1), - value: value, - position: position, + name, + value, + position, isInheritable: false }); - froca.attributes[attributeId] = attribute; - note.attributes.push(attributeId); } + if (key.startsWith("~")) { + console.log("Created relation with ", name, value); + attribute = new FAttribute(froca, { + noteId: note.noteId, + attributeId, + type: "relation", + name, + value, + position, + isInheritable: false + }); + } + + if (!attribute) { + continue; + } + + froca.attributes[attributeId] = attribute; + note.attributes.push(attributeId); position++; } } diff --git a/src/public/app/test/setup.ts b/src/public/app/test/setup.ts index 47b827e0e..516553422 100644 --- a/src/public/app/test/setup.ts +++ b/src/public/app/test/setup.ts @@ -46,6 +46,12 @@ function mockServer() { attributes: [] } } + }, + + async post(url: string, data: {}) { + if (url === "tree/load") { + throw new Error(`A module tried to load from the server the following notes: ${data.noteIds.join(",")}\nThis is not supported, use Froca mocking instead and ensure the note exist in the mock.`) + } } } }; diff --git a/src/public/app/widgets/view_widgets/calendar_view.spec.ts b/src/public/app/widgets/view_widgets/calendar_view.spec.ts index e5b536a62..8c1be2961 100644 --- a/src/public/app/widgets/view_widgets/calendar_view.spec.ts +++ b/src/public/app/widgets/view_widgets/calendar_view.spec.ts @@ -85,4 +85,17 @@ describe("Building events", () => { expect(events[1]).toMatchObject({ title: "Note 2", start: "2025-05-07" }); }); + it("supports relation as custom title", async () => { + const noteIds = buildNotes([ + { id: "mySharedTitle", title: "My shared title" }, + { title: "Note 1", "~myTitle": "mySharedTitle", "#startDate": "2025-05-05", "#calendar:title": "~myTitle" }, + { title: "Note 2", "#startDate": "2025-05-07", "#calendar:title": "~myTitle" }, + ]); + const events = await CalendarView.buildEvents(noteIds); + + expect(events).toHaveLength(2); + expect(events[0]).toMatchObject({ title: "My shared title", start: "2025-05-05" }); + expect(events[1]).toMatchObject({ title: "Note 2", start: "2025-05-07" }); + }); + });