Skip to content

Commit ac3f206

Browse files
committed
fix(empty-call): Call could not return nothing
Call functions could not return `undefined` or `[]` without throwing an error. Now both are valid output from a call function in which it will assume that nothing has to be subscribed / then'd to and go along its merry way processing. Closes #166
1 parent ff878af commit ac3f206

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/run/call/runCallAction.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,27 @@ function runCallAction(matchAndPath, routerInstance, callPath, args,
4545
// Required to get the references from the outputting jsong
4646
// and pathValues.
4747
map(function(res) {
48+
4849
// checks call for isJSONG and if there is jsong without paths
4950
// throw errors.
5051
var refs = [];
5152
var values = [];
5253

5354
// Will flatten any arrays of jsong/pathValues.
54-
var callOutput = res.reduce(function(flattenedRes, next) {
55-
return flattenedRes.concat(next);
56-
}, []);
55+
var callOutput = res.
56+
57+
// Filters out any falsy values
58+
filter(function(x) {
59+
return x;
60+
}).
61+
reduce(function(flattenedRes, next) {
62+
return flattenedRes.concat(next);
63+
}, []);
64+
65+
// An empty output from call
66+
if (callOutput.length === 0) {
67+
return [];
68+
}
5769

5870
var refLen = -1;
5971
callOutput.forEach(function(r) {

test/unit/core/call.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,47 @@ var CallNotFoundError = require('./../../../src/errors/CallNotFoundError');
1515
var CallRequiresPathsError = require('./../../../src/errors/CallRequiresPathsError');
1616

1717
describe('Call', function() {
18+
it('should be able to return nothing from a call', function(done) {
19+
var router = new R([{
20+
route: 'a.b',
21+
call: function(callPath, args) {
22+
return undefined;
23+
}
24+
}]);
25+
26+
var onNext = sinon.spy();
27+
router.
28+
call(['a', 'b']).
29+
doAction(onNext, noOp, function() {
30+
expect(onNext.calledOnce, 'onNext called once').to.be.ok;
31+
expect(onNext.getCall(0).args[0]).to.deep.equals({
32+
jsonGraph: {},
33+
paths: []
34+
});
35+
}).
36+
subscribe(noOp, done, done);
37+
});
38+
39+
it('should be able to return empty array from a call', function(done) {
40+
var router = new R([{
41+
route: 'a.b',
42+
call: function(callPath, args) {
43+
return [];
44+
}
45+
}]);
46+
47+
var onNext = sinon.spy();
48+
router.
49+
call(['a', 'b']).
50+
doAction(onNext, noOp, function() {
51+
expect(onNext.calledOnce, 'onNext called once').to.be.ok;
52+
expect(onNext.getCall(0).args[0]).to.deep.equals({
53+
jsonGraph: {},
54+
paths: []
55+
});
56+
}).
57+
subscribe(noOp, done, done);
58+
});
1859

1960
it('should bind "this" properly on a call that tranverses through a reference.', function(done) {
2061
var values = [];

0 commit comments

Comments
 (0)