donal
2018-03-26 c382abd0c94c0c58aaf68b848e427114e7d68be6
commit | author | age
6ef0cc 1 'use strict';
D 2
c382ab 3 const app = require('../../app');
D 4 const request = require('supertest');
6ef0cc 5 require('should');
D 6
7 describe('GET /api/todos', function() {
8
9   it('should respond with JSON array', function(done) {
10     request(app)
11       .get('/api/todos')
12       .expect(200)
13       .expect('Content-Type', /json/)
14       .end(function(err, res) {
15         if (err) return done(err);
16         res.body.should.be.instanceof(Array);
17         done();
18       });
19   });
20
21 });
22
23 describe('POST /api/todos', function() {
24   it('should create the todo and return with the todo', function(done) {
25     request(app)
26       .post('/api/todos')
27       .send({title: 'learn about endpoint/server side testing', completed: false})
28       .expect(201)
29       .expect('Content-Type', /json/)
30       .end(function(err, res) {
31         if (err) return done(err);
32         res.body.should.have.property('_id');
33         res.body.title.should.equal('learn about endpoint/server side testing');
34         res.body.completed.should.equal(false);
35         done();
36       });
37   });
38 });
39
40 describe('GET /api/todos/:id', function() {
c382ab 41   const todoId;
6ef0cc 42   beforeEach(function createObjectToUpdate(done) {
D 43     request(app)
44       .post('/api/todos')
45       .send({title: 'learn about endpoint/server side testing', completed: false})
46       .expect(201)
47       .expect('Content-Type', /json/)
48       .end(function (err, res) {
49         if (err) return done(err);
50         todoId = res.body._id;
51         done();
52       });
53   });
54   it('should update the todo', function (done) {
55     request(app)
56       .get('/api/todos/' + todoId)
57       .expect(200)
58       .expect('Content-Type', /json/)
59       .end(function (err, res) {
60         if (err) return done(err);
61         res.body._id.should.equal(todoId);
62         res.body.title.should.equal('learn about endpoint/server side testing');
63         res.body.completed.should.equal(false);
64         done();
65       });
66   });
67   it('should return 404 for valid mongo object id that does not exist', function(done){
68     request(app)
69       .get('/api/todos/' + 'abcdef0123456789ABCDEF01')
70       .expect(404)
71       .end(function(err) {
72         if (err) return done(err);
73         done();
74       });
75   });
76   it('should return 400 for invalid object ids', function(done){
77     request(app)
78       .get('/api/todos/' + 123)
79       .expect(400)
80       .end(function(err, res) {
81         if (err) return done(err);
82         res.text.should.equal('not a valid mongo object id')
83         done();
84       });
85   });
86 });
87
88 describe('PUT /api/todos/:id', function() {
c382ab 89   const todoId;
6ef0cc 90   beforeEach(function createObjectToUpdate(done){
D 91     request(app)
92       .post('/api/todos')
93       .send({title: 'learn about endpoint/server side testing', completed: false})
94       .expect(201)
95       .expect('Content-Type', /json/)
96       .end(function(err, res) {
97         if (err) return done(err);
98         todoId = res.body._id;
99         done();
100       });
101   });
102   it('should update the todo', function(done) {
103     request(app)
104       .put('/api/todos/' + todoId)
105       .send({title: 'LOVE endpoint/server side testing!', completed: true})
106       .expect(200)
107       .expect('Content-Type', /json/)
108       .end(function(err, res) {
109         if (err) return done(err);
110         res.body.should.have.property('_id');
111         res.body.title.should.equal('LOVE endpoint/server side testing!');
112         res.body.completed.should.equal(true);
113         done();
114       });
115   });
116   it('should return 404 for valid mongo object id that does not exist', function(done){
117     request(app)
118       .put('/api/todos/' + 'abcdef0123456789ABCDEF01')
119       .send({title: 'LOVE endpoint/server side testing!', completed: true})
120       .expect(404)
121       .end(function(err) {
122         if (err) return done(err);
123         done();
124       });
125   });
126   it('should return 400 for invalid object ids', function(done){
127     request(app)
128       .put('/api/todos/' + 123)
129       .send({title: 'LOVE endpoint/server side testing!', completed: true})
130       .expect(400)
131       .end(function(err, res) {
132         if (err) return done(err);
133         res.text.should.equal('not a valid mongo object id')
134         done();
135       });
136   });
137 });
138
139 describe('DELETE /api/todos/:id', function() {
c382ab 140   const todoId;
6ef0cc 141   beforeEach(function createObjectToUpdate(done){
D 142     request(app)
143       .post('/api/todos')
144       .send({title: 'learn about endpoint/server side testing', completed: false})
145       .expect(201)
146       .expect('Content-Type', /json/)
147       .end(function(err, res) {
148         if (err) return done(err);
149         todoId = res.body._id;
150         done();
151       });
152   });
153   it('should delete the todo', function(done) {
154     request(app)
155       .delete('/api/todos/' + todoId)
156       .expect(204)
157       .end(function(err) {
158         if (err) return done(err);
159         done();
160       });
161   });
162   it('should return 404 for valid mongo object id that does not exist', function(done){
163     request(app)
164       .delete('/api/todos/' + 'abcdef0123456789ABCDEF01')
165       .expect(404)
166       .end(function(err) {
167         if (err) return done(err);
168         done();
169       });
170   });
171   it('should return 400 for invalid object ids', function(done){
172     request(app)
173       .delete('/api/todos/' + 123)
174       .send({title: 'LOVE endpoint/server side testing!', completed: true})
175       .expect(400)
176       .end(function(err, res) {
177         if (err) return done(err);
178         res.text.should.equal('not a valid mongo object id')
179         done();
180       });
181   });
182 });