Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
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");
        });
    });
   
});
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

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");
    });
  });
});
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!