Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(client): attribute toggling
Elian Doran6398830c2d10
diff --git a/apps/client/src/services/attributes.spec.ts b/apps/client/src/services/attributes.spec.tsnew file mode 100644index 0000000000..cd52cb6d5f--- /dev/null+++ b/apps/client/src/services/attributes.spec.ts@@ -0,0 +1,113 @@+import { beforeEach, describe, expect, it, vi } from "vitest";++import { buildNote } from "../test/easy-froca";+import { setBooleanWithInheritance } from "./attributes";+import froca from "./froca";+import server from "./server.js";++// Spy on server methods to track calls+// @ts-expect-error the generic typing is causing issues here+server.put = vi.fn(async <T> (url: string, data?: T) => ({} as T));+// @ts-expect-error the generic typing is causing issues here+server.remove = vi.fn(async <T> (url: string) => ({} as T));++describe("Set boolean with inheritance", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it("doesn't call server if value matches directly", async () => {+ const noteWithLabel = buildNote({+ title: "New note",+ "#foo": ""+ });+ const noteWithoutLabel = buildNote({+ title: "New note"+ });++ await setBooleanWithInheritance(noteWithLabel, "foo", true);+ await setBooleanWithInheritance(noteWithoutLabel, "foo", false);+ expect(server.put).not.toHaveBeenCalled();+ expect(server.remove).not.toHaveBeenCalled();+ });++ it("sets boolean normally without inheritance", async () => {+ const standaloneNote = buildNote({+ title: "New note"+ });++ await setBooleanWithInheritance(standaloneNote, "foo", true);+ expect(server.put).toHaveBeenCalledWith(`notes/${standaloneNote.noteId}/set-attribute`, {+ type: "label",+ name: "foo",+ value: "",+ isInheritable: false+ });+ });++ it("removes boolean normally without inheritance", async () => {+ const standaloneNote = buildNote({+ title: "New note",+ "#foo": ""+ });++ const attributeId = standaloneNote.getLabel("foo")!.attributeId;+ await setBooleanWithInheritance(standaloneNote, "foo", false);+ expect(server.remove).toHaveBeenCalledWith(`notes/${standaloneNote.noteId}/attributes/${attributeId}`);+ });++ it("doesn't call server if value matches inherited", async () => {+ const parentNote = buildNote({+ title: "Parent note",+ "#foo(inheritable)": "",+ "children": [+ {+ title: "Child note"+ }+ ]+ });+ const childNote = froca.getNoteFromCache(parentNote.children[0])!;+ expect(childNote.isLabelTruthy("foo")).toBe(true);+ await setBooleanWithInheritance(childNote, "foo", true);+ expect(server.put).not.toHaveBeenCalled();+ expect(server.remove).not.toHaveBeenCalled();+ });++ it("overrides boolean with inheritance", async () => {+ const parentNote = buildNote({+ title: "Parent note",+ "#foo(inheritable)": "",+ "children": [+ {+ title: "Child note"+ }+ ]+ });+ const childNote = froca.getNoteFromCache(parentNote.children[0])!;+ expect(childNote.isLabelTruthy("foo")).toBe(true);+ await setBooleanWithInheritance(childNote, "foo", false);+ expect(server.put).toHaveBeenCalledWith(`notes/${childNote.noteId}/set-attribute`, {+ type: "label",+ name: "foo",+ value: "false",+ isInheritable: false+ });+ });++ it("deletes override boolean with inheritance", async () => {+ const parentNote = buildNote({+ title: "Parent note",+ "#foo(inheritable)": "",+ "children": [+ {+ title: "Child note",+ "#foo": "false",+ }+ ]+ });+ const childNote = froca.getNoteFromCache(parentNote.children[0])!;+ expect(childNote.isLabelTruthy("foo")).toBe(false);+ await setBooleanWithInheritance(childNote, "foo", true);+ expect(server.remove).toHaveBeenCalledWith(`notes/${childNote.noteId}/attributes/${childNote.getLabel("foo")!.attributeId}`);+ });+});diff --git a/apps/client/src/test/easy-froca.ts b/apps/client/src/test/easy-froca.tsindex 59d4c2e4a2..d3ae75718f 100644--- a/apps/client/src/test/easy-froca.ts+++ b/apps/client/src/test/easy-froca.ts@@ -69,24 +69,6 @@ export function buildNote(noteDef: NoteDefinition) {});note.getBlob = async () => blob;- // Manage children.- if (noteDef.children) {- for (const childDef of noteDef.children) {- const childNote = buildNote(childDef);- const branchId = `${note.noteId}_${childNote.noteId}`;- const branch = new FBranch(froca, {- branchId,- noteId: childNote.noteId,- parentNoteId: note.noteId,- notePosition: childNotePosition,- fromSearchNote: false- });- froca.branches[branchId] = branch;- note.addChild(childNote.noteId, branchId, false);- childNotePosition += 10;- }- }-let position = 0;for (const [ key, value ] of Object.entries(noteDef)) {const attributeId = utils.randomString(12);@@ -136,5 +118,25 @@ export function buildNote(noteDef: NoteDefinition) {}noteAttributeCache.attributes[note.noteId].push(attribute);}++ // Manage children.+ if (noteDef.children) {+ for (const childDef of noteDef.children) {+ const childNote = buildNote(childDef);+ const branchId = `${note.noteId}_${childNote.noteId}`;+ const branch = new FBranch(froca, {+ branchId,+ noteId: childNote.noteId,+ parentNoteId: note.noteId,+ notePosition: childNotePosition,+ fromSearchNote: false+ });+ froca.branches[branchId] = branch;+ note.addChild(childNote.noteId, branchId, false);+ childNote.addParent(note.noteId, branchId, false);+ childNotePosition += 10;+ }+ }+return note;}