Despite expectations, all these specifications perform well. Witness the absurd responses generated by the delay function. I attempted to enhance code readability but unfortunately fell short.
Test spec:
describe("delay", function () {
var chai = require("chai");
var expect = chai.expect;
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var Calculator = require('C:/BaseCalculators/Calculator');
var delay = require('C:/BaseCalculators/delay');
var calculator;
beforeEach(function () {
calculator = new Calculator();
});
it("returns a promise", function () {
var willAdd = delay( 100, calculator, 'add', [ 1, 1 ] );
expect( willAdd ).to.be.instanceOf( Promise );
expect( willAdd ).to.be.fulfilled;
});
it("delays execution", function () {
expect( delay( 1000, calculator, 'add', [ 10, 5 ] ) ).to.eventually.equal( 150 );
expect( delay( 500, calculator, 'subtract', [ 9, 5 ] ) ).to.eventually.equal( 14 );
});
it("cannot execute functions that do not exist", function () {
expect( delay( 1000, calculator, 'sqrt', [ 2, 2 ] ) ).to.be.rejected;
});
});
Calculator appears to operate smoothly with its own tests, but additional code can be added as needed. Below is the delay function which may be causing issues:
delay = function (ms, obj, methodName, params) {
var p = new Promise((resolve, reject) => {
setTimeout(() => {
var data = obj[methodName](...params);
console.log(data);
p.resolve(data);
}, ms);
}
)
return p;
};
module.exports = delay;