Last active
August 7, 2016 03:30
-
-
Save rodoabad/55c1eba75c041bff4314e2b4e909c3a2 to your computer and use it in GitHub Desktop.
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
import 'isomorphic-fetch'; | |
import {expect} from 'code'; | |
import {getFruits} from '../../src/fruits-repository'; | |
import sinon from 'sinon'; | |
describe('Given the fruits repository', () => { | |
let fetchStub, | |
sandbox; | |
beforeEach(() => { | |
sandbox = sinon.sandbox.create(); | |
fetchStub = sandbox.stub(global, 'fetch'); | |
}); | |
afterEach(() => { | |
sandbox.restore(); | |
}); | |
describe('when getting a collection of fruits', () => { | |
it('should call fetch', () => { | |
const expectedEndPoint = '/fruits'; | |
fetchStub.returns(Promise.resolve()); | |
getFruits(); | |
sinon.assert.calledOnce(fetchStub); | |
sinon.assert.calledWithExactly(fetchStub, expectedEndPoint); | |
}); | |
describe('and is successful', () => { | |
it('should return an array of fruits', () => { | |
const expectedFruits = [ | |
'apple', | |
'banana', | |
'pears' | |
]; | |
const json = sandbox.stub().returns(expectedFruits); | |
fetchStub.returns(Promise.resolve({json})); | |
return getFruits() | |
.then(data => { | |
expect(data).array().equal(expectedFruits); | |
}) | |
.catch(() => { | |
expect(false).true(); | |
}); | |
}); | |
}); | |
describe('and is unsuccessful', () => { | |
it('should return an error object', () => { | |
const expectedErrorObject = { | |
message: 'No fruits found.' | |
}; | |
const json = sandbox.stub().returns(expectedErrorObject); | |
fetchStub.returns(Promise.reject({json})); | |
return getFruits() | |
.then(() => { | |
expect(false).true(); | |
}) | |
.catch(error => { | |
expect(error).object().equal(expectedErrorObject); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment