Commit 9d808180 by Wolfram Kriesing

Inject the fetch function too. Now we have control over all the dependencies of this function.

parent aaf3278c
const { assertThat, equalTo } = require('hamjest');
// const { fetch } = require('./backend');
let fetch;
const loadStudent = ({ studentId }, dependencies) => {
const cache = dependencies.cache;
const fetch = dependencies.fetch;
if (!cache[studentId]) {
cache[studentId] = fetch({ studentId });
}
......@@ -17,9 +17,9 @@ describe('Load a student', () => {
assertThat(student, equalTo('student 42'));
});
it('fetch the student if not in the cache', () => {
let emptyCache = {};
fetch = () => 'student 23';
const student = loadStudent({ studentId: 23 }, { cache: emptyCache });
const emptyCache = {};
const dependencies = { cache: emptyCache, fetch: () => 'student 23' };
const student = loadStudent({ studentId: 23 }, dependencies);
assertThat(student, equalTo('student 23'));
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment