How to properly unit test a function with a switch calling other functions with a mockist approach in Jest

Viewed 16

So I'm trying to go with a mockist approach of testing this convert function in a temperature converter class. But I can't quite figure out how to properly test it without also testing the functions of the cases.

While I COULD just call the function and set the expected result to be the converted temperature, that wouldn't be a true unit test with the approach I'm trying to take.

I want to test the switch, not the functions in the cases.

How would I go about doing that using Jest?

convert(toScale: string) {
  switch (`${this.#system} to ${toScale}`) {
    case "C to F":
      return this.celsiusToFahrenheit(this.#value);
    case "C to K":
      return this.celsiusToKelvin(this.#value);
    case "F to C":
      return this.fahrenheitToCelsius(this.#value);
    case "F to K":
      return this.fahrenheitToKelvin(this.#value);
    case "K to C":
      return this.kelvinToCelsius(this.#value);
    case "K to F":
      return this.kelvinToFahrenheit(this.#value);
    default:
      break;
  }
}

0 Answers
Related