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: 'johndoe@gmail.com',
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);
});
});
});
@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