From 43a70ea71c567e0b410dd97d8c6b73b4b3457cf9 Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 4 Nov 2016 14:45:46 -0700 Subject: [PATCH] Add tests for stream. Move to promises the rightway. --- routes/api/stream/index.js | 14 +++++++++----- tests/routes/api/stream/index.js | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 1bbe259e4..4b7c3e6fa 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -8,16 +8,20 @@ const router = express.Router(); router.get('/', (req, res, next) => { - const comments = Comment.findByAssetId(req.query.asset_id); - const users = User.findByIdArray(comments.map((comment) => comment.author_id)); - const actions = Action.findByItemIdArray(comments.map((comment) => comment.id)); + const commentsPromise = Comment.findByAssetId(req.query.asset_id); - Promise.all([comments, users, actions]).then(([comments, users, actions]) => { + commentsPromise.then(comments => { + return Promise.all([ + comments, + User.findByIdArray(comments.map((comment) => comment.author_id)), + Action.findByItemIdArray(comments.map((comment) => comment.id)) + ]); + }).then(([comments, users, actions]) => { res.json([...comments,...users,...actions]); }).catch(error => { + console.log(error); next(error); }); - }); module.exports = router; diff --git a/tests/routes/api/stream/index.js b/tests/routes/api/stream/index.js index aeff010de..a4ca53228 100644 --- a/tests/routes/api/stream/index.js +++ b/tests/routes/api/stream/index.js @@ -1,4 +1,11 @@ require('../../../utils/mongoose'); + +const app = require('../../../../app'); +const chai = require('chai'); +const chaiHttp = require('chai-http'); +chai.use(chaiHttp); +var expect = chai.expect; + const Action = require('../../../../models/action'); const User = require('../../../../models/user'); const Comment = require('../../../../models/comment'); @@ -44,5 +51,15 @@ describe('api/stream: routes', () => { }) }) - it('should return a stream with comments, users and actions') + it('should return a stream with comments, users and actions', function(done){ + chai.request(app) + .get('/api/v1/stream') + .query({'asset_id': 'asset'}) + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + if (err) return done(err); + done(); + }); + }) })