Skip to content

Instantly share code, notes, and snippets.

@Johnsonojo
Last active November 22, 2018 21:52
Show Gist options
  • Save Johnsonojo/2cff5ad6d6f11991bd45983b23d15efd to your computer and use it in GitHub Desktop.
Save Johnsonojo/2cff5ad6d6f11991bd45983b23d15efd to your computer and use it in GitHub Desktop.
Tests for delete a articles endpoint
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);
});
});
});
@Johnsonojo
Copy link
Author

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.

@frostyblok
Copy link

Wonderful job, Johnson. You have a typo on line 16, just do a quick fix.

@Johnsonojo
Copy link
Author

Thanks frostyblok for your comment.

@marcdomain
Copy link

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.

@oluwajuwon
Copy link

Nice one Johnson, I like you how you made use of the before function

@akhilome
Copy link

Nice work, chief! Really exhaustive! 💪

Like @marcdomain stated, however, checking the database if the article was actually deleted wouldn't be a bad idea.

@Johnsonojo
Copy link
Author

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