Skip to content

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

feat: add tests for all database methods

thecodrrfd7b50b5c831
diff --git a/packages/core/__tests__/database.test.js b/packages/core/__tests__/database.test.js
index b9761474a..2a55ff913 100644
--- a/packages/core/__tests__/database.test.js
+++ b/packages/core/__tests__/database.test.js
@@ -8,52 +8,105 @@ const TEST_NOTE = {
content: { delta: "I am a delta", text: "I am a text" }
};
+const TEST_NOTEBOOK = {
+ title: "Test Notebook",
+ description: "Test Description"
+};
+
test("storage is defined", () => {
expect(db.storage).toBeDefined();
});
test("add a note", async () => {
let timestamp = await db.addNote(TEST_NOTE);
- expect(timestamp).toBeGreaterThan(1574880904538);
+ expect(timestamp).toBeGreaterThan(0);
+ TEST_NOTE["dateCreated"] = timestamp;
});
-test("save and get note", async () => {
- let timestamp = await db.addNote(TEST_NOTE);
- expect(timestamp).toBeGreaterThan(1574880904538);
- let note = db.getNote(timestamp);
+test("get all notes", () => {
+ let notes = db.getNotes();
+ expect(notes.length).toBeGreaterThan(0);
+});
+
+test("search all notes", () => {
+ let filtered = db.searchNotes("text");
+ expect(filtered.length).toBeGreaterThan(0);
+});
+
+test("get a note", () => {
+ let note = db.getNote(TEST_NOTE.dateCreated);
expect(note.title).toBe("I am a title");
});
-test("add and update note", async () => {
- let timestamp = await db.addNote(TEST_NOTE);
- expect(timestamp).toBeGreaterThan(1574880904538);
+test("update note", async () => {
let updateTimestamp = await db.addNote({
...TEST_NOTE,
- title: "I am a new title",
- dateCreated: timestamp
+ title: "I am a new title"
});
- expect(updateTimestamp).toBe(timestamp);
- let note = db.getNote(timestamp);
- let newNote = db.getNote(updateTimestamp);
- expect(note.title).toBe("I am a new title");
- expect(newNote.title).toBe("I am a new title");
+ expect(updateTimestamp).toBe(TEST_NOTE.dateCreated);
});
-test("delete note", async () => {
- let timestamp = await db.addNote(TEST_NOTE);
- let note = db.getNote(timestamp);
- expect(note.title).toBe("I am a title");
- await db.deleteNotes([note]);
- note = db.getNote(timestamp);
- expect(note).toBeUndefined();
+test("add a notebook", async () => {
+ let id = await db.addNotebook(TEST_NOTEBOOK);
+ TEST_NOTEBOOK["dateCreated"] = id;
+ expect(id).toBeGreaterThan(0);
+});
+
+test("get all notebooks", () => {
+ let notebooks = db.getNotebooks();
+ expect(notebooks.length).toBeGreaterThan(0);
});
-test("get all notes", async () => {
- let notes = await db.getNotes();
+test("get a notebook", () => {
+ let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);
+ expect(notebook).toBeDefined();
+});
+
+test("add topic to notebook", async () => {
+ await db.addTopicToNotebook(TEST_NOTEBOOK.dateCreated, "Home");
+ let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);
+ expect(notebook.topics.hasOwnProperty("Home")).toBe(true);
+});
+
+test("add note to topic", async () => {
+ await db.addNoteToTopic(
+ TEST_NOTEBOOK.dateCreated,
+ "Home",
+ TEST_NOTE.dateCreated
+ );
+ let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);
+ expect(notebook.topics["Home"][0]).toBeGreaterThan(0);
+});
+
+test("get topic", () => {
+ let notes = db.getTopic(TEST_NOTEBOOK.dateCreated, "Home");
expect(notes.length).toBeGreaterThan(0);
});
-test("search all notes", async () => {
- let filtered = await db.searchNotes("text");
- expect(filtered.length).toBeGreaterThan(0);
+test("delete note from topic", async () => {
+ await db.deleteNoteFromTopic(
+ TEST_NOTEBOOK.dateCreated,
+ "Home",
+ TEST_NOTE.dateCreated
+ );
+ let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);
+ expect(notebook.topics["Home"][0]).toBeUndefined();
+});
+
+test("delete topic from notebook", async () => {
+ await db.deleteTopicFromNotebook(TEST_NOTEBOOK.dateCreated, "Home");
+ let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);
+ expect(notebook.topics.hasOwnProperty("Home")).toBe(false);
+});
+
+test("delete note", async () => {
+ await db.deleteNotes([TEST_NOTE]);
+ let note = db.getNote(TEST_NOTE.dateCreated);
+ expect(note).toBeUndefined();
+});
+
+test("delete notebook", async () => {
+ await db.deleteNotebooks([TEST_NOTEBOOK]);
+ let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);
+ expect(notebook).toBeUndefined();
});
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
index af9581ac2..40f03981c 100644
--- a/packages/core/api/database.js
+++ b/packages/core/api/database.js
@@ -23,7 +23,7 @@ class Database {
/**
* Get all notes
*/
- async getNotes() {
+ getNotes() {
return extractValues(this.notes);
}
@@ -81,7 +81,7 @@ class Database {
* @param {string} query the search query
* @returns An array containing the filtered notes
*/
- async searchNotes(query) {
+ searchNotes(query) {
if (!query) return [];
return ff(
extractValues(this.notes),
@@ -94,7 +94,7 @@ class Database {
* Get all notebooks
* @returns An array containing all the notebooks
*/
- async getNotebooks() {
+ getNotebooks() {
return extractValues(this.notebooks);
}
@@ -107,13 +107,15 @@ class Database {
if (!notebook || !notebook.title) return;
const id = notebook.dateCreated || Date.now();
let topics = {};
- for (let topic of notebook.topics) {
- topics[topic] = [];
+ if (notebook.topics) {
+ for (let topic of notebook.topics) {
+ topics[topic] = [];
+ }
}
this.notebooks[id] = {
title: notebook.title,
description: notebook.description,
- dateCreated: notebook.dateCreated,
+ dateCreated: id,
pinned: notebook.pinned || false,
favorite: notebook.favorite || false,
topics,
@@ -134,7 +136,7 @@ class Database {
return this.notebookTopicFn(
notebookId,
topic,
- notebook => (notebook.topics[topic] = [])
+ notebook => ((notebook.topics[topic] = []), true)
);
}
@@ -144,11 +146,11 @@ class Database {
* @param {string} topic The topic to delete
*/
deleteTopicFromNotebook(notebookId, topic) {
- return this.notebookTopicFn(
- notebookId,
- topic,
- notebook => delete notebook.topics[topic]
- );
+ return this.notebookTopicFn(notebookId, topic, notebook => {
+ if (!notebook.topics[topic]) return false;
+ delete notebook.topics[topic];
+ return true;
+ });
}
/**
@@ -159,8 +161,10 @@ class Database {
*/
addNoteToTopic(notebookId, topic, noteId) {
return this.notebookTopicFn(notebookId, topic, notebook => {
- let topic = notebook.topics[topic];
- topic[topic.length] = noteId;
+ if (!notebook.topics.hasOwnProperty(topic)) return false;
+ let nbTopic = notebook.topics[topic];
+ notebook.topics[topic][nbTopic.length] = noteId;
+ return true;
});
}
@@ -172,10 +176,12 @@ class Database {
*/
deleteNoteFromTopic(notebookId, topic, noteId) {
return this.notebookTopicFn(notebookId, topic, notebook => {
- let topic = notebook.topics[topic];
- let index = topic.indexOf(noteId);
+ if (!notebook.topics[topic]) return false;
+ let nbTopic = notebook.topics[topic];
+ let index = nbTopic.indexOf(noteId);
if (index <= -1) return;
- topic.splice(index, 1);
+ notebook.topics[topic].splice(index, 1);
+ return true;
});
}
@@ -211,13 +217,14 @@ class Database {
await this.delete(notebooks, KEYS.notebooks);
}
- async notebookTopicFn(notebookId, topic, fn) {
+ notebookTopicFn(notebookId, topic, fn) {
if (!notebookId || !topic || !this.notebooks[notebookId]) return;
let notebook = this.notebooks[notebookId];
- if (!notebook.topics[topic]) return;
- fn(notebook);
- this.notes[notebookId] = notebook;
- await this.storage.write(KEYS.notebooks, this.notebooks);
+ if (fn(notebook)) {
+ this.notes[notebookId] = notebook;
+ return this.storage.write(KEYS.notebooks, this.notebooks);
+ }
+ return Promise.resolve();
}
getItem(id, key) {
diff --git a/packages/core/package.json b/packages/core/package.json
index 6e0b9c11c..4f2498e65 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -14,7 +14,7 @@
"jest": "^24.9.0"
},
"scripts": {
- "test": "jest --coverage"
+ "test": "jest"
},
"dependencies": {
"fast-filter": "^1.0.4",