Skip to content

Instantly share code, notes, and snippets.

@ekpangmichael
Last active November 24, 2018 15:00
Show Gist options
  • Save ekpangmichael/817e37661bca744500707402bc8c81d7 to your computer and use it in GitHub Desktop.
Save ekpangmichael/817e37661bca744500707402bc8c81d7 to your computer and use it in GitHub Desktop.
userTest.js
import request from "supertest";
import chaiHttp from "chai-http";
import chai, { expect } from "chai";
chai.use(chaiHttp);
const newUser = {
userName: "Ekpang Michael",
userEmail: "[email protected]",
userAddress: "Lagos",
userPassword: "test123"
};
describe("### Testing Users Routes", () => {
it("should create a new user and get back a response", done => {
request(app)
.post("/api/v1/users")
.send(newUser)
.end((err, res) => {
const data = JSON.parse(res.text);
const name = data[1].users.userName;
expect(err).to.be.a("null");
expect(name).to.equal(newUser.userName);
expect(data[0].message).to.equal("Registration successful");
done();
});
});
it("Signin should return user sigin successful", done => {
request(app)
.post("/api/v1/users/signin")
.send({
userEmail: newUser.userEmail,
userPassword: newUser.userPassword
})
.end((err, res) => {
const data = JSON.parse(res.text);
const name = data[1].users.userName;
expect(err).to.be.a("null");
expect(name).to.equal(newUser.userName);
expect(data[0].message).to.equal("User login successfully");
done();
});
});
it("Get one particular user - should return User found successfully", done => {
request(app)
.get(`/api/v1/users/${id}`)
.send()
.end((err, res) => {
expect(res).to.have.status(200);
const data = JSON.parse(res.text);
expect(data[0].message).to.equal("User found successfully");
done();
});
});
it("create new user with the same email should return Email already Taken", done => {
request(app)
.post("/api/v1/users/")
.send(newUser)
.end((err, res) => {
expect(res).to.have.status(400);
const data = JSON.parse(res.text);
expect(data.message).to.equal("Email already Taken!");
done();
});
});
});
@theghostyced
Copy link

Lovely one Ekpang. But I would love to know your thought process behind returning your res.text as an array. Is there any particular reason behind this?

Lovely one again

@ekpangmichael
Copy link
Author

@theghostyced thanks for the feedback. Please What do you think is a better implementation?

@theghostyced
Copy link

theghostyced commented Nov 22, 2018

Well I would have expected you to send your response in a JSON obj

{ 
          message: 'Lorem Ipsum',
          user: { userName: 'Ekpang' }
 }

so you could then call the data as data.message or data.user.userName
But it all good though. Nice one

@davidshare
Copy link

Looks good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment