Created
May 3, 2017 17:12
-
-
Save thephilip/5d4485127d512807c06d90b0655057b4 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
// Dependency Injection | |
const assert = require('assert') | |
function getAnimals(fetch, id) { | |
return fetch('http://api.animalfarmgame.com/animals/' + id) | |
.then(response => response.json()) | |
.then(data => data.results[0]) | |
} | |
describe('getAnimals', () => { | |
it('calls fetch with the correct url', () => { | |
const fakeFetch = url => { | |
assert( url === 'http://api.animalfarmgame.com/animals/123') | |
return new Promise(function(resolve) { | |
}) | |
} | |
getAnimals(fakeFetch, 123) | |
}) | |
it('parses the response of fetch correctly', (done) => { | |
const fakeFetch = url => { | |
return new Promise.resolve({ | |
json: () => Promise.resolve({ | |
results: [ { name: 'fluffykins' } ] | |
}) | |
}) | |
} | |
getAnimals(fakeFetch, 12345) | |
.then(result => { | |
assert(result.name == 'fluffykins') | |
done() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment