Skip to content

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

tests: add converter tests

thecodrrf0c912baa2ed
diff --git a/packages/core/__tests__/convert.test.js b/packages/core/__tests__/convert.test.js
new file mode 100644
index 000000000..4f9c2a5f5
--- /dev/null
+++ b/packages/core/__tests__/convert.test.js
@@ -0,0 +1,56 @@
+import Convert from "../utils/convert";
+
+test("object to string", () => {
+ let objStr = Convert.toString({ hello: "world" });
+ expect(objStr).toStrictEqual(`{"hello":"world"}`);
+});
+
+test("array to string", () => {
+ let arrStr = Convert.toString(["hello", "world", "what"]);
+ expect(arrStr).toStrictEqual(`["hello","world","what"]`);
+});
+
+test("number to string", () => {
+ let numStr = Convert.toString(2010);
+ expect(numStr).toBe("2010");
+});
+
+test("bool to string", () => {
+ let boolStr = Convert.toString(true);
+ expect(boolStr).toBe("true");
+});
+
+test("null conversion", () => {
+ expect(Convert.toString(undefined)).toBeUndefined();
+});
+
+//fromString
+test("string to bool", () => {
+ expect(Convert.fromString("true")).toBe(true);
+ expect(Convert.fromString("false")).toBe(false);
+});
+
+test("string to int", () => {
+ expect(Convert.fromString("2010")).toBe(2010);
+});
+
+test("string to float", () => {
+ expect(Convert.fromString("20.2")).toBe(20.2);
+});
+
+test("string to object", () => {
+ let objStr = JSON.stringify({ hello: "world" });
+ expect(Convert.fromString(objStr)).toStrictEqual({ hello: "world" });
+});
+
+test("string to array", () => {
+ expect(Convert.fromString('["hello","world","what"]')).toStrictEqual([
+ "hello",
+ "world",
+ "what"
+ ]);
+});
+
+test("string to string", () => {
+ expect(Convert.fromString("hello")).toBe("hello");
+});
diff --git a/packages/core/__tests__/database.test.js b/packages/core/__tests__/database.test.js
index 2a55ff913..1c2ebd420 100644
--- a/packages/core/__tests__/database.test.js
+++ b/packages/core/__tests__/database.test.js
@@ -10,13 +10,21 @@ const TEST_NOTE = {
const TEST_NOTEBOOK = {
title: "Test Notebook",
- description: "Test Description"
+ description: "Test Description",
+ topics: ["hello"]
};
test("storage is defined", () => {
expect(db.storage).toBeDefined();
});
+test("add undefined note", async () => {
+ let timestamp = await db.addNote();
+ expect(timestamp).toBeUndefined();
+ timestamp = await db.addNote({});
+ expect(timestamp).toBeUndefined();
+});
+
test("add a note", async () => {
let timestamp = await db.addNote(TEST_NOTE);
expect(timestamp).toBeGreaterThan(0);
@@ -110,3 +118,5 @@ test("delete notebook", async () => {
let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);
expect(notebook).toBeUndefined();
});
+
+test("delete unknown key", async () => {});
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
index 344d62739..e20e28510 100644
--- a/packages/core/api/database.js
+++ b/packages/core/api/database.js
@@ -32,8 +32,7 @@ class Database {
* @param {object} note The note to add or update
*/
async addNote(note) {
- if (!note || !note.content || (!note.title && !note.content))
- return undefined; //TODO add test
+ if (!note || (!note.title && !note.content)) return undefined;
let timestamp = note.dateCreated || Date.now();
//add or update a note into the database
@@ -65,7 +64,7 @@ class Database {
* @param {array} notes the notes to be deleted
*/
async deleteNotes(notes) {
- this.delete(notes, KEYS.notes);
+ await deleteItems.call(this, notes, KEYS.notes);
}
/**
@@ -73,7 +72,7 @@ class Database {
* @param {string} id the id of the note (must be a timestamp)
*/
getNote(id) {
- return this.getItem(id, KEYS.notes);
+ return getItem.call(this, id, KEYS.notes);
}
/**
@@ -108,7 +107,6 @@ class Database {
const id = notebook.dateCreated || Date.now();
let topics = {};
if (notebook.topics) {
- //TODO add test
for (let topic of notebook.topics) {
topics[topic] = [];
}
@@ -134,7 +132,8 @@ class Database {
* @param {string} topic The topic to add
*/
addTopicToNotebook(notebookId, topic) {
- return this.notebookTopicFn(
+ return notebookTopicFn.call(
+ this,
notebookId,
topic,
notebook => ((notebook.topics[topic] = []), true)
@@ -147,7 +146,7 @@ class Database {
* @param {string} topic The topic to delete
*/
deleteTopicFromNotebook(notebookId, topic) {
- return this.notebookTopicFn(notebookId, topic, notebook => {
+ return notebookTopicFn.call(this, notebookId, topic, notebook => {
if (!notebook.topics[topic]) return false;
delete notebook.topics[topic];
return true;
@@ -161,7 +160,7 @@ class Database {
* @param {number} noteId The ID of the note
*/
addNoteToTopic(notebookId, topic, noteId) {
- return this.notebookTopicFn(notebookId, topic, notebook => {
+ return notebookTopicFn.call(this, notebookId, topic, notebook => {
if (!notebook.topics.hasOwnProperty(topic)) return false;
let nbTopic = notebook.topics[topic];
notebook.topics[topic][nbTopic.length] = noteId;
@@ -176,7 +175,7 @@ class Database {
* @param {number} noteId The ID of the note
*/
deleteNoteFromTopic(notebookId, topic, noteId) {
- return this.notebookTopicFn(notebookId, topic, notebook => {
+ return notebookTopicFn.call(this, notebookId, topic, notebook => {
if (!notebook.topics[topic]) return false;
let nbTopic = notebook.topics[topic];
let index = nbTopic.indexOf(noteId);
@@ -207,7 +206,7 @@ class Database {
* @returns The notebook
*/
getNotebook(id) {
- return this.getItem(id, KEYS.notebooks);
+ return getItem.call(this, id, KEYS.notebooks);
}
/**
@@ -215,36 +214,36 @@ class Database {
* @param {array} notebooks The notebooks to delete
*/
async deleteNotebooks(notebooks) {
- await this.delete(notebooks, KEYS.notebooks);
+ await deleteItems.call(this, notebooks, KEYS.notebooks);
}
+}
- notebookTopicFn(notebookId, topic, fn) {
- if (!notebookId || !topic || !this.notebooks[notebookId]) return;
- let notebook = this.notebooks[notebookId];
- if (fn(notebook)) {
- this.notes[notebookId] = notebook;
- return this.storage.write(KEYS.notebooks, this.notebooks);
- }
- //TODO add test
- return Promise.resolve();
- }
+export default Database;
- getItem(id, key) {
- if (this[key].hasOwnProperty(id)) {
- return this[key][id];
+async function deleteItems(items, key) {
+ if (!items || items.length <= 0 || !this[key] || this[key].length <= 0)
+ return; //TODO add test
+ for (let item of items) {
+ if (this[key].hasOwnProperty(item.dateCreated)) {
+ delete this[key][item.dateCreated];
}
}
+ await this.storage.write(key, this[key]);
+}
- async delete(items, key) {
- if (!items || items.length <= 0 || !this[key] || this[key].length <= 0)
- return; //TODO add test
- for (let item of items) {
- if (this[key].hasOwnProperty(item.dateCreated)) {
- delete this[key][item.dateCreated];
- }
- }
- await this.storage.write(key, this[key]);
+function notebookTopicFn(notebookId, topic, fn) {
+ if (!notebookId || !topic || !this.notebooks[notebookId]) return;
+ let notebook = this.notebooks[notebookId];
+ if (fn(notebook)) {
+ this.notes[notebookId] = notebook;
+ return this.storage.write(KEYS.notebooks, this.notebooks);
}
+ //TODO add test
+ return Promise.resolve();
}
-export default Database;
+function getItem(id, key) {
+ if (this[key].hasOwnProperty(id)) {
+ return this[key][id];
+ }
+}
diff --git a/packages/core/utils/convert.js b/packages/core/utils/convert.js
index 2ece7d329..74aa8b022 100644
--- a/packages/core/utils/convert.js
+++ b/packages/core/utils/convert.js
@@ -1,25 +1,19 @@
-//TODO add tests
export default class Convert {
static toString(input) {
- try {
- let type = typeof input;
- if (type === "object") return JSON.stringify(input);
- return input.toString();
- } catch (error) {
- return input.toString();
- }
+ if (!input) return;
+ let type = typeof input;
+ if (type === "object") return JSON.stringify(input);
+ return input.toString();
}
static fromString(input) {
- try {
- let firstChar = input[0];
- if (firstChar === "[" || firstChar === "{") return JSON.parse(input);
- if (!isNaN(parseInt(input))) return parseInt(input);
- if (!isNaN(parseFloat(input))) return parseFloat(input);
- if (parseBoolean(input) !== undefined) return parseBoolean(input);
- return input;
- } catch (error) {
- return input;
- }
+ if (!input) return;
+ let firstChar = input[0];
+ if (firstChar == "[" || firstChar == "{") return JSON.parse(input);
+ if (!isNaN(parseFloat(input)) && input.includes("."))
+ return parseFloat(input);
+ if (!isNaN(parseInt(input))) return parseInt(input);
+ if (parseBoolean(input) !== undefined) return parseBoolean(input);
+ return input;
}
}
function parseBoolean(value) {
diff --git a/packages/core/utils/index.js b/packages/core/utils/index.js
index 2d683cc29..4ce3bbb58 100644
--- a/packages/core/utils/index.js
+++ b/packages/core/utils/index.js
@@ -1,6 +1,6 @@
export function extractValues(obj) {
const t = [];
- for (var key in obj) {
+ for (let key in obj) {
t[t.length] = obj[key];
}
return t;