Created
May 18, 2019 08:29
-
-
Save Bilguun132/98be8716d57910df7a39739a6513830b to your computer and use it in GitHub Desktop.
TDDNode-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { User } = require("../models/user.model"); | |
const request = require("supertest"); | |
const expect = require("chai").expect; | |
const app = require("../app"); | |
describe("api/users", () => { | |
beforeEach(async () => { | |
await User.deleteMany({}); | |
}); | |
describe("GET /", () => { | |
it("should return all users", async () => { | |
const users = [ | |
{ name: "test", email: "[email protected]", gender: "male" }, | |
{ name: "test1", email: "[email protected]", gender: "female" } | |
]; | |
await User.insertMany(users); | |
console.log(users); | |
const res = await request(app).get("/api/users"); | |
expect(res.status).to.equal(200); | |
expect(res.body.length).to.equal(2); | |
}); | |
}); | |
describe("GET/:id", () => { | |
it("should return a user if valid id is passed", async () => { | |
const user = new User({ | |
name: "test", | |
email: "[email protected]", | |
gender: "male" | |
}); | |
await user.save(); | |
const res = await request(app).get("/api/users/" + user._id); | |
expect(res.status).to.equal(200); | |
expect(res.body).to.have.property("name", user.name); | |
}); | |
it("should return 400 error when invalid object id is passed", async () => { | |
const res = await request(app).get("/api/users/1"); | |
expect(res.status).to.equal(400); | |
}); | |
it("should return 404 error when valid object id is passed but does not exist", async () => { | |
const res = await request(app).get("/api/users/111111111111"); | |
expect(res.status).to.equal(404); | |
}); | |
}); | |
describe("POST /", () => { | |
it("should return user when the all request body is valid", async () => { | |
const res = await request(app) | |
.post("/api/users") | |
.send({ | |
name: "test", | |
email: "[email protected]", | |
gender: "male" | |
}); | |
expect(res.status).to.equal(200); | |
expect(res.body).to.have.property("_id"); | |
expect(res.body).to.have.property("name", "test"); | |
}); | |
// add more tests to validate request body accordingly eg, make sure name is more than 3 characters etc | |
}); | |
describe("PUT /:id", () => { | |
it("should update the existing order and return 200", async () => { | |
const user = new User({ | |
name: "test", | |
email: "[email protected]", | |
gender: "male" | |
}); | |
await user.save(); | |
const res = await request(app) | |
.put("/api/users/" + user._id) | |
.send({ | |
name: "newTest", | |
email: "[email protected]", | |
gender: "male" | |
}); | |
expect(res.status).to.equal(200); | |
expect(res.body).to.have.property("name", "newTest"); | |
}); | |
}); | |
describe("DELETE /:id", () => { | |
it("should delete requested id and return response 200", async () => { | |
const user = new User({ | |
name: "test", | |
email: "[email protected]", | |
gender: "male" | |
}); | |
await user.save(); | |
const res = await request(app).delete("/api/users/" + user._id); | |
expect(res.status).to.be.equal(200); | |
}); | |
it("should return 404 when deleted user is requested", async () => { | |
const user = new User({ | |
name: "test", | |
email: "[email protected]", | |
gender: "male" | |
}); | |
await user.save(); | |
let res = await request(app).delete("/api/users/" + user._id); | |
expect(res.status).to.be.equal(200); | |
res = await request(app).get("/api/users/" + user._id); | |
expect(res.status).to.be.equal(404); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment