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