类型“ tech”的参数不能分配给类型“ PropsWithChildren <Props>”的参数

时间:2020-06-22 23:37:39

标签: reactjs jestjs react-props

尝试测试功能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>'.

1 个答案:

答案 0 :(得分:1)

该测试错误地使用默认导入而不是命名。因此getProblemType被分配了组件,并期望被传递道具。因此,错误。通过插入花括号来解决:

 import { getProblemType } from "components/Common/Authorization";