Skip to content

Commit

Permalink
More test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradhan V committed Oct 29, 2017
1 parent 777354d commit dcd3b2e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 20 deletions.
4 changes: 3 additions & 1 deletion chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ Chain.prototype._callFunctions = function (delay, functions, ...args) {
//
const create = function (functionArray, callback, ...args) {
const fchain = new Chain(functionArray, callback);
fchain.setFunctionArgs(args);
if (args.length !== 0) {
fchain.setFunctionArgs(args);
}
return fchain;
};
const createWithDelay = function (delay, functionArray, callback, ...args) {
Expand Down
20 changes: 7 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,29 @@
const chain = require('./chain');
//
const repeatFunction = function (actualFunction, continueCallback, ...args) {
let result = null;
// set result to the previous result
let [result] = args.slice(-1);
let repeat = false;
let error = null;
try {
result = actualFunction(...args);
} catch (err) {
if (continueCallback) {
// TODO: use the callback and decide to stop.
continueCallback(err, result);
}
error = err;
}
let repeat = false;
if (continueCallback) {
repeat = continueCallback(null, result);
repeat = continueCallback(error, result);
}
if (repeat) {
const fcc = chain.create();
fcc.setFunctions([repeatFunction]);
// args.unshift(actualFunction);
// fcc.setFunctionArgs([actualFunction, continueCallback, resultCallback].concat(result));
fcc.setFunctionArgs([result]);
return fcc;
}
return result;
};
//
const createRepeatFunctionChain = function (actualFunction, continueCallback, resultCallback, ...args) {
const fchain = chain.createWithDelay(
-1, [repeatFunction], resultCallback,
actualFunction, continueCallback, ...args
);
const fchain = chain.create([repeatFunction], resultCallback, actualFunction, continueCallback, ...args);
return fchain;
};
//
Expand Down
111 changes: 105 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,38 @@ const noReturnValueNoArgs = () => {
};
//
/* eslint no-invalid-this: 0 */
/* eslint no-undefined: 0 */
// returns an array [functionArray, callback], this is passed to the create chain function
const getCreateArgs = function (done, fdelay, args) {
let createargs = [];
if (fdelay) {
createargs.push(fdelay);
}
} /* else if (this._functionArray) {
createargs.push(undefined);
}*/
// dont add undefined for missing delay.
// this method is called by create chain without delay too.
if (this._functionArray) {
createargs.push(this._functionArray);
} else if (!this.noCallback) {
createargs.push(undefined);
}
if (!this.noCallback) {
createargs.push((finalVal) => {
expect(this.expected).to.equal(finalVal);
done();
});
} else if (args) {
createargs.push(undefined);
createargs.push(undefined);
}
if (args) {
// createargs.push(...args);
createargs = createargs.concat(args);
}
return createargs;
};
const ERROR = new Error('Some Error');
// TODO: add destructive test case.. send non-functions in the function array (string, int...)
const tests = [
{
Expand All @@ -66,6 +77,27 @@ const tests = [
getCreateArgs,
'expected': 'abcd',
'functionArgs': ['a']
}, {
'description': 'function has error',
'_functionArray': [
addNextCharWrap, () => {
throw ERROR;
}, addNextCharWrap
],
getCreateArgs,
'expected': ERROR,
'functionArgs': ['a']
}, {
'description': 'function has error, no callback',
'_functionArray': [
addNextCharWrap, () => {
throw ERROR;
}, addNextCharWrap
],
getCreateArgs,
'noCallback': true,
'expected': ERROR,
'functionArgs': ['a']
}, {
'description': 'check no return values',
/* eslint no-empty-function: 0 */
Expand All @@ -91,10 +123,19 @@ const tests = [
getCreateArgs,
'expected': 'abcd',
'functionArgs': ['p']
}, {
'description': 'function returns another chain, with functions having own args',
'_functionArray': [
addNextCharWrap, addNextCharWrap,
() => chain.create([addNextCharWrap, addNextCharWrap], () => {}, 'l'),
addNextCharWrap, addNextCharWrap
],
getCreateArgs,
'expected': 'lmnop',
'functionArgs': ['o']
}
];
/* eslint no-magic-numbers:0 */
// TODO: add variants for create with delay, create with function args during create
describe('#tests, args while start', () => {
tests.forEach((test) => {
it(test.description, (done) => {
Expand Down Expand Up @@ -141,7 +182,6 @@ describe('#tests, args while create', () => {
if (test.getCreateArgs && test.functionArgs) {
it(test.description, (done) => {
let fchain = null;
/* eslint no-undefined: 0 */
fchain = chain.create(...test.getCreateArgs(done, undefined, test.functionArgs));
fchain.startCalls();
if (test.noCallback) {
Expand All @@ -156,9 +196,7 @@ describe('#tests, args while create, with delay', () => {
// other cases are covered already before
if (test.getCreateArgs && test.functionArgs) {
it(test.description, (done) => {
let fchain = null;
/* eslint no-undefined: 0 */
fchain = chain.createWithDelay(...test.getCreateArgs(done, delay, test.functionArgs));
const fchain = chain.createWithDelay(...test.getCreateArgs(done, delay, test.functionArgs));
fchain.startCalls();
if (test.noCallback) {
done();
Expand All @@ -167,6 +205,20 @@ describe('#tests, args while create, with delay', () => {
}
});
});
describe('#tests, args while start and create, with delay', () => {
tests.forEach((test) => {
if (test.getCreateArgs && test.functionArgs) {
it(test.description, (done) => {
const fchain = chain.createWithDelay(...test.getCreateArgs(done, delay, test.functionArgs));
fchain.startCalls(...test.functionArgs);
if (test.noCallback) {
done();
}
});
}
});
});

describe('#chain', () => {
it('use set functions', (done) => {
const fchain = chain.create();
Expand Down Expand Up @@ -241,6 +293,53 @@ describe('#repeat()', () => {
}, 0);
fc.startCalls();
});
it('repeat function call, no continue callback function', (done) => {
const fc = chain.createRepeatFunctionChain(addOneWrap, undefined, (finalVal) => {
expect(finalVal).to.equal(1);
done();
}, 0);
fc.startCalls();
});
it('repeat function call, function throws error, dont continue', (done) => {
globalObject.value = 0;
const fc = chain.createRepeatFunctionChain((num, result) => {
if (result === 3) {
throw ERROR;
}
return addOneWrap(num, result);
}, (error) => error === null, (finalVal) => {
expect(finalVal).to.equal(3);
done();
}, 0);
fc.startCalls();
});
it('repeat function call, function throws error, no continue callback function', (done) => {
globalObject.value = 0;
const fc = chain.createRepeatFunctionChain((num, result) => {
if (result === 3) {
throw ERROR;
}
return addOneWrap(num, result);
}, undefined, (finalVal) => {
expect(finalVal).to.equal(1);
done();
}, 0);
fc.startCalls();
});
it('repeat function call, function throws error, continue', (done) => {
globalObject.value = 0;
const fc = chain.createRepeatFunctionChain((num, result) => {
globalObject.value += 1;
if (globalObject.value === 2) {
throw ERROR;
}
return addOneWrap(num, result);
}, (error, result) => result !== 8, (finalVal) => {
expect(finalVal).to.equal(8);
done();
}, 0);
fc.startCalls();
});
it('repeat function call, with delay', (done) => {
const fc = chain.createRepeatFunctionChain(addOneWrap, (error, result) => result !== 10, (finalVal) => {
expect(finalVal).to.equal(10);
Expand Down

0 comments on commit dcd3b2e

Please sign in to comment.