Skip to content

Instantly share code, notes, and snippets.

@oluwajuwon
Last active November 22, 2018 19:47
Show Gist options
  • Save oluwajuwon/9dc512202dcbb9695deb175887fd6b65 to your computer and use it in GitHub Desktop.
Save oluwajuwon/9dc512202dcbb9695deb175887fd6b65 to your computer and use it in GitHub Desktop.
This test is for a post request to the article endpoint. From the test, you can see the values I initialised and sent to the endpoint. I also imported my token generation controller to be able to generate a token for the test user to be able to post a new article. The test is supposed to return a status of 201 if the article was successfully posted
import { expect } from 'chai';
import request from 'supertest';
import app from '../app';
import generateToken from '../controllers/generateToken';
const user = { user: { userId: 1, userType: 'Author' } };
const userToken = generateToken.createToken(user);
describe('POST /api/v1/article', () => {
it('should return status 201 if the new article was added', (done) => {
const articleTitle = 'Reflections on the EPIC CALL Values';
const articleContent = 'The EPIC CALL values mean Excellence, Passionate...';
const createdAt = new Date();
const updatedAt = new Date();
request(app)
.post('/api/v1/article')
.set('x-access-token', userToken)
.send({
articleTitle, articleContent, createdAt, updatedAt,
})
.end((err, response) => {
expect(response.status).to.equal(201);
done();
});
});
}
@frostyblok
Copy link

You have done a great job, Juwon!

@Johnsonojo
Copy link

Nice implementation, but why don't you have another file that contains the article JSON object, and then import and use it in your test?

@akhilome
Copy link

Awesome work, Juwon. 👏

I, however, think you can make more assertions because as Wale mentioned during the session, it's not enough to just assert for the status code returned by the endpoint being tested.

How about checking the database to see if the POSTed data eventually got into it?

Again, awesome work. 👍

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