Skip to content

Commit

Permalink
feat(combineEpics): verbose combineEpics returned function name (#480)
Browse files Browse the repository at this point in the history
Combined functions always return `<anonymous>` which is not very useful. This Commit updates the name of the combined epics to the following:

> combineEpics(epic1, epic2)

style(combineEpics): shorter code

style(combineEpics): fix linting

fallback to '<anonymous>'

add unit tests
  • Loading branch information
Bamieh authored and jayphelps committed May 9, 2018
1 parent 592da27 commit 816a916
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/combineEpics.js
Expand Up @@ -3,8 +3,8 @@ import { merge } from 'rxjs';
/**
Merges all epics into a single one.
*/
export const combineEpics = (...epics) => (...args) =>
merge(
export const combineEpics = (...epics) => {
const merger = (...args) => merge(
...epics.map(epic => {
const output$ = epic(...args);
if (!output$) {
Expand All @@ -13,3 +13,8 @@ export const combineEpics = (...epics) => (...args) =>
return output$;
})
);

return Object.defineProperty(merger, 'name', {
value: `combineEpics(${epics.map(epic => epic.name || '<anonymous>').join(', ')})`,
});
};
24 changes: 24 additions & 0 deletions test/combineEpics-spec.js
Expand Up @@ -71,4 +71,28 @@ describe('combineEpics', () => {
rootEpic();
}).to.throw('combineEpics: one of the provided Epics "epic2" does not return a stream. Double check you\'re not missing a return statement!');
});

describe('returned epic function name', () => {
const epic1 = () => 'named epic';
const epic2 = () => 'named epic';
const epic3 = () => 'named epic';

it('should name the new epic with `combineEpics(...epic names)`', () => {
const rootEpic = combineEpics(epic1, epic2);

expect(rootEpic).to.have.property('name').that.equals('combineEpics(epic1, epic2)');
});

it('should annotate combined anonymous epics with `<anonymous>`', () => {
const rootEpic = combineEpics(() => 'anonymous', epic2);

expect(rootEpic).to.have.property('name').that.equals('combineEpics(<anonymous>, epic2)');
});

it('should include all combined epic names in the returned epic', () => {
const rootEpic = combineEpics(epic1, epic2, epic3);

expect(rootEpic).to.have.property('name').that.equals('combineEpics(epic1, epic2, epic3)');
});
});
});

0 comments on commit 816a916

Please sign in to comment.