Created
January 15, 2017 21:14
-
-
Save mpj/c55dc66bc2cfd389dbbd25ab5092d4f3 to your computer and use it in GitHub Desktop.
Code from the "Dependency Injection Basics" episode of Fun Fun Function
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 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 = () => { | |
return Promise.resolve({ | |
json: () => Promise.resolve({ | |
results: [ | |
{ name: 'fluffykins' } | |
] | |
}) | |
}) | |
} | |
getAnimals(fakeFetch, 12345) | |
.then(result => { | |
assert(result.name === 'fluffykins') | |
done() | |
}) | |
}) | |
}) |
I did some more thingking and this fileStorage.save(fileStream, {user, folder})
strikes me as a bit odd. why do the filesStorage need the user
and folder
. Is it somehow part of the location that the fileStream
will bes saved at?
Also there is some asymmetri here. storeDocument
gets raw data as arguments, except for the stream object. but the access method userAccess.canWrite(user, folder)
takes full-blown object. there might be some answers hidden in this odd relationship.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mpj and @koresar:
If i understand this code correctly:
then
boom.forbidden()
is more or less a constant which we don't need to replace with a stub, we are actually very interested in exactly this result, right?Other observations about the code is that we can use composition of the Promise Monad and write alot of it without needing Promises at all:
This part would be super simple to test...
Other things... that the original code does in the same function is fetching data from databases, looking up Access policies, and writing the data to a stream.
checking the access:
I have used currying here to help with configuration, so that the user of the function at runtime don't need to know which userAccess is being used.
saving the actual file:
combining them:
and the final product:
so the factory would look something like this:
some of the interfaces feels klunky, and they could be a written abit better with reader monads and cofunctors, depending on how the two db objects are related.
Also note that the real factory would be simpler since it would be using other factories also that the tests would be simpler since they would only care about the dependencies one level down.
So the real benefit here is that the
checkAccsessAndSaveStream
function don't need to know about the databases or thefileStorage
. You could say that these dependensies juped over the parents of the user and made them simpler.I also have the feeling that there would be even more code that could have been written without needing to know that it was async.
what do you think?