Navigation Menu

Skip to content

Commit

Permalink
Ensure that Todo#id is always a number
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Jan 21, 2014
1 parent 0e2ece9 commit 62239d2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
71 changes: 58 additions & 13 deletions lib/todo.js
Expand Up @@ -40,23 +40,68 @@ Todo.create = function(arr) {
};

/**
* Change the todo status to "pending".
*
* @api public
* Prototype.
*/

Todo.prototype.undo = function() {
this.status = 'pending';
};
Todo.prototype = {

/**
* Change the todo status to "done".
*
* @api public
*/
/**
* ID getter.
*
* @returns {Number}
*/

get id() {
return this._id;
},

/**
* ID setter.
*
* @param {Number} id
* @api public
*/

set id(id) {
this._id = +id;
assert(!Number.isNaN(this._id), 'ID must be a number');
},

/**
* Change the todo status to "pending".
*
* @api public
*/

undo: function() {
this.status = 'pending';
},

/**
* Change the todo status to "done".
*
* @api public
*/

complete: function() {
this.status = 'done';
},

/**
* toJSON.
*
* @returns {Object}
* @api public
*/

Todo.prototype.complete = function() {
this.status = 'done';
toJSON: function() {
return {
id: this.id,
desc: this.desc,
status: this.status,
modified: this.modified,
};
},
};

/**
Expand Down
13 changes: 13 additions & 0 deletions test/todo.js
Expand Up @@ -23,6 +23,19 @@ describe(Todo, function() {
todo.modified.should.be.instanceOf(Date);
});

describe('#id', function() {
it('casts the id to number', function() {
var todo = new Todo('1', 'test');
todo.id.should.eq(1);
});

it('throws an error when id is NaN', function() {
should.throw(function() {
new Todo('id', 'desc');
});
});
});

describe('.create', function() {
it('returns an array of todo items', function() {
var attrs = [
Expand Down

0 comments on commit 62239d2

Please sign in to comment.