Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: notebook.totalNotes count incorrect

thecodrr5152fa97d0f4
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index 966df1a73..71a43360f 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -42,10 +42,14 @@ test("delete note", () =>
let topic = topics.topic("General");
await topic.add(id);
topic = topics.topic("General");
+
expect(topic.all.findIndex((v) => v.id === id)).toBeGreaterThan(-1);
await db.notes.delete(id);
expect(db.notes.note(id)).toBeUndefined();
expect(topic.all.findIndex((v) => v.id === id)).toBe(-1);
+
+ expect(db.notebooks.notebook(notebookId).data.totalNotes).toBe(0);
+ expect(topics.topic("General")._topic.totalNotes).toBe(0);
}));
test("get all notes", () =>
@@ -179,18 +183,25 @@ test("duplicate note to topic should not be added", () =>
expect(topic.all.length).toBe(1);
}));
-test("move note", () =>
+test("add the same note to 2 notebooks", () =>
noteTest().then(async ({ db, id }) => {
let notebookId = await db.notebooks.add({ title: "Hello" });
let topics = db.notebooks.notebook(notebookId).topics;
await topics.add("Home");
- let topic = topics.topic("Home");
- await topic.add(id);
+ let topic = topics.topic("Home")._topic;
+ await db.notes.move({ id: notebookId, topic: topic.id }, id);
+
+ expect(topics.topic(topic.id).has(id)).toBe(true);
+
let notebookId2 = await db.notebooks.add({ title: "Hello2" });
- await db.notebooks.notebook(notebookId2).topics.add("Home2");
- await db.notes.move({ id: notebookId2, topic: "Home2" }, id);
+ let topics2 = db.notebooks.notebook(notebookId2).topics;
+ await topics2.add("Home2");
+ let topic2 = topics2.topic("Home2")._topic;
+ await db.notes.move({ id: notebookId2, topic: topic2.id }, id);
+
let note = db.notes.note(id);
- expect(note.notebooks.some((n) => n.id === notebookId2)).toBe(true);
+ expect(note.notebooks.length).toBe(2);
+ expect(topics2.topic(topic2.id).has(id)).toBe(true);
}));
test("moving note to same notebook and topic should do nothing", () =>
diff --git a/packages/core/collections/topics.js b/packages/core/collections/topics.js
index e81fb8eae..615a9a5bc 100644
--- a/packages/core/collections/topics.js
+++ b/packages/core/collections/topics.js
@@ -47,16 +47,17 @@ export default class Topics {
notebook.topics = [];
notebook.totalNotes = 0;
- allTopics.forEach((t) => {
+ for (let t of allTopics) {
let topic = makeTopic(t, this._notebookId);
if (notebook.topics.findIndex((_topic) => _topic.title === t) > -1)
- return;
+ continue;
- if (topic.title.length <= 0) return;
+ if (topic.title.length <= 0) continue;
let index = notebook.topics.findIndex((t) => t.id === topic.id);
if (index > -1) {
+ notebook.totalNotes -= notebook.topics[index].totalNotes;
notebook.topics[index] = {
...notebook.topics[index],
...topic,
@@ -66,8 +67,7 @@ export default class Topics {
}
notebook.totalNotes += topic.totalNotes;
- });
-
+ }
return this._db.notebooks._collection.addItem(notebook);
}