mirror of
https://github.com/wassname/talk.git
synced 2026-08-05 13:30:26 +08:00
Add tests for stream. Move to promises the rightway.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user