【问题标题】:.text() returning "<Button />" instead of the button's text.text() 返回“<Button />”而不是按钮的文本
【发布时间】:2021-08-22 14:46:41
【问题描述】:

基本上是标题中的内容。我正在为其编写测试的组件中有以下代码:

&lt;Button&gt;Edit&lt;/Button&gt; // &lt;- Material UI Button component

我的测试中的这一行失败了:

expect(wrapper.find(Button).text()).to.equal("Edit");

出现此错误:

assert.strictEqual(received, expected)

Expected value to strictly be equal to:
  "Edit"
Received:
  "<Button />"

Message:
  expected '<Button />' to equal 'Edit'

知道会发生什么吗?

【问题讨论】:

  • 代替.text() 尝试使用.value()
  • @NirjalPaudel 我得到TypeError: wrapper.find(...).value is not a function
  • 我认为您应该更新问题以获得更多信息。试试console.log( wrapper.find(Button) ) 看看它的所有属性和方法。我想你会在那里找到答案

标签: javascript jestjs enzyme


【解决方案1】:

我认为您正在尝试做的事情是这样的:

import { render, within } from '@testing-library/react';
import {Button} from '../components'

it('renders a button with "Edit" label', () => {
  const { container } = render(<Button />);
  const { getByText } = within(container);
  expect(getByText('Edit')).toBeInTheDocument();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2014-01-22
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    相关资源
    最近更新 更多