Search code examples
javascriptreactjsjestjsswitch-statementconditional-operator

Jest unit test switch case with ternary operator


I have written test case for below switch case, but I'm not sure how to write the case with ternary operator

export function getFormName(type, uppercase, t) {
    switch (type) {
      case "withdrawal":
        return uppercase
          ? _.upperCase("withdrawal")
          : "withdrawal";
      case "closure":
        return uppercase
          ? _.upperCase("closure") //.upperCase is from loadash
          : "closure";
      default:
        return uppercase ? _.upperCase(type) : toTitleCase(type); //toTitleCase returns first character in uppercase
    }
  }

My partial solution:

import _ from "lodash";

import { toTitleCase } from "utils"; //toTitleCase returns first character in uppercase

describe("utils/helpers", () => {
    describe("getFormName()", () => {
        it("should return withdrawal", async() => {
            const value = "withdrawal";
            const result = _.upperCase(value);
            expect(result).toEqual("WITHDRAWAL");
        });
    });
   
});

Solution

  • You don't need async for the callback function in your test cases. Please use the following 2 test cases as examples to test your getFormName() function.

    describe("utils/helpers", () => {
      describe("getFormName()", () => {
        it("should return withdrawal", () => {
          const result = getFormName("withdrawal", true, null); // please put the relevant param if necessary for t
          expect(result).toEqual("WITHDRAWAL");
        });
    
        it("should return closure", () => {
          const result = getFormName("closure", true, null); // please put the relevant param if necessary for t
          expect(result).toEqual("CLOSURE");
        });
      });
    });