Last active
November 22, 2018 21:52
-
-
Save Johnsonojo/2cff5ad6d6f11991bd45983b23d15efd to your computer and use it in GitHub Desktop.
Tests for delete a articles endpoint
This file contains 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 chaiHttp from 'chai-http'; | |
import chai from 'chai'; | |
import app from '../app'; | |
let userToken; | |
const userSignin = '/api/v1/auth/login'; | |
const user = { | |
email: '[email protected]', | |
password: 'WhyDoYouCare?', | |
}; | |
const { expect } = chai; | |
chai.use(chaiHttp); | |
describe('Article Controller', () => { | |
before((done) => { | |
chai.request(app) | |
.post(`${userSignin}`) | |
.send(user) | |
.end((err, res) => { | |
userToken = res.body.data.token; | |
done(err); | |
}); | |
}); | |
it('should allow authencticated delete his article', (done) => { | |
chai.request(app) | |
.delete('/api/v1/users/10/articles/1') | |
.set('token', userToken) | |
.end((err, res) => { | |
expect(res.status).to.equal(200); | |
expect(res.body).to.be.an('object'); | |
expect(res.body.status).to.equal('success'); | |
expect(res.body.message).to.equal('Article 1 deleted successfully'); | |
done(err); | |
}); | |
}); | |
it('should throw an error for a non-existent article id', (done) => { | |
chai.request(app) | |
.delete('/api/v1/users/10/articles/10000') | |
.set('token', userToken) | |
.end((err, res) => { | |
expect(res.status).to.equal(404); | |
expect(res.body).to.be.an('object'); | |
expect(res.body.status).to.equal('failure'); | |
expect(res.body.message).to.equal('Article not found'); | |
done(err); | |
}); | |
}); | |
it('should throw an error for a string article id', (done) => { | |
chai.request(app) | |
.delete('/api/v1/users/10/articles/1o0') | |
.set('token', userToken) | |
.end((err, res) => { | |
expect(res.status).to.equal(400); | |
expect(res.body).to.be.an('object'); | |
expect(res.body.status).to.equal('failure'); | |
expect(res.body.message).to.equal('Article validation not successful'); | |
expect(res.body.data[0].msg).to.equal('Article id must be an integer'); | |
done(err); | |
}); | |
}); | |
}); |
Wonderful job, Johnson. You have a typo on line 16, just do a quick fix.
Thanks frostyblok for your comment.
Nice Job. I think you should add an "expect" line to your successful-delete test case that would ensure that the article is removed from your table/model.
Nice one Johnson, I like you how you made use of the before function
Nice work, chief! Really exhaustive! 💪
Like @marcdomain stated, however, checking the database if the article was actually deleted wouldn't be a bad idea.
Ok. Thank you guys for the feedback. I will fix it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These are tests for the endpoint that enables a user to delete one of his articles. I ensured that the user is authenticated before deleting any of his articles.