TwoToc code
[YouAndWeb_TwoToc] / server / api / comment / comment.integration.js
1 'use strict';
2
3 var app = require('../..');
4 var request = require('supertest');
5
6 var newComment;
7
8 describe('Comment API:', function() {
9
10   describe('GET /api/comments', function() {
11     var comments;
12
13     beforeEach(function(done) {
14       request(app)
15         .get('/api/comments')
16         .expect(200)
17         .expect('Content-Type', /json/)
18         .end(function(err, res) {
19           if (err) {
20             return done(err);
21           }
22           comments = res.body;
23           done();
24         });
25     });
26
27     it('should respond with JSON array', function() {
28       comments.should.be.instanceOf(Array);
29     });
30
31   });
32
33   describe('POST /api/comments', function() {
34     beforeEach(function(done) {
35       request(app)
36         .post('/api/comments')
37         .send({
38           name: 'New Comment',
39           info: 'This is the brand new comment!!!'
40         })
41         .expect(201)
42         .expect('Content-Type', /json/)
43         .end(function(err, res) {
44           if (err) {
45             return done(err);
46           }
47           newComment = res.body;
48           done();
49         });
50     });
51
52     it('should respond with the newly created comment', function() {
53       newComment.name.should.equal('New Comment');
54       newComment.info.should.equal('This is the brand new comment!!!');
55     });
56
57   });
58
59   describe('GET /api/comments/:id', function() {
60     var comment;
61
62     beforeEach(function(done) {
63       request(app)
64         .get('/api/comments/' + newComment._id)
65         .expect(200)
66         .expect('Content-Type', /json/)
67         .end(function(err, res) {
68           if (err) {
69             return done(err);
70           }
71           comment = res.body;
72           done();
73         });
74     });
75
76     afterEach(function() {
77       comment = {};
78     });
79
80     it('should respond with the requested comment', function() {
81       comment.name.should.equal('New Comment');
82       comment.info.should.equal('This is the brand new comment!!!');
83     });
84
85   });
86
87   describe('PUT /api/comments/:id', function() {
88     var updatedComment
89
90     beforeEach(function(done) {
91       request(app)
92         .put('/api/comments/' + newComment._id)
93         .send({
94           name: 'Updated Comment',
95           info: 'This is the updated comment!!!'
96         })
97         .expect(200)
98         .expect('Content-Type', /json/)
99         .end(function(err, res) {
100           if (err) {
101             return done(err);
102           }
103           updatedComment = res.body;
104           done();
105         });
106     });
107
108     afterEach(function() {
109       updatedComment = {};
110     });
111
112     it('should respond with the updated comment', function() {
113       updatedComment.name.should.equal('Updated Comment');
114       updatedComment.info.should.equal('This is the updated comment!!!');
115     });
116
117   });
118
119   describe('DELETE /api/comments/:id', function() {
120
121     it('should respond with 204 on successful removal', function(done) {
122       request(app)
123         .delete('/api/comments/' + newComment._id)
124         .expect(204)
125         .end(function(err, res) {
126           if (err) {
127             return done(err);
128           }
129           done();
130         });
131     });
132
133     it('should respond with 404 when comment does not exist', function(done) {
134       request(app)
135         .delete('/api/comments/' + newComment._id)
136         .expect(404)
137         .end(function(err, res) {
138           if (err) {
139             return done(err);
140           }
141           done();
142         });
143     });
144
145   });
146
147 });