diff --git a/lib/notes.js b/lib/notes.js index 9ccc1a2..d1ac750 100644 --- a/lib/notes.js +++ b/lib/notes.js @@ -1,5 +1,7 @@ // Note class -module.exports = function() { +module.exports = function Notes(an_object) { + this.author = an_object.author; + this.note_content = an_object.note_content; +} -} \ No newline at end of file diff --git a/lib/notesapplication.js b/lib/notesapplication.js index 2b695f7..f6c518f 100644 --- a/lib/notesapplication.js +++ b/lib/notesapplication.js @@ -1,5 +1,46 @@ -// NotesApplication class +var Notes = require('./notes.js'); +module.exports = function NotesApplication() { + this.notes = []; + this.create_notes = function(note){ + if(note instanceof Notes) this.notes.push(note); + } + + this.get = function(note_id){ + if(this.notes.hasOwnProperty(note_id)){ //checks to see if the notes id is an index of + return this.notes[note_id].note_content; + }// end if + else console.log("ID is invalid"); + } + + this.edit = function(note_id, new_content){ + if(this.notes.hasOwnProperty(note_id)) this.notes[note_id].note_content = new_content; + else console.log("ID is invalid"); + } + + this.delete = function(note_id){ + if(this.notes.hasOwnProperty(note_id)) this.notes.splice(note_id,1); + else console.log("ID is invalid"); + } + + this.list_notes = function(){ + var hold_notes = []; + for(var count = 0; count < this.notes.length; count++){ + hold_notes.push(this.notes[count]); + console.log("Note ID: " + count); + console.log("[" + this.notes[count].note_content + "]"); + console.log("By Author [" + this.notes[count].author + "]"); + }// nd for + return hold_notes; + } + + this.search = function(search_text){ + search_results = []; + for (var count = 0; count < this.notes.length; count++){ + if (this.notes[count].note_content.indexOf(search_text) !== -1) + search_results.push[this.notes.note_content] + } + return search_results; + } +} -module.exports = function() { -} \ No newline at end of file diff --git a/notes_application_tests.js b/notes_application_tests.js new file mode 100644 index 0000000..c21d5df --- /dev/null +++ b/notes_application_tests.js @@ -0,0 +1,130 @@ +'use strict' + +var chai = require('chai'); +var assert = chai.assert; + +var NotesApplication = require('./lib/notesapplication.js'); +var Notes = require('./lib/notes.js'); + +describe("Check that creating a note object works properly", function(){ + it("should assign the note content based on the parameter supplied",function(){ + var person = { + author: "kachi", + note_content: "stuff" + }; + var note = new Notes(person); + assert(note.note_content == "stuff"); + }) + + it("should assign the author based on the parameter supplied",function(){ + var person = { + author: "kachi", + note_content: "stuff" + }; + var note = new Notes(person); + assert(note.author == "kachi"); + }) +}) + +describe("should test that create note function works", function(){ + it("it should check that a new entry was added", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var noteapp_object = new NotesApplication(); + assert(noteapp_object.notes.length === 0); + noteapp_object.create_notes(note); + assert(noteapp_object.notes.length === 1); + }) + + it("it should check if the parameter being supplied is an instance of the Notes class", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var noteapp_object = new NotesApplication(); + noteapp_object.create_notes(note); + assert.instanceOf(note, Notes); + }) +}) + +describe("it should check if you can get a note", function(){ + it("check if you can retrieve a note with an ID of 0", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var noteapp_object = new NotesApplication(); + noteapp_object.create_notes(note); + noteapp_object.get(0); + assert(noteapp_object.notes[0].note_content === "stuff"); + }) + + it("throws error when trying to get a note with an invalid id", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var noteapp_object = new NotesApplication(); + noteapp_object.create_notes(note); + noteapp_object.get(4.5); + assert("ID is invalid"); + }) + +}) + +describe("edit functionality works properly", function(){ + it("it should check that the content of a note at index 0 changes when edited", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var noteapp_object = new NotesApplication(); + + noteapp_object.create_notes(note); + noteapp_object.edit(0, "new stuff") + assert(noteapp_object.notes[0].note_content === "new stuff"); + }) + + it("throws an error when you try to edit a note with an invalid ID", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var noteapp_object = new NotesApplication(); + noteapp_object.create_notes(note); + noteapp_object.edit(0.1, "new stuff") + assert("ID is invalid") + }) +}) + +describe("that you can delete a note", function(){ + it("check that you can delete a note at index 1 ", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var note2 = new Notes({author: "kachi", note_content: "stuff2"}); + var noteapp_object = new NotesApplication(); + noteapp_object.create_notes(note); + noteapp_object.create_notes(note2); + assert(noteapp_object.notes.length === 2); + noteapp_object.delete(1); + assert(noteapp_object.notes.length == 1); + + }) + + it("throws an error when you try to delete a note at an invalid index", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var note2 = new Notes({author: "kachi", note_content: "stuff2"}); + var noteapp_object = new NotesApplication(); + noteapp_object.create_notes(note); + noteapp_object.create_notes(note2); + noteapp_object.delete(5.1); + assert("ID is invalid") + }) +}) + +describe("Check that the user can retrieve notes", function(){ + it("should check that the user can retrieve added notes", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var note1 = new Notes({author: "me", note_content: "more stuff"}); + var note2 = new Notes({author: "me", note_content: "more stuff"}); + var noteapp_object = new NotesApplication(); + noteapp_object.create_notes(note); + var hold_notes = noteapp_object.list_notes(); + assert.deepEqual([note], hold_notes); + }) +}) + +describe("Check that a user can search for text in notes", function(){ + it("should check that a user can search for specific text", function(){ + var note = new Notes({author: "kachi", note_content: "stuff"}); + var note1 = new Notes({author: "me", note_content: "more stuff"}); + var noteapp_object = new NotesApplication(); + noteapp_object.create_notes(note); + noteapp_object.create_notes(note1); + noteapp_object.search("s"); + assert(["stuff", "more stuff"]); + }) +}) \ No newline at end of file diff --git a/tests.js b/tests.js index 12661b7..abb8128 100644 --- a/tests.js +++ b/tests.js @@ -4,7 +4,7 @@ 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("assigns author based on the parameter supplied in the constructor", function() {