Skip to content

Instantly share code, notes, and snippets.

@Oluwafayokemi
Last active August 8, 2018 22:14
Show Gist options
  • Save Oluwafayokemi/0202a73bb9c76a6005470d5800dde6f0 to your computer and use it in GitHub Desktop.
Save Oluwafayokemi/0202a73bb9c76a6005470d5800dde6f0 to your computer and use it in GitHub Desktop.
TDD for Authors' Haven - Team Elven
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();
});
});
})
@yomigeek
Copy link

  • An in-depth written test.
  • You should correct this test response "The length of the password you have entered is greater than 8, Please enter a password with length greater than 8".
  • I think you need not to write "The email you have entered is wrong, Please check properly to enter a valid email", consider this -> "The email you have entered is wrong, Please enter a valid email".

Good job overall.

@Veraclins
Copy link

A very good set of tests
I have some suggestions:

  • Don't you think your error messages are too lengthy? It may spread over two lines on frontend. For instance 'password should be at least 8 characters' pass the same message
  • Don't you think evaluating variables in the expect body may introduce extra points of possible error and more complexity? For instance, Success signing up ${username} could simply be 'Success signing up Fayokemi'

@chukwuemekachm
Copy link

A good job and in-depth test.
Just like observations made by Veraclins and yomigeek, I also feel your error messages are lengthy.
In your last test, you created three variables and used them in the string literal to return a personified message to the user. I feel you can also send the values in the variables to the server. But then you never assigned values to the variables.

@madeofhuman
Copy link

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.

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