带三元操作符的单元测试切换用例



我已经为下面的开关案例编写了测试用例,但我不确定如何使用三元操作符编写案例

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

相关内容

  • 没有找到相关文章

最新更新