Last active
July 25, 2018 08:36
-
-
Save madeofhuman/4526ef75abbe3b77317feddc2dcde2f3 to your computer and use it in GitHub Desktop.
TDD For Authors' Haven - 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
// 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(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 because409
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.