I have a wrapper function defer that delays implementation of any other function:
const playerPerformance = {
goals: 33,
assists: 21,
points(penaltiesEarned) {
console.log((this.goals * 2) + (this.assists * 1.5) + (penaltiesEarned * 1.5));
},
};
const defer = (func, ms) => {
return function() {
setTimeout(() => func.call(this, ...arguments), ms);
};
};
const deferredPointsDisplay = defer(playerPerformance.points, 1000);
deferredPointsDisplay.call( { goals: 18, assists: 19 }, 7); // 75
I want to run a unit test which will test what data is logged into a console but I fail to take control of the timer, so the test will be passed.
it('should return 75', () => {
const playerPerformance = {
goals: 33,
assists: 21,
points(penaltiesEarned) {
console.log((this.goals * 2) + (this.assists * 1.5) + (penaltiesEarned * 1.5));
},
};
const consoleSpy = jest.spyOn(console, 'log');
const deferredPointsDisplay = defer(playerPerformance.points, 1000);
deferredPointsDisplay.call( { goals: 18, assists: 19 }, 7);
expect(consoleSpy).toHaveBeenCalledWith(75);
});
Please help me develop this unit test to pass the test.