1 | var should = require('should')
|
2 | , request = require('supertest')
|
3 | , mongoose = require('mongoose')
|
4 | , app = require('./support/')
|
5 | , db = require('./support/db')
|
6 | , models = require('./support/models')
|
7 | , User = models.User
|
8 | , Tag = models.Tag
|
9 | , Post = models.Post
|
10 | , Comment = models.Comment;
|
11 |
|
12 | describe('comments', function() {
|
13 | beforeEach(function(done) {
|
14 | db.setUp(done);
|
15 | });
|
16 | afterEach(function(done) {
|
17 | db.tearDown(done);
|
18 | });
|
19 | describe('GET /', function() {
|
20 | it('should return a list of comments', function(done) {
|
21 | request(app)
|
22 | .get('/comments')
|
23 | .expect(200).end(function(err, res) {
|
24 | if (err) done(err)
|
25 | res.body.comments.length.should.equal(1);
|
26 | done();
|
27 | });
|
28 | });
|
29 | });
|
30 | describe('POST /', function() {
|
31 | it('should create and return a comment', function(done) {
|
32 | request(app)
|
33 | .post('/comments').send({ comment: {
|
34 | content: 'b',
|
35 | post_id: post.id
|
36 | }})
|
37 | .expect(200).end(function(err, res) {
|
38 | if (err) done(err)
|
39 | res.body.comment.content.should.equal('b');
|
40 | res.body.comment.post_id.should.equal(post.id);
|
41 | res.body.comment.user_id.should.equal(user.id);
|
42 | done();
|
43 | });
|
44 | });
|
45 | });
|
46 | describe('GET /:id', function() {
|
47 | it('should return a comment', function(done) {
|
48 | request(app)
|
49 | .get('/comments/' + comment.id)
|
50 | .expect(200)
|
51 | .end(function(err, res) {
|
52 | if (err) done(err)
|
53 | res.body.comment._id.should.equal(comment.id);
|
54 | res.body.comment.content.should.equal('a');
|
55 | res.body.comment.post_id.should.equal(post.id);
|
56 | done();
|
57 | });
|
58 | });
|
59 | });
|
60 | describe('PUT /:id', function() {
|
61 | it('should update and return a comment', function(done) {
|
62 | request(app)
|
63 | .put('/comments/' + comment.id)
|
64 | .send({ comment: {
|
65 | content: 'b'
|
66 | }})
|
67 | .expect(200)
|
68 | .end(function(err, res) {
|
69 | res.body.comment.content.should.equal('b');
|
70 | if (err) done(err)
|
71 | Comment.findById(comment.id, function(err, comment) {
|
72 | if (err) done(err)
|
73 | comment.content.should.equal('b');
|
74 | done();
|
75 | });
|
76 | });
|
77 | });
|
78 | });
|
79 | describe('QUERY /', function() {
|
80 | it('should return matched posts', function(done) {
|
81 | request(app)
|
82 | .post('/comments')
|
83 | .send(
|
84 | {
|
85 | query: {
|
86 | q:{
|
87 | content: 'a'
|
88 | }
|
89 | }
|
90 | }
|
91 | )
|
92 | .expect(200)
|
93 | .end(function(err, res) {
|
94 | if (err) done(err)
|
95 | res.body.comments.length.should.equal(1);
|
96 | done();
|
97 | });
|
98 | });
|
99 | });
|
100 | describe('DELETE /:id', function() {
|
101 | it('should remove a comment', function(done) {
|
102 | request(app)
|
103 | .del('/comments/' + comment.id)
|
104 | .expect(200)
|
105 | .expect({})
|
106 | .end(function(err, res) {
|
107 | if (err) done(err)
|
108 | Comment.findById(comment.id, function(err, _comment) {
|
109 | if (err) done(err)
|
110 | should.not.exist(_comment);
|
111 | done();
|
112 | });
|
113 | });
|
114 | });
|
115 | });
|
116 | });
|