首页 > 解决方案 > 为什么要开玩笑这个组件?

问题描述

输入组件必须有道具“名称”。
但 Jest 正在通过测试。
这没有意义。
如何解决?

class Input extends React.Component<
  {
    errorMassage: string;
    label: string;
    name: string;
    value: number | string;
    type: "text" | "number" | "price";
    onFocus: () => void;
    onChange: (name?: string, e?: MouseEvent) => void;
    autoFocus: boolean;
  },
  {}
......

import React from "react";
import ReactDOM from "react-dom";
import Input from "../Input";

describe("<Input>", () => {
  it("renders without crashing", () => {
    const div = document.createElement("div");
    ReactDOM.render(<Input />, div);
  });
});

标签: javascriptreactjstypescriptjestjs

解决方案


我认为您需要将道具设置为type InputProps = {...},然后将 React 组件设置为该类型React.Component<InputProps>

此外,我认为它通过了,因为该玩笑测试实际上只是测试它是否呈现到页面。不是它是否正确渲染。此外,您是否有任何可能基于的默认道具?


推荐阅读