Permalink
Browse files

Use FakeStorage to test commands.js

  • Loading branch information...
1 parent 048b5bb commit 3dcd2b839ed374db5c5e50f103856a8bbb88ba0d @RStankov RStankov committed Feb 5, 2012
Showing with 78 additions and 137 deletions.
  1. +15 −15 lib/commands.js
  2. +63 −122 test/commands.test.js
View
@@ -27,11 +27,11 @@ commands.print = console.log;
var app = require('./app');
/**
- * Storage. Just an alias to application config.
+ * commands.storage. Just an alias to application config.
*
* @type {Object}
*/
-var storage = require('./storage');
+commands.storage = require('./storage');
/**
* Formatter.
@@ -61,7 +61,7 @@ commands.version = function() {
*/
commands.list = function() {
var out = [];
- storage.get('items', function(err, items) {
+ commands.storage.get('items', function(err, items) {
items || (items = []);
for (var i = -1, len = items.length; ++i < len;) {
if (!app.argv.all && items[i].done) continue;
@@ -105,12 +105,12 @@ commands.undo = function(num) {
* @api private
*/
commands.toggle = function(num, state) {
- storage.get('items', function(err, items) {
+ commands.storage.get('items', function(err, items) {
items || (items = []);
if (!items[num]) throw new Error('There is no todo item with number ' + num + 1);
items[num].done = state;
- storage.set('items', items, function() {
- storage.save(function(err) {
+ commands.storage.set('items', items, function() {
+ commands.storage.save(function(err) {
if (err) throw err;
});
});
@@ -125,11 +125,11 @@ commands.toggle = function(num, state) {
*/
commands.delete = function(num) {
num = +num - 1;
- storage.get('items', function(err, items) {
+ commands.storage.get('items', function(err, items) {
items || (items = []);
items.splice(num, 1);
- storage.set('items', items, function() {
- storage.save(function(err) {
+ commands.storage.set('items', items, function() {
+ commands.storage.save(function(err) {
if (err) throw err;
});
});
@@ -143,8 +143,8 @@ commands.delete = function(num) {
* @api public
*/
commands.clear = function(num) {
- storage.set('items', [], function(err, items) {
- storage.save(function(err) {
+ commands.storage.set('items', [], function(err, items) {
+ commands.storage.save(function(err) {
if (err) throw err;
});
});
@@ -157,11 +157,11 @@ commands.clear = function(num) {
* @api public
*/
commands.add = function(item) {
- storage.get('items', function(err, items) {
+ commands.storage.get('items', function(err, items) {
items || (items = []);
items.push({ text: item, done: false });
- storage.set('items', items, function(err) {
- storage.save(function(err) {
+ commands.storage.set('items', items, function(err) {
+ commands.storage.save(function(err) {
if (err) throw err;
});
});
@@ -180,7 +180,7 @@ commands.write = function(filename) {
var filename = "~/todo.txt";
}
var out = [];
- storage.get('items', function(err, items) {
+ commands.storage.get('items', function(err, items) {
items || (items = []);
for (var i = -1, len = items.length; ++i < len;) {
if (!app.argv.all && items[i].done) continue;
View
@@ -1,153 +1,94 @@
/*!
* todo - Todos in the CLI like what.
- *
+ *
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/
-
-/**
- * Module dependencies.
- */
-var sinon = require('sinon');
-
-/**
- * Support.
- */
-var storage = require('../lib/storage');
-/**
- * The tests object.
- *
- * @type {Object}
- */
-var commands = require('../lib/commands');
+var sinon = require('sinon'),
+ commands = require('../lib/commands'),
+ Storage = require('./support/fake_storage');
describe('commands', function() {
- describe('.version()', function() {
- it('should be sane', function() {
- commands.version.should.be.ok
+ var storage;
+
+ beforeEach(function() {
+ storage = commands.storage = new Storage();
+ });
+
+ describe('version', function() {
+ it('is sane', function() {
+ commands.version.should.be.ok;
});
});
-
- describe('.list()', function() {
- it('should return list items', function(done) {
- var i = 0;
- var expected = 3;
- var out = '';
-
- sinon.stub(storage, 'get', function(key, cb) {
- cb(null, [{text: 'Foo', done: false}]);
- });
-
+
+ describe('list', function() {
+ it('print list items', function(done) {
+ storage.items = [{text: 'Foo', done: false}];
+
+ var left = 3,
+ out = '';
+
sinon.stub(commands, 'print', function(text) {
out += text;
- if (++i === expected) finish();
+ if (--left == 0) {
+ out.should.eql(' #1 \u001b[31m✖\u001b[39m Foo');
+ commands.print.restore();
+ done();
+ }
});
-
- function finish() {
- out.should.eql(' #1 \u001b[31m✖\u001b[39m Foo');
- commands.print.restore();
- storage.get.restore();
- done();
- };
-
+
commands.list();
});
});
-
- describe('.check()', function(done) {
- it('should mark todo item as done', function(done) {
- sinon.stub(storage, 'get', function(key, cb) {
- cb(null, [{text: 'Foo', done: false}]);
- });
- sinon.stub(storage, 'set', function(key, value, cb) {
- key.should.eql('items');
- value.should.eql([ { text: 'Foo', done: true } ]);
- cb();
- });
- sinon.stub(storage, 'save', function(key, cb) {
- storage.set.restore();
- storage.get.restore();
- storage.save.restore();
- done();
- });
+
+ describe('check', function() {
+ it('marks todo item as done', function() {
+ storage.items = [{done: false}];
+
commands.check(1);
+
+ storage.items.should.eql([{done: true}]);
});
});
-
- describe('.undo()', function(done) {
- it('should mark todo item as not done yet', function(done) {
- sinon.stub(storage, 'get', function(key, cb) {
- cb(null, [{text: 'Foo', done: true}]);
- });
- sinon.stub(storage, 'set', function(key, value, cb) {
- key.should.eql('items');
- value.should.eql([ { text: 'Foo', done: false } ]);
- cb();
- });
- sinon.stub(storage, 'save', function(key, cb) {
- storage.set.restore();
- storage.get.restore();
- storage.save.restore();
- done();
- });
+
+ describe('undo', function() {
+ it('marks todo item as not done yet', function() {
+ storage.items = [{done: true}];
+
commands.undo(1);
+
+ storage.items.should.eql([{done: false}]);
});
});
-
- describe('.delete()', function(done) {
- it('should remove an item', function(done) {
- sinon.stub(storage, 'get', function(key, cb) {
- cb(null, [{text: 'Foo', done: true}]);
- });
- 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.get.restore();
- storage.save.restore();
- done();
- });
+
+ describe('delete', function() {
+ it('removes an item', function() {
+ storage.items = [1, 2];
+
commands.delete(1);
+
+ storage.items.should.eql([2]);
});
});
-
- describe('.add()', function(done) {
- it('should remove an item', function(done) {
- sinon.stub(storage, 'get', function(key, cb) {
- cb(null, [{text: 'Foo', done: true}]);
- });
- sinon.stub(storage, 'set', function(key, value, cb) {
- key.should.eql('items');
- value.should.eql([{text: 'Foo', done: true}, {text: 'Foo bar', done: false}]);
- cb();
- });
- sinon.stub(storage, 'save', function(key, cb) {
- storage.set.restore();
- storage.get.restore();
- storage.save.restore();
- done();
- });
- commands.add('Foo bar');
+
+ describe('add', function() {
+ it('adds an item', function() {
+ storage.items = [1];
+
+ commands.add("Foo bar");
+
+ storage.items[1].should.eql({text: 'Foo bar', done: false});
});
});
-
- 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();
- });
+
+ describe('clear', function() {
+ it('clear all todos', function() {
+ storage.items = [1,2,3,4];
+
commands.clear();
+
+ storage.items.should.eql([]);
});
});
});

0 comments on commit 3dcd2b8

Please sign in to comment.