使用Jest

时间:2019-03-19 04:56:31

标签: javascript jestjs

我需要方法上的帮助以及如何对javascript函数进行测试,并且其中包含if循环。

我的代码如下:

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." };
  }
}

我如何用Jest编写上面的测试用例

1 个答案:

答案 0 :(得分:1)

您可以像这样处理所有情况:

vector