Return early from a function with some data if condition is met withing child function, otherwise return 'no return'.
The test inputs 'toReturn' and 'returnWhat' MUST be passed to child funciton and SHOULD NOT be referenced in parent. No mutations of test inputs should occur.
const returnFromChild=(toReturn, returnWhat)=>{
const child=(toRet, retWhat)=>{
if(toRet){
return retWhat
}
}
const retThis = child(toReturn,returnWhat);
if(retThis!=undefined){
return retThis
}
return 'no return'
}
const chai = require("chai");
const assert = chai.assert;
describe("No Return", function() {
it("should not return string", function() {
assert.strictEqual(returnFromChild(false, 'something'), 'no return');
});
it("should not return number", function() {
assert.strictEqual(returnFromChild(false, 27), 'no return');
});
it("should not return true", function() {
assert.strictEqual(returnFromChild(false, true), 'no return');
});
it("should return false", function() {
assert.strictEqual(returnFromChild(false, false), 'no return');
});
it("should not return array", function() {
assert.strictEqual(returnFromChild(false, [1,2,3]), 'no return');
});
});
describe("Return", function() {
it("should return string", function() {
assert.strictEqual(returnFromChild(true, 'something'), 'something');
});
it("should return number", function() {
assert.strictEqual(returnFromChild(true, 27), 27);
});
it("should return true", function() {
assert.strictEqual(returnFromChild(true, true), true);
});
it("should return false", function() {
assert.strictEqual(returnFromChild(true, false), false);
});
it("should return array", function() {
assert.deepEqual(returnFromChild(true, [1,2,3]), [1,2,3]);
});
});