From 55b9f8b36d68a197317fb2dc9250e20cdd61a305 Mon Sep 17 00:00:00 2001 From: Awa Desmoline Date: Thu, 18 Aug 2016 17:41:24 +0100 Subject: [PATCH 1/3] Implemented tests --- tests.js | 126 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 12 deletions(-) diff --git a/tests.js b/tests.js index 12661b7..3b323c3 100644 --- a/tests.js +++ b/tests.js @@ -1,24 +1,126 @@ -'use strict' +// 'use strict' var chai = require('chai'); var assert = chai.assert; var NotesApplication = require('./lib/notesapplication.js'); -var Note = require('./lib/note.js'); +var Note = require('./lib/notes.js'); describe("Note creation works properly", function() { + + it("creates a note with parameters author and note_content", function() { + var note = new Note(); + assert(note.hasOwnProperty("author")) + assert(note.hasOwnProperty("note_content")) + + }) + it("assigns author based on the parameter supplied in the constructor", function() { - note = new Note("Hello world", "Chidiebere") - assert(note.author == "Chidiebere") + var note = new Note("Chidiebere", "Hello world"); + assert(note.author === "Chidiebere") + }) + + it("assigns note content based on the parameter supplied in the constructor", function() { + var note = new Note("Awa", "Welcom Awa"); + assert(note.note_content === "Welcom Awa"); }) }) -describe("Notes application increments number of notes as notes are added", function() { - it("increments the note list as notes are added", function() { - note = new Note("Hello world", "Chidiebere"); - noteapp = new NotesApplication("Chidiebere"); - assert(noteapp.notelist.length == 0) - noteapp.addNote(note) - assert(noteapp.notelist.length == 1) + +describe("Notes application works properly", function() { + + it("creates an empty list of notes when notes application is created", function() { + var noteapp = new NotesApplication(); + assert(noteapp.notesList) + }) + + // it("adds, retrieves, searches, lists, edits and deletes notes", function() { + // var noteapp = new NotesApplication(); + + // }) + + it("adds notes successfully", function() { + var note = new Note("Chidiebere", "Hello world"); + var noteapp = new NotesApplication(); + var len = noteapp.length + + noteapp.create(note) + var new_len = noteapp.length + + assert(new_len === len + 1); + assert(note in noteapp.notesList); + }) + + it("retrieves notes successfully", function() { + var note = new Note("Chidiebere", "Hello world"); + var noteapp = new NotesApplication(); + assert(noteapp.getNote(0) === note) + assert(noteapp.getNote(5) === 'string') + }) + + + it("searches for notes successfully", function() { + var note1 = new Note("Chidiebere", "Hello world"); + var noteapp = new NotesApplication(); + + assert(noteapp.searchNote("wo") === "0 Results Found") + + noteapp.create(note1); + assert(noteapp.searchNote("wo") === "1 Results Found"); + + var note2 = new Note("Awa", "Working with Functions"); + noteapp.create(note2); + + assert(noteapp.searchNote("or") === "2 Results Found"); + + assert(noteapp.searchNote("Awa") === "0 Results Found") + + + }) + + + it("returns a list of notes or an empty list if no list is present", function() { + var noteapp = new NotesApplication(); + assert(noteapp.listNote() === []) + + var note1 = new Note("Awa", "Hello world"); + var note2 = new Note("Awa", "Working with Functions"); + + + noteapp.create(note1); + noteapp.create(note2); + + assert(noteapp.listNote() === noteapp.notesList) + assert(noteapp.notesList.length === 2) }) -}) \ No newline at end of file + + + it("deletes note correctly", function() { + + var note = new Note("Chidiebere", "Hello world"); + var noteapp = new NotesApplication(); + + assert(noteapp.notesList.length === 0) + + noteapp.create(note); + assert(noteapp.notesList.length === 1) + + noteapp.deleteNote(0) + assert(noteapp.notesList.length === 0) + }) + + it("edits notes correctly", function() { + + var note = new Note("Awa", "Hello world"); + var noteapp = new NotesApplication(); + var testNote = noteapp.notesList[0]; + noteapp.create(note); + + assert(testNote === note) + + noteapp.editNote(0, "Going Deeper in Javascript") + assert(testNote !== note) + + }) +}) + From 695eae2351e2b8889ba48118e518a0051ddef886 Mon Sep 17 00:00:00 2001 From: Awa Desmoline Date: Thu, 18 Aug 2016 17:58:45 +0100 Subject: [PATCH 2/3] Created the application --- lib/notes.js | 3 +- lib/notesapplication.js | 138 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 139 insertions(+), 2 deletions(-) diff --git a/lib/notes.js b/lib/notes.js index 9ccc1a2..56dad38 100644 --- a/lib/notes.js +++ b/lib/notes.js @@ -1,5 +1,6 @@ // Note class module.exports = function() { - + this.author = author; + this.note_content = note_content; } \ No newline at end of file diff --git a/lib/notesapplication.js b/lib/notesapplication.js index 2b695f7..d687fc0 100644 --- a/lib/notesapplication.js +++ b/lib/notesapplication.js @@ -1,5 +1,141 @@ // NotesApplication class module.exports = function() { + /** + * NotesApplication class for managing authors' notes + * @constructor + */ + + + this.notesList = []; + + + /** + * Adds a note to the NotesApplication instance + * @param {Note} note A note object + */ + + create: function(note) { + if (note instanceof Note) { + this.notesList.push(note); + } else { + return "The Object supplied is not a note!" + } + } + + + /** + * Operates on an instance of NotesApplication + * Lists all the notes in the application + */ + listNotes: function() { + if (this.notesList.length < 1) { + return []; + } else { + for (var i = 0; i < this.notesList.length; i++) { + console.log("Note ID: " + i + "\n" + + "CONTENT: " + this.notesList[i].note_content + "\n" + + "By Author " + this.notesList[i].author + "\n") + } + return this.notesList; + } + + } + + + /** + * Operates on an instance of NotesApplication + * Fetches the note at + * @param {Number} note_id The index of the note within the list of notes + */ + getNote: function(note_id) { + var note = this.notesList[note_id] + if (note) + return note + return "No note with ID " + "'" + note_id + "'" + } + + + + /** + * Operates on an instance of NotesApplication + * Searches all notes that match a certain string + * @param {string} search_text The text to search for. + */ + searchNote: function(search_text) { + if (this.notesList.length < 1) { + return "O Results Found"; + } else { + var notesFound = [] + + for (var i = 0; i < this.notesList.length; i++) { + if (this.notesList[i].note_content.indexOf(search_text) > -1) + notesFound.push(this.notesList[i]) + } + + if (notesFound.length < 1) { + return "0 Results Found"; + } else { + for (var j = 0; j < notesFound.length; j++) { + console.log( "Showing results for search " + "'" + search_text + "'" + "\n" + + "Note ID: " + j + "\n" + + "CONTENT: " + this.notesList[j].note_content + "\n" + + "By Author " + this.notesList[j].author + "\n" ) + } + return notes.length + " Results Found" + } + } + } + + + /** + * Operates on an instance of NotesApplication + * Deletes the note at + * @param {Number} note_id The index of the note within the list of notes + */ + deleteNote: function(note_id) { + var note = this.notesList[note_id] + if (note) { + this.notesList.pop(note) + return "Note with ID of " + note_id + " succcessfully deleted!"; + } else { + return "Note with ID of " + note_id + " is not in the Application!"; + } + } + + + /** + * Operates on an instance of NotesApplication + * Edits the note at + * @param {Number} note_id The index of the note within the list of notes + * @param {string} new_content The new content to add + */ + editNote: function(note_id, new_content) { + var note = this.notesList[note_id] + if (note) { + note.note_content = new_content; + return "Note with ID of " + note_id + " succcessfully edited!" + } else { + return "Note with ID of " + note_id + " is not in the Application!" + } + } +} + + +// var note1 = new Note("Awa", "This is my first note"); +// var noteApp = new NotesApplication(); +// noteApp.create(note1); + +// console.log() + +// noteApp.editNote(0, "I just edited my first note") + + + +// console.log() + +// console.log(noteApp.searchNote("edited")); + + + -} \ No newline at end of file From 1b394edff43e66dba81270951357f441bd42d243 Mon Sep 17 00:00:00 2001 From: Awa Desmoline Date: Thu, 18 Aug 2016 22:32:19 +0100 Subject: [PATCH 3/3] All tests working --- lib/notes.js | 5 ++++- lib/notesapplication.js | 39 +++++++++----------------------- tests.js | 50 +++++++++++++++++++++-------------------- 3 files changed, 41 insertions(+), 53 deletions(-) diff --git a/lib/notes.js b/lib/notes.js index 56dad38..afab06c 100644 --- a/lib/notes.js +++ b/lib/notes.js @@ -1,6 +1,9 @@ // Note class +/** + * Note class for creating authors' notes + */ -module.exports = function() { +module.exports = function(author, note_content) { this.author = author; this.note_content = note_content; } \ No newline at end of file diff --git a/lib/notesapplication.js b/lib/notesapplication.js index d687fc0..796181b 100644 --- a/lib/notesapplication.js +++ b/lib/notesapplication.js @@ -1,11 +1,12 @@ // NotesApplication class -module.exports = function() { - /** +var Note = require("./notes.js") + +/** * NotesApplication class for managing authors' notes - * @constructor */ +module.exports = function() { this.notesList = []; @@ -14,8 +15,7 @@ module.exports = function() { * Adds a note to the NotesApplication instance * @param {Note} note A note object */ - - create: function(note) { + this.create = function(note) { if (note instanceof Note) { this.notesList.push(note); } else { @@ -28,7 +28,7 @@ module.exports = function() { * Operates on an instance of NotesApplication * Lists all the notes in the application */ - listNotes: function() { + this.listNotes = function() { if (this.notesList.length < 1) { return []; } else { @@ -48,7 +48,7 @@ module.exports = function() { * Fetches the note at * @param {Number} note_id The index of the note within the list of notes */ - getNote: function(note_id) { + this.getNote = function(note_id) { var note = this.notesList[note_id] if (note) return note @@ -62,7 +62,7 @@ module.exports = function() { * Searches all notes that match a certain string * @param {string} search_text The text to search for. */ - searchNote: function(search_text) { + this.searchNote = function(search_text) { if (this.notesList.length < 1) { return "O Results Found"; } else { @@ -82,7 +82,7 @@ module.exports = function() { + "CONTENT: " + this.notesList[j].note_content + "\n" + "By Author " + this.notesList[j].author + "\n" ) } - return notes.length + " Results Found" + return notesFound.length + " Results Found" } } } @@ -93,7 +93,7 @@ module.exports = function() { * Deletes the note at * @param {Number} note_id The index of the note within the list of notes */ - deleteNote: function(note_id) { + this.deleteNote =function(note_id) { var note = this.notesList[note_id] if (note) { this.notesList.pop(note) @@ -110,7 +110,7 @@ module.exports = function() { * @param {Number} note_id The index of the note within the list of notes * @param {string} new_content The new content to add */ - editNote: function(note_id, new_content) { + this.editNote = function(note_id, new_content) { var note = this.notesList[note_id] if (note) { note.note_content = new_content; @@ -122,20 +122,3 @@ module.exports = function() { } -// var note1 = new Note("Awa", "This is my first note"); -// var noteApp = new NotesApplication(); -// noteApp.create(note1); - -// console.log() - -// noteApp.editNote(0, "I just edited my first note") - - - -// console.log() - -// console.log(noteApp.searchNote("edited")); - - - - diff --git a/tests.js b/tests.js index 3b323c3..02b291c 100644 --- a/tests.js +++ b/tests.js @@ -8,8 +8,9 @@ var Note = require('./lib/notes.js'); describe("Note creation works properly", function() { + it("creates a note with parameters author and note_content", function() { - var note = new Note(); + var note = new Note("This is my first note"); assert(note.hasOwnProperty("author")) assert(note.hasOwnProperty("note_content")) @@ -26,7 +27,6 @@ describe("Note creation works properly", function() { }) }) - describe("Notes application works properly", function() { it("creates an empty list of notes when notes application is created", function() { @@ -34,28 +34,33 @@ describe("Notes application works properly", function() { assert(noteapp.notesList) }) - // it("adds, retrieves, searches, lists, edits and deletes notes", function() { - // var noteapp = new NotesApplication(); - - // }) + it("implements the create, getNote, searchNote, listNotes, editNote and deleteNote functions", function() { + var noteapp = new NotesApplication(); + assert(noteapp.hasOwnProperty("create")) + assert(noteapp.hasOwnProperty("getNote")) + assert(noteapp.hasOwnProperty("searchNote")) + assert(noteapp.hasOwnProperty("listNotes")) + assert(noteapp.hasOwnProperty("editNote")) + assert(noteapp.hasOwnProperty("deleteNote")) + }) it("adds notes successfully", function() { var note = new Note("Chidiebere", "Hello world"); var noteapp = new NotesApplication(); - var len = noteapp.length + var len = noteapp.notesList.length noteapp.create(note) - var new_len = noteapp.length - - assert(new_len === len + 1); - assert(note in noteapp.notesList); + var new_len = noteapp.notesList.length + assert(new_len === (len + 1)); + assert(note === noteapp.notesList[0]); }) it("retrieves notes successfully", function() { var note = new Note("Chidiebere", "Hello world"); var noteapp = new NotesApplication(); + noteapp.create(note) assert(noteapp.getNote(0) === note) - assert(noteapp.getNote(5) === 'string') + assert(noteapp.getNote(5) === "No note with ID " + "'" + 5 + "'") }) @@ -63,16 +68,15 @@ describe("Notes application works properly", function() { var note1 = new Note("Chidiebere", "Hello world"); var noteapp = new NotesApplication(); - assert(noteapp.searchNote("wo") === "0 Results Found") + assert(noteapp.searchNote("world") == "O Results Found") noteapp.create(note1); - assert(noteapp.searchNote("wo") === "1 Results Found"); + assert(noteapp.searchNote("world") == "1 Results Found"); - var note2 = new Note("Awa", "Working with Functions"); + var note2 = new Note("Awa", "working with Functions"); noteapp.create(note2); - assert(noteapp.searchNote("or") === "2 Results Found"); - + assert(noteapp.searchNote("wor") === "2 Results Found"); assert(noteapp.searchNote("Awa") === "0 Results Found") @@ -81,7 +85,7 @@ describe("Notes application works properly", function() { it("returns a list of notes or an empty list if no list is present", function() { var noteapp = new NotesApplication(); - assert(noteapp.listNote() === []) + assert(noteapp.listNotes().length === 0) var note1 = new Note("Awa", "Hello world"); var note2 = new Note("Awa", "Working with Functions"); @@ -90,13 +94,12 @@ describe("Notes application works properly", function() { noteapp.create(note1); noteapp.create(note2); - assert(noteapp.listNote() === noteapp.notesList) + assert(noteapp.listNotes() === noteapp.notesList) assert(noteapp.notesList.length === 2) }) it("deletes note correctly", function() { - var note = new Note("Chidiebere", "Hello world"); var noteapp = new NotesApplication(); @@ -110,16 +113,15 @@ describe("Notes application works properly", function() { }) it("edits notes correctly", function() { - var note = new Note("Awa", "Hello world"); var noteapp = new NotesApplication(); - var testNote = noteapp.notesList[0]; noteapp.create(note); - assert(testNote === note) + assert(noteapp.notesList[0] === note) noteapp.editNote(0, "Going Deeper in Javascript") - assert(testNote !== note) + + assert(noteapp.notesList[0].note_content === "Going Deeper in Javascript") }) })