-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature[167899197]Users should be able to read articles
- create controller method to view article - write test to test view article controller method [Delivers #167899197]
- Loading branch information
1 parent
550a208
commit 7f54d1b
Showing
6 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import * as createArticle from './create.test'; | ||
import * as likeOrDislikeArticle from './likeOrDislikeArticle.test'; | ||
import * as removeArticleLikeOrDislike from './removeArticleLikeOrDislike.test'; | ||
import * as viewArticle from './viewArticle.test'; | ||
|
||
export { createArticle, likeOrDislikeArticle, removeArticleLikeOrDislike }; | ||
export { | ||
createArticle, | ||
likeOrDislikeArticle, | ||
removeArticleLikeOrDislike, | ||
viewArticle | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import chai, { expect } from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import { config } from 'dotenv'; | ||
import sinon from 'sinon'; | ||
import app from '../../server'; | ||
import Articles from '../../server/controllers/Articles'; | ||
|
||
const { viewArticle } = Articles; | ||
|
||
config(); | ||
|
||
chai.use(chaiHttp); | ||
|
||
const { BASE_URL } = process.env; | ||
|
||
describe('GET Article', () => { | ||
context('when a user views a published article', () => { | ||
it('it returns the user profile', async () => { | ||
const response = await chai | ||
.request(app) | ||
.get(`${BASE_URL}/articles/how-to-train-your-dragon`); | ||
expect(response).to.have.status(200); | ||
expect(response.body.article).to.not.have.property('slug', 'isArchived'); | ||
expect(response.body.article).to.have.any.keys( | ||
'id', | ||
'title', | ||
'description', | ||
'image', | ||
'articleBody', | ||
'likesCount', | ||
'dislikesCount', | ||
'publishedAt', | ||
'createdAt', | ||
'updatedAt', | ||
'authorId' | ||
); | ||
}); | ||
}); | ||
|
||
context('when a user tries to view an article that does not exist', () => { | ||
it('it returns a not found error', async () => { | ||
const response = await chai | ||
.request(app) | ||
.get(`${BASE_URL}/articles/90000`); | ||
expect(response).to.have.status(404); | ||
expect(response.body.error).to.equal('article not found'); | ||
}); | ||
}); | ||
|
||
context('when a user tries to view an archived article', () => { | ||
it('it returns the user profile', async () => { | ||
const response = await chai | ||
.request(app) | ||
.get(`${BASE_URL}/articles/how-to-train-your-dragon-6`); | ||
expect(response).to.have.status(404); | ||
expect(response.body.error).to.equal('article not found'); | ||
}); | ||
}); | ||
|
||
context('when the database is unable to query the user table ', () => { | ||
it('will throw a server error', async () => { | ||
const stubfunc = { viewArticle }; | ||
const sandbox = sinon.createSandbox(); | ||
sandbox.stub(stubfunc, 'viewArticle').rejects(new Error('Server Error')); | ||
|
||
const next = sinon.spy(); | ||
const res = { | ||
status: () => ({ | ||
json: next | ||
}) | ||
}; | ||
await viewArticle({}, res); | ||
sinon.assert.calledOnce(next); | ||
}); | ||
}); | ||
}); |