jeudi 3 mai 2018

js switch statment unit test [on hold]

I have a util function like so:

export const selectOperator = (operator, n1, n2) => {
    let result;

    switch(operator) {
        case '+':
            result = n1 + n2;
            console.log(result)
        case "-":
            result = n1 - n2;
        case "*":
            result = n1 * n2;
        case "/":
            result = n1 / n2;
    }

    return result;
}

which I am trying to test like so:

it('should select the right case based on the operator', () => {
  const operators = ['+', '/', '-', '*']
  const numA = 10
  const numB = 5

  operators.forEach(el => {
    if(el == '+'){
      //selectOperator(el, numA, numB).toEqual(12)
      console.log(selectOperator('+', 10, 5))
    }
    if(el == '-'){
      //selectOperator(el, numA, numB).toEqual(12)
      //console.log(selectOperator(el, numA, numB))
    }
    if(el == '/'){
      //selectOperator(el, numA, numB).toEqual(12)
      //console.log(selectOperator(el, numA, numB))
    }
    if(el == '*'){
      //selectOperator(el, numA, numB).toEqual(12)
      //console.log(selectOperator(el, numA, numB))
    }
  })
})

the console inside the util function returns 15 and that's correct, however the console inside the test returns 2 (it seems like it's dividing 10/5)

Aucun commentaire:

Enregistrer un commentaire