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
refactor(client): await the removal inside removeOwnedLabelByName
Elian Doranae6dcfc2ccb3
diff --git a/apps/client/src/services/attributes.spec.ts b/apps/client/src/services/attributes.spec.tsindex 71179bd346..f4b0d0147c 100644--- a/apps/client/src/services/attributes.spec.ts+++ b/apps/client/src/services/attributes.spec.ts@@ -360,29 +360,31 @@ describe("removeAttributeById / removeOwnedAttributesByNameOrType", () => {describe("removeOwnedLabelByName / removeOwnedRelationByName", () => {beforeEach(() => vi.clearAllMocks());- it("removeOwnedLabelByName removes an existing label and returns true", () => {+ it("removeOwnedLabelByName removes an existing label and returns true", async () => {const note = buildNote({ title: "N", "#color": "red" });const labelId = note.getOwnedLabel("color")!.attributeId;- expect(attributeService.removeOwnedLabelByName(note, "color")).toBe(true);+ await expect(attributeService.removeOwnedLabelByName(note, "color")).resolves.toBe(true);expect(server.remove).toHaveBeenCalledWith(`notes/${note.noteId}/attributes/${labelId}`);});- it("removeOwnedLabelByName returns false when label is absent", () => {+ it("removeOwnedLabelByName returns false when label is absent", async () => {const note = buildNote({ title: "N" });- expect(attributeService.removeOwnedLabelByName(note, "missing")).toBe(false);+ await expect(attributeService.removeOwnedLabelByName(note, "missing")).resolves.toBe(false);expect(server.remove).not.toHaveBeenCalled();});- it("removeOwnedRelationByName removes an existing relation and returns true", () => {+ it("removeOwnedRelationByName removes an existing relation and returns true", async () => {const note = buildNote({ title: "N", "~template": "tpl1" });const relId = note.getOwnedRelation("template")!.attributeId;- expect(attributeService.removeOwnedRelationByName(note, "template")).toBe(true);+ await expect(attributeService.removeOwnedRelationByName(note, "template"))+ .resolves.toBe(true);expect(server.remove).toHaveBeenCalledWith(`notes/${note.noteId}/attributes/${relId}`);});- it("removeOwnedRelationByName returns false when relation is absent", () => {+ it("removeOwnedRelationByName returns false when relation is absent", async () => {const note = buildNote({ title: "N" });- expect(attributeService.removeOwnedRelationByName(note, "missing")).toBe(false);+ await expect(attributeService.removeOwnedRelationByName(note, "missing"))+ .resolves.toBe(false);expect(server.remove).not.toHaveBeenCalled();});});diff --git a/apps/client/src/services/attributes.ts b/apps/client/src/services/attributes.tsindex 33b1c6f477..64a3bea2f7 100644--- a/apps/client/src/services/attributes.ts+++ b/apps/client/src/services/attributes.ts@@ -59,10 +59,7 @@ export async function setBooleanWithInheritance(note: FNote, labelName: string,} else if (value) {await setLabel(note.noteId, labelName, "");} else {- const label = note.getOwnedLabel(labelName);- if (label) {- await removeAttributeById(note.noteId, label.attributeId);- }+ await removeOwnedLabelByName(note, labelName);}}@@ -86,10 +83,10 @@ export async function removeOwnedAttributesByNameOrType(note: FNote, type: Attri* @param labelName the name of the label to remove.* @returns `true` if an attribute was identified and removed, `false` otherwise.*/-function removeOwnedLabelByName(note: FNote, labelName: string) {+async function removeOwnedLabelByName(note: FNote, labelName: string) {const label = note.getOwnedLabel(labelName);if (label) {- removeAttributeById(note.noteId, label.attributeId);+ await removeAttributeById(note.noteId, label.attributeId);return true;}return false;@@ -103,10 +100,10 @@ function removeOwnedLabelByName(note: FNote, labelName: string) {* @param relationName the name of the relation to remove.* @returns `true` if an attribute was identified and removed, `false` otherwise.*/-function removeOwnedRelationByName(note: FNote, relationName: string) {+async function removeOwnedRelationByName(note: FNote, relationName: string) {const relation = note.getOwnedRelation(relationName);if (relation) {- removeAttributeById(note.noteId, relation.attributeId);+ await removeAttributeById(note.noteId, relation.attributeId);return true;}return false;diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 768f01a5d5..82b12a3515 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -846,7 +846,7 @@ describe("BoardApi card operations", () => {it("takes a card off the board by removing the value it is grouped by", async () => {const { api, items } = createBoardWithCards();- const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);await api.removeFromBoard([ items[0].note.noteId ]);expect(removeLabel).toHaveBeenCalledWith(items[0].note, "status");@@ -868,7 +868,7 @@ describe("BoardApi card operations", () => {});const [ cardId ] = [ ...board.getChildNoteIds() ];const { api } = createApi({}, [], board);- const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined as never);await api.removeFromBoard([ cardId ]);@@ -881,7 +881,7 @@ describe("BoardApi card operations", () => {const board = buildNote({ title: "Board", children: [ { title: "Card" } ] });const { api } = createApi({}, [], board, "~status");const removeRelation = vi.spyOn(attributes, "removeOwnedRelationByName")- .mockReturnValue(true);+ .mockResolvedValue(true);await api.removeFromBoard([ board.getChildNoteIds()[0] ]);expect(removeRelation).toHaveBeenCalledWith(expect.anything(), "status");@@ -899,7 +899,7 @@ describe("BoardApi card operations", () => {});const { api } = createApi({}, [], board, "~status");const removeRelation = vi.spyOn(attributes, "removeOwnedRelationByName")- .mockReturnValue(true);+ .mockResolvedValue(true);const message = vi.spyOn(toast, "showMessage").mockReturnValue(undefined);await api.removeFromBoard([ board.getChildNoteIds()[0] ]);@@ -2037,7 +2037,7 @@ describe("filing a card under the inbox", () => {});const { api } = createApi({}, [], board, "~status");const removeRelation = vi.spyOn(attributes, "removeOwnedRelationByName")- .mockReturnValue(true);+ .mockResolvedValue(true);const message = vi.spyOn(toast, "showMessage").mockReturnValue(undefined);await api.changeColumn(board.getChildNoteIds()[0], "");@@ -2055,7 +2055,7 @@ describe("filing a card under the inbox", () => {});const { api } = createApi({}, [], board, "~status");const removeRelation = vi.spyOn(attributes, "removeOwnedRelationByName")- .mockReturnValue(true);+ .mockResolvedValue(true);await api.removeFromBoard([ board.getChildNoteIds()[0] ]);diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex f565a38be2..dad39c2f57 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -276,7 +276,7 @@ describe("Board keyboard", () => {it("strips the grouping label with Delete, leaving the note where it is", async () => {const board = await renderBoard();const removed = noteOf(board, "First");- const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);focusCard(board, 0, 0);press(board, "Delete");@@ -292,7 +292,7 @@ describe("Board keyboard", () => {*/it("leaves Delete unanswered while the inbox column is drawn", async () => {const board = await renderBoard(undefined, undefined, [], true);- const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);// The inbox is seeded at the front, so the first card stands in the column after it.focusCard(board, 1, 0);@@ -314,7 +314,7 @@ describe("Board keyboard", () => {it("deletes the note itself with Shift and Delete", async () => {const board = await renderBoard();- const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);focusCard(board, 0, 0);press(board, "Delete", { shiftKey: true });@@ -331,7 +331,7 @@ describe("Board keyboard", () => {*/it("hands focus to the card below the one that goes, before it goes", async () => {const board = await renderBoard();- vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);const removed = noteOf(board, "First");focusCard(board, 0, 0);@@ -345,7 +345,7 @@ describe("Board keyboard", () => {it("takes every picked-out card off the board, not only the focused one", async () => {const board = await renderBoard();- const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);const first = noteOf(board, "First");const third = noteOf(board, "Third");pick(board, 0, 0);@@ -381,7 +381,7 @@ describe("Board keyboard", () => {*/it("hands focus past the cards going with it", async () => {const board = await renderBoard();- vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);pick(board, 0, 0);pick(board, 0, 1);focusCard(board, 0, 0);@@ -395,7 +395,7 @@ describe("Board keyboard", () => {/** A card the reader is standing on but has not picked out goes on its own. */it("leaves a selection the focused card is no part of alone", async () => {const board = await renderBoard();- const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);const second = noteOf(board, "Second");pick(board, 0, 1);focusCard(board, 0, 0);@@ -410,7 +410,7 @@ describe("Board keyboard", () => {it("takes the whole column off the board from its header, asking first", async () => {const board = await renderBoard();- const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);const remove = vi.spyOn(BoardApi.prototype, "confirmAndRemoveColumn").mockResolvedValue(true);focusHeader(board, 0);@@ -634,7 +634,7 @@ describe("Board keyboard", () => {/** The inbox is named by the empty string, which is a column like any other to move to. */it("sends a card into the inbox column beside it", async () => {const board = await renderBoard(undefined, undefined, [], true);- const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);// The inbox is seeded at the front, so the first card is in the column after it.focusCard(board, 1, 0);