尝试测试功能getProblemType时出现错误。
Argument of type '"tech"' is not assignable to parameter of type 'PropsWithChildren<Props>'.
下面是我的组件的代码。
interface Props {
type: SupportType;
}
export function getProblemType(srType:SupportType):ProblemTypeEnum {
switch (srType) {
case "limit":
return "LIMIT";
case "account":
return "ACCOUNT";
default:
return "TECH";
}
}
const Auth: React.FC<Props> = (props) => {
const problemType = getProblemType(props.supportType);
const supportValidate = apiCalls.callSupport(false, "123", userId, problemType, state.homeRegionName);
return (<>....</>)
};
export default Auth;
我的测试如下所示
describe("Auth tests", () => {
let mock : Props;
const runProblemTypeTest = (url: string, supportType: SupportType) => {
}
mount(
<Auth supportType={supportType}>
<div>TestChild</div>
</Authorization>
);
expect(apiCalls).toHaveBeenCalledWith(
false,
"1234",
"test",
getProblemType(supportType),
"homeRegion"
);
it("check getProblemType when support is tech", () => {
mock.supportType = "tech";
expect(getProblemType(mock.supportType)).toEqual("TECH");
});
it("check problem type passed to validate when problem type absent in the url", () => {
mock.supportType = "tech";
runProblemTypeTest("http://www.test.com", "tech");
});
});
当我在getProblemType上传递mock.supportType时,出现以下错误
Argument of type '"tech"' is not assignable to parameter of type 'PropsWithChildren<Props>'.
答案 0 :(得分:1)
该测试错误地使用默认导入而不是命名。因此getProblemType
被分配了组件,并期望被传递道具。因此,错误。通过插入花括号来解决:
import { getProblemType } from "components/Common/Authorization";