Я получаю сообщение об ошибке при попытке протестировать функцию 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");
  });
});

Когда я передаю mock.supportType в getProblemType, я получаю ошибку ниже Argument of type '"tech"' is not assignable to parameter of type 'PropsWithChildren<Props>'.

0
magi 23 Июн 2020 в 02:37

1 ответ

Лучший ответ

Тест ошибочно использует импорт по умолчанию вместо named. Итак, getProblemType назначается компонент и ожидает передачи реквизитов. Отсюда ошибка. Исправьте, вставив фигурные скобки:

 import { getProblemType } from "components/Common/Authorization";
1
Son Nguyen 23 Июн 2020 в 01:12