Skip to content

Commit

Permalink
fix(fetch-sections,fetch-topics): Prevent error because of missing se…
Browse files Browse the repository at this point in the history
…ctions or topics
  • Loading branch information
Simon Mollweide committed Dec 14, 2017
1 parent ded5d57 commit 21592ae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/fetchSections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const fetchSection = require('../fetchSection');
**/
function fetchSections(sections, { _fetchSection }) {
return new Promise(pResolve => {
// in case of there are sections
if (Object.keys(sections).length <= 0) {
pResolve({});
return;
}

pSeries(
Object.keys(sections).map((sectionKey, sectionIndex) =>
_fetchSection.bind(null, { sections, sectionKey, sectionIndex })
Expand Down
13 changes: 13 additions & 0 deletions src/fetchSections/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ describe('fetchSections', () => {
done();
});
});
it('fetch without sections', done => {
const _fetchSections = resolve(fetchSections, {
fetchSection: () => {
return new Promise(pResolve => {
pResolve({});
});
},
});
_fetchSections({}).then(results => {
expect(results).toEqual({});
done();
});
});
});
6 changes: 6 additions & 0 deletions src/fetchTopics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const fetchTopic = require('../fetchTopic');
**/
function fetchTopics({ sections, sectionKey, sectionIndex }, { _fetchTopic }) {
return new Promise(pResolve => {
// in case of there are no topics in this section
if (Object.keys(sections[sectionKey].topics).length <= 0) {
pResolve({});
return;
}

pSeries(
Object.keys(sections[sectionKey].topics).map((topicKey, topicIndex) =>
_fetchTopic.bind(null, { sections, sectionKey, sectionIndex }, { topicKey, topicIndex })
Expand Down
22 changes: 22 additions & 0 deletions src/fetchTopics/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,26 @@ describe('fetchTopics', () => {
done();
});
});
it('fetch without topics', done => {
const _fetchTopics = resolve(fetchTopics, {
fetchTopic() {
return new Promise(pResolve => {
pResolve({ a: {} });
});
},
});
const props = {
sections: {
section1: {
topics: {},
},
},
sectionKey: 'section1',
sectionIndex: 0,
};
_fetchTopics(props).then(results => {
expect(results).toEqual({});
done();
});
});
});

0 comments on commit 21592ae

Please sign in to comment.