簡體   English   中英

帶有三元運算符的 Jest 單元測試開關案例

[英]Jest unit test switch case with ternary operator

我已經為下面的 switch case 編寫了測試用例,但我不確定如何用三元運算符編寫 case

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
    }
  }

我的部分解決方案:

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

您的測試用例中的回調函數不需要async 請使用以下 2 個測試用例作為示例來測試您的getFormName()函數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM