Permalink
Browse files

Clear cmd.

  • Loading branch information...
vesln committed Jan 29, 2012
1 parent 78cab5a commit 9e23dfad93d78af86721b05bc77ce6a5e1a8316c
Showing with 37 additions and 1 deletion.
  1. +3 −0 lib/cli.js
  2. +17 −1 lib/commands.js
  3. +1 −0 test/cli.test.js
  4. +16 −0 test/commands.test.js
View
@@ -25,6 +25,9 @@ app.cmd(/version/, commands.version);
// Lists all todo items.
app.cmd(/ls/, commands.list);
+// Clears the whole todo.
+app.cmd(/clear/, commands.clear);
+
// Marks a todo item as done.
app.cmd(/check (.+)/, commands.check);
View
@@ -122,7 +122,23 @@ commands.delete = function(num) {
num = +num - 1;
storage.get('items', function(err, items) {
items || (items = []);
- items = items.splice(num, 1);
+ items.splice(num, 1);
+ storage.set('items', items, function() {
+ storage.save(function(err) {
+ if (err) throw err;
+ });
+ });
+ });
+};
+
+/**
+ * Clears the whole todo item.
+ *
+ * @param {String} Todo item number.
+ * @api public
+ */
+commands.clear = function(num) {
+ storage.set('items', [], function(err, items) {
storage.save(function(err) {
if (err) throw err;
});
View
@@ -32,6 +32,7 @@ describe('cli', function() {
it('should register routes', function() {
cli.router.routes.version.on.should.eql(commands.version);
cli.router.routes.ls.on.should.eql(commands.list);
+ cli.router.routes.clear.on.should.eql(commands.clear);
cli.router.routes.rm['(.+)'].on.should.eql(commands.delete);
cli.router.routes.check['(.+)'].on.should.eql(commands.check);
cli.router.routes.undo['(.+)'].on.should.eql(commands.undo);
View
@@ -134,4 +134,20 @@ describe('commands', function() {
commands.add('Foo bar');
});
});
+
+ describe('.clear()', function(done) {
+ it('should clear the todo', function(done) {
+ sinon.stub(storage, 'set', function(key, value, cb) {
+ key.should.eql('items');
+ value.should.eql([]);
+ cb();
+ });
+ sinon.stub(storage, 'save', function(key, cb) {
+ storage.set.restore();
+ storage.save.restore();
+ done();
+ });
+ commands.clear();
+ });
+ });
});

0 comments on commit 9e23dfa

Please sign in to comment.