test(calendar): relation as custom title

This commit is contained in:
Elian Doran 2025-02-28 19:55:02 +02:00
parent 6f2a0f9ee1
commit ca7cff45c9
No known key found for this signature in database
3 changed files with 50 additions and 10 deletions

View File

@ -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++;
}
}

View File

@ -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.`)
}
}
}
};

View File

@ -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" });
});
});