Skip to content

Instantly share code, notes, and snippets.

@madeofhuman
Last active July 25, 2018 08:36
Show Gist options
  • Save madeofhuman/4526ef75abbe3b77317feddc2dcde2f3 to your computer and use it in GitHub Desktop.
Save madeofhuman/4526ef75abbe3b77317feddc2dcde2f3 to your computer and use it in GitHub Desktop.
TDD For Authors' Haven - Elven
// Chukwuka Odina
// Import the Chai assertion library and it's http plugin
// Use 'should' as the assertion type
import chai from 'chai';
import 'chai/register-should';
import chaiHttp from 'chai-http';
// Import the app
import app from '../server/index';
// Initialise chai with the http plugin
chai.use(chaiHttp);
describe('User follow action', () => {
const alreadyFollowedUser = 'veraclins'; // a user profile that the test user is already following
const unfollowedUser = 'andela'; // a user profile that the test user is not following
const self = 'madeofhuman' // the test user
const token = 'Bearer 762busajsnoay4a.njsahib8yogr.mnasuheegfv73hybj' // the bearer token
describe('When the user is already following the new user', () => {
it('should not follow the new user', (done) => {
chai.request(app)
.post(`/api/profiles/${alreadyFollowedUser}/follow`)
.set('content-type', 'application/json')
.set('Authorization', token)
.end((err, res) => {
res.should.have.status(409);
res.body.should.be.an('object').with.property('errors');
res.body.errors.should.be.an('object').with.property('message')
.equals('You are already following this user.');
done();
});
});
});
describe('When the user is not yet following the new user', () => {
it('should follow the new user', (done) => {
chai.request(app)
.post(`/api/profiles/${unfollowedUser}/follow`)
.set('content-type', 'application/json')
.set('Authorization', token)
.end((err, res) => {
res.should.have.status(201);
res.body.should.be.an('object').with.property('success');
res.body.success.should.have.property('message')
.equals(`You are now following ${unfollowedUser}`);
res.body.should.have.property('user');
res.body.user.should.be.an('object').with.property('username')
.equals(unfollwedUser);
done();
});
});
});
describe('When the user tries to follow themself', () => {
it('should return an error', (done) => {
chai.request(app)
.post(`/api/profiles/${self}/follow`)
.set('content-type', 'application/json')
.set('Authorization', token)
.end((err, res) => {
res.should.have.status(409);
res.body.should.be.an('object').with.property('errors');
res.body.errors.should.be.an('object').with.property('message')
.equals(`You cannot follow yourself.`);
done();
});
});
});
});
@yomigeek
Copy link

yomigeek commented Jul 23, 2018

The code test is descriptive enough to understand the purpose.
Nice work.

@Veraclins
Copy link

The tests are clear and straightforward. But don't you think using variables in the body of the tests could lead to errors? Like You are now following ${unfollowedUser} could simply be You are now following andela.

@Oluwafayokemi
Copy link

Nice Work, Well done CJ.

@madeofhuman
Copy link
Author

@Veraclins I understand where you're coming from. In a more complex test, I will hard-code the expected values.

@chukwuemekachm
Copy link

A well formatted and descriptive task. Good job. But I feel you expectin g 422 as the status code of your last test would be much better. This is because 409 means the resource you are trying to create exists on the server already and would create conflicts. 422 means the server understands your request but can't process it due to semantic errors.

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