Last active
August 8, 2018 22:14
-
-
Save Oluwafayokemi/0202a73bb9c76a6005470d5800dde6f0 to your computer and use it in GitHub Desktop.
TDD for Authors' Haven - Team Elven
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
import chai from 'chai'; | |
import supertest from 'supertest'; | |
import app from '../app'; | |
const request = supertest(app); | |
const { expect } = chai; | |
// existing user email | |
// invalid email | |
// password of length less than 8 | |
// password not being alphanumeric | |
// successful sign up | |
describe('Test user API', () => { | |
it('should return a status 400 error if email already exist', (done) => { | |
request | |
.post('/api/users') | |
.set('Content-type', 'application/json') | |
.send({ | |
username: 'Fayokemi', | |
email: '[email protected]', | |
password: 'signup49test', | |
}) | |
.end((err, res) => { | |
expect(res.status).to.equal(400); | |
expect(res.body).to.be.an('object'); | |
expect(res.body).to.haveOwnProperty('errors').to.be.an('array'); | |
expect(res.body.errors.email[0]).to.equal('The email you have entered already exist'); | |
done(); | |
}); | |
}); | |
it('should return a status 400 error response for invalid email format', (done) => { | |
request | |
.post('/api/users') | |
.set('Content-type', 'application/json') | |
.send({ | |
username: 'Fayokemi', | |
email: 'fayoarightcom', | |
password: 'signup49test', | |
}) | |
.end((err, res) => { | |
expect(res.status).to.equal(400); | |
expect(res.body).to.be.an('object'); | |
expect(res.body).to.be.an('array'); | |
expect(res.body.errors.email[1]).to.equal('The email you have entered is invalid'); | |
done(); | |
}); | |
}); | |
it('should return a status 400 error if the password length is less than 8', (done) => { | |
request | |
.post('/api/users') | |
.set('Content-type', 'application/json') | |
.send({ | |
username: 'Fayokemi', | |
email: '[email protected]', | |
password: 'sign', | |
}) | |
.end((err, res) => { | |
expect(res.status).to.equal(400); | |
expect(res.body).to.be.an('object'); | |
expect(res.body).to.haveOwnProperty('errors').to.be.an('array'); | |
expect(res.body.errors.password[0]).to.equal('Password length may not be less than 8'); | |
done(); | |
}); | |
}); | |
it('should return a status 400 error if the password is not Alphanumeric', (done) => { | |
request | |
.post('/api/users') | |
.set('Content-type', 'application/json') | |
.send({ | |
username: 'Fayokemi', | |
email: '[email protected]', | |
password: 'signeduptoday', | |
}) | |
.end((err, res) => { | |
expect(res.status).to.equal(400); | |
expect(res.body).to.be.an('object'); | |
expect(res.body.errors).to.be.an('array'); | |
expect(res.body).to.haveOwnProperty('errors').to.be.an('array'); | |
expect(res.body.errors.password[1]).to.equal('The password field should contain both letters and numerals'); | |
done(); | |
}); | |
}); | |
it('should return a status 200 for successfully signing up', (done) => { | |
let username; | |
let firstName; | |
let password; | |
request | |
.post('/api/users') | |
.set('Content-type', 'application/json') | |
.send({ | |
username: 'Fayokemi', | |
email: '[email protected]', | |
password: 'signed49test7', | |
}) | |
.end((err, res) => { | |
expect(res.status).to.equal(400); | |
expect(res.body).to.be.an('object'); | |
expect(res.body).to.haveOwnProperty('message').to.equal(`Success signing up ${username}`); | |
expect(res.body.email).to.equal(`${email}`); | |
expect(res.body.password).to.equal(`${password}`); | |
done(); | |
}); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good job on the test.
Echoing what everybody else has said, I feel the responses are too wordy. Try and make then more concise next time.