Created
July 8, 2020 13:07
-
-
Save kianaditya/23453be7d6a8bb6b682a9950f24db234 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const sinon = require('sinon') | |
const expect = require('chai').expect | |
const queries = require('../../../src/models/queries') | |
const { index, show } = require('../../../src/controllers/posts') | |
describe('Posts controller', () => { | |
const posts = [] | |
const specificPost = {} | |
describe('GET Posts', () => { | |
it('should use index action and send response', async () => { | |
const findAll = sinon.stub(queries, 'getAllPosts').resolves(posts) | |
let resSpy = sinon.spy() | |
const req = {} | |
const res = { | |
status: sinon.stub().returns({ send: resSpy }), | |
} | |
await index(req, res) | |
expect(findAll.calledOnce).to.equal(true) | |
expect(resSpy.calledOnce).to.equal(true) | |
expect(resSpy.calledWith(posts)).to.equal(true) | |
findAll.restore() | |
}) | |
}) | |
describe('GET specific post', () => { | |
it('should use show action and send response', async () => { | |
const findOne = sinon | |
.stub(queries, 'getSpecificPost') | |
.resolves(specificPost) | |
let resSpy = sinon.spy() | |
const req = { | |
params: { | |
id: 1, | |
}, | |
} | |
const res = { | |
status: () => { | |
return { | |
send: resSpy, | |
} | |
}, | |
} | |
const spy = sinon.spy(res, 'status') | |
await show(req, res) | |
expect(findOne.calledOnce).to.equal(true) | |
expect(spy.calledOnce).to.equal(true) | |
expect(resSpy.calledOnce).to.equal(true) | |
expect(resSpy.calledWith(specificPost)).to.equal(true) | |
findOne.restore() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment