From c107a15abb674a6dba7f87437e274c170e68d3fc Mon Sep 17 00:00:00 2001 From: Radek Stepan Date: Mon, 12 Aug 2013 19:43:58 +0100 Subject: [PATCH] not found and empty milestones cases --- src/milestones.coffee | 7 +++++-- test/milestones.coffee | 43 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/milestones.coffee b/src/milestones.coffee index 64dc17f..4f1daf1 100644 --- a/src/milestones.coffee +++ b/src/milestones.coffee @@ -5,7 +5,10 @@ module.exports = 'get_current': (user, repo, cb) -> req.milestones user, repo, (err, data) -> return cb err if err + return cb data.message if data.message + return cb 'No open milestones for a repo' unless data.length # Go through the milestones looking for one that ends/ended soonest. - max = [ null, +Infinity ] - ( max = [ parseInt(i), int ] if (int = +new Date due_on) < max[1] for i, { due_on } of data ) + max = [ null, 'A' ] + for i, { due_on } of data when due_on < max[1] + max = [ parseInt(i), due_on ] cb null, data[max[0]] \ No newline at end of file diff --git a/test/milestones.coffee b/test/milestones.coffee index 656d215..3f2a5de 100644 --- a/test/milestones.coffee +++ b/test/milestones.coffee @@ -10,7 +10,7 @@ milestones = proxy path.resolve(__dirname, '../src/milestones.coffee'), './request': req module.exports = - 'get current': (done) -> + 'get current from 1': (done) -> req.milestones = (user, repo, cb) -> cb null, [ { @@ -23,4 +23,45 @@ module.exports = milestones.get_current null, null, (err, milestone) -> assert.ifError err assert.equal milestone.number, 1 + done.call null + + 'get current from > 1': (done) -> + req.milestones = (user, repo, cb) -> + cb null, [ + { + 'number': 1 + 'created_at': '2013-01-01T00:00:00Z' + 'due_on': '2013-02-01T00:00:00Z' + } + { + 'number': 2 + 'created_at': '2013-01-01T00:00:00Z' + 'due_on': '2013-01-15T00:00:00Z' + } + { + 'number': 3 + 'created_at': '2013-01-01T00:00:00Z' + 'due_on': '2013-02-15T00:00:00Z' + } + ] + + milestones.get_current null, null, (err, milestone) -> + assert.ifError err + assert.equal milestone.number, 2 + done.call null + + 'get current when empty': (done) -> + req.milestones = (user, repo, cb) -> + cb null, [] + + milestones.get_current null, null, (err, milestone) -> + assert.equal err, 'No open milestones for a repo' + done.call null + + 'get current when not found': (done) -> + req.milestones = (user, repo, cb) -> + cb null, { 'message': 'Not Found' } + + milestones.get_current null, null, (err, milestone) -> + assert.equal err, 'Not Found' done.call null \ No newline at end of file