简体   繁体   English

rootInstance.findByType(“输入”); 给出预期的1个,但发现2个实例的节点类型为:“ undefined”

[英]rootInstance.findByType(“input”); Giving Expected 1 but found 2 instances with node type : “undefined”

So I have a component that shows some text fields. 因此,我有一个显示一些文本字段的组件。 One input, one select and based on the input the user uses on that select, (The value of the select) the third is shown or not. 一个输入,一个选择,并根据用户在该选择上使用的输入(选择的值)显示或不显示第三个。

Im trying to use React-Test-Renderer to test the first input field only, but Im getting the following Error: Expected 1 but found 2 instances with node type: "undefined" 我试图使用React-Test-Renderer仅测试第一个输入字段,但是我收到以下错误:预期为1,但发现2个实例的节点类型为:“ undefined”

Here is the input field code: 这是输入字段代码:

    <div>
      <div className="container">
        <h3 className="h3">Info</h3>
        <div className="row">
          <div className="col">
            <label htmlFor="test">test:</label>
            <input
              id="myID"
              value={aPropThatIsAnEmptyString}
              type="text"
              className={`form-control form-control-sm ${style.fieldsGap}`}
              onChange={e => setTest(e.target.value)}
              placeholder="test"
            />
          </div>
          <div className="col">
            <label htmlFor="ddlType">test2:</label>
            <select
              id="SelectId" 

Here is my test code: 这是我的测试代码:


 it("test input field", () => {
    const newProps = {
      something: initialState,
      dispatch: reducer
    };
    const component = TestRenderer.create(
      <ContextBR.Provider value={{ ...newProps }}>
        <ComponentWithTheFields/>
      </ContextBR.Provider>
    );
    const rootInstance = component.root;
    console.log(rootInstance);
    const inputField = rootInstance.findByType("input");

    inputField.props.onChange({ target: { value: "" } });
    expect(inputField.props.value).toBe("");

    inputField.props.onChange({ target: { value: "blue" } });
    expect(inputField.props.value).toBe("blue");
  });

IF you're wondering what ContextBR.Provider is, we used context instead of redux. 如果您想知道ContextBR.Provider是什么,我们使用上下文而不是redux。

My error message: Expected 1 but found 2 instances with node type: "undefined" 我的错误消息:预期为1,但发现2个实例的节点类型为:“ undefined”



  29 |     const rootInstance = component.root;
  30 |     console.log(rootInstance);
> 31 |     const inputField= rootInstance.findByType("input");
     |                                   ^
  32 | 
  33 |     inputField.props.onChange({ target: { value: "" } });
  34 |     expect(inputField.props.value).toBe("");

I tried ad first without the <ContextBr.Provider wrapping but that gave me Dispatch is null or undefined. 我首先尝试了广告,没有<ContextBr.Provider包装,但是这使我Dispatch为null或未定义。 Which is probably because I didnt send the context too and the component uses it. 这可能是因为我也没有发送上下文,组件也使用了它。

I was expecting it to find the input, and assert if its value is at the one I sent in, "". 我期望它找到输入,并断言其值是否为我发送的值“”。 After that I was aiming at adding duplicate code just instead of sending in "" rather send in "blue " (just as an example) then assert on that. 之后,我的目标是添加重复代码,而不是发送“”而不是发送“ blue”(仅作为示例),然后对此进行断言。

Im trying to test user experience. 我正在尝试测试用户体验。 That he can type on it and that it shows what he's typing. 他可以在上面键入内容,并显示他正在键入的内容。

From the documentation 文档中

testInstance.findByType() testInstance.findByType()

Find a single descendant test instance with the provided type. 查找具有提供的类型的单个后代测试实例。 If there is not exactly one test instance with the provided type, it will throw an error. 如果没有一个具有提供的类型的测试实例,它将抛出一个错误。

My guess is that you have more than one input field hidden in your component, causing findByType() to throw an error. 我的猜测是您的组件中隐藏了多个输入字段,从而导致findByType()引发错误。

You can pick one of the input fields by using 您可以使用来选择输入字段之一

rootInstance.findAllByType("input")[index] 

(I'm not sure why React says the type is undefined, but findAllByType() still works for me) (我不确定为什么React说类型是不确定的,但是findAllByType()仍然对我有用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM