开玩笑/测试馆无法在嵌套组件中找到内容

发布于 2025-02-12 20:31:07 字数 2385 浏览 0 评论 0原文

我有以下form具有两个嵌套组件的组件(input and code> engreting

import React from 'react';

export default function Form() {
  return (
    <div>
      <p>Form</p>
      <Greeting />
      <Input label="first name" id="firstname" />
      <Input label="last name" id="lastname" />
    </div>
  );
}

function Input({ label, id }: { label:string, id:string }) {
  return (
    <div>
      <label htmlFor={id}>{label}</label>
      <input id={id} />
    </div>
  );
}

function Greeting() {
  return <div>Hello</div>;
}

) /react 测试:

test('All input fields are displayed', allFieldsAreDisplayed);
test('Greeting displayed', greetingDisplayed);

function allFieldsAreDisplayed() {
  render(<Form />);
  const firstNameInput = screen.getByText('first name');
  expect(firstNameInput).toBeInTheDocument();
}

function greetingDisplayed() {
  render(<Form />);
  const greeting = screen.getByText('Hello');
  expect(greeting).toBeInTheDocument();
}

但是,当我运行测试以查看是否显示两者时,我会收到以下错误消息:

 TestingLibraryElementError: Unable to find an element with the text: first name. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.   

    Ignored nodes: comments, <script />, <style />
    <body />

      12 | function allFieldsAreDisplayed() {
      13 |   render(<Form />);
    > 14 |   const firstNameInput = screen.getByText('first name');
         |                                 ^
      15 |   expect(firstNameInput).toBeInTheDocument();
      16 | }
      17 |

and::

TestingLibraryElementError: Unable to find an element with the text: Hello. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.        

Ignored nodes: comments, <script />, <style />
<body />

  18 | function greetingDisplayed() {
  19 |   render(<Form />);
> 20 |   const greeting = screen.getByText('Hello');
     |                           ^
  21 |   expect(greeting).toBeInTheDocument();
  22 | }
  23 |

I have the following Form component with two nested components (Input and Greeting):

import React from 'react';

export default function Form() {
  return (
    <div>
      <p>Form</p>
      <Greeting />
      <Input label="first name" id="firstname" />
      <Input label="last name" id="lastname" />
    </div>
  );
}

function Input({ label, id }: { label:string, id:string }) {
  return (
    <div>
      <label htmlFor={id}>{label}</label>
      <input id={id} />
    </div>
  );
}

function Greeting() {
  return <div>Hello</div>;
}

And I have this Jest and @testing-library/react test:

test('All input fields are displayed', allFieldsAreDisplayed);
test('Greeting displayed', greetingDisplayed);

function allFieldsAreDisplayed() {
  render(<Form />);
  const firstNameInput = screen.getByText('first name');
  expect(firstNameInput).toBeInTheDocument();
}

function greetingDisplayed() {
  render(<Form />);
  const greeting = screen.getByText('Hello');
  expect(greeting).toBeInTheDocument();
}

However, when I run the test to see if both are displayed, I get the below error messages:

 TestingLibraryElementError: Unable to find an element with the text: first name. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.   

    Ignored nodes: comments, <script />, <style />
    <body />

      12 | function allFieldsAreDisplayed() {
      13 |   render(<Form />);
    > 14 |   const firstNameInput = screen.getByText('first name');
         |                                 ^
      15 |   expect(firstNameInput).toBeInTheDocument();
      16 | }
      17 |

And:

TestingLibraryElementError: Unable to find an element with the text: Hello. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.        

Ignored nodes: comments, <script />, <style />
<body />

  18 | function greetingDisplayed() {
  19 |   render(<Form />);
> 20 |   const greeting = screen.getByText('Hello');
     |                           ^
  21 |   expect(greeting).toBeInTheDocument();
  22 | }
  23 |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文