I need help on approach and how do I implement test on javascript function and that has if loops inside.
My code is like below :
function calculate(obj, buttonName) {
//When AC button is pressed, we will be displaying 0 on screen, so all states go to null.
if (buttonName === "AC") {
return {
result: null,
nextOperand: null,
operator: null
};
}
if (buttonName === ".") {
if (obj.nextOperand) {
//cant have more than one decimal point in a number, dont change anything
if (obj.nextOperand.includes(".")) {
return {};
}
//else append dot to the number.
return { nextOperand: obj.nextOperand + "." };
}
//If the operand is pressed that directly starts with .
return { nextOperand: "0." };
}
}
How do I write a test case for above with Jest