Skip to content

Instantly share code, notes, and snippets.

@rodoabad
Last active August 7, 2016 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodoabad/55c1eba75c041bff4314e2b4e909c3a2 to your computer and use it in GitHub Desktop.
Save rodoabad/55c1eba75c041bff4314e2b4e909c3a2 to your computer and use it in GitHub Desktop.
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