简体   繁体   English

我的数据测试 ID 在那里,但我的测试找不到它反应酶开玩笑单元测试

[英]my data test id is there but my test cannot find it react enzyme jest unit tests

I am still new to unit testing with jest and enzyme, I have a data-test-id and I am trying to make a unit test using that.我对玩笑和酶的单元测试仍然很陌生,我有一个 data-test-id 并且我正在尝试使用它进行单元测试。 Yet, I get an error saying that the data-test-id was not found.但是,我收到一条错误消息,提示找不到数据测试 ID。

I have the following我有以下

import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { configure, mount, shallow } from 'enzyme';
import { shallowToJson } from 'enzyme-to-json';

import ConfirmationModal from '../src/components/GlobalForm/ConfirmationModal/ConfirmationModal';

configure({ adapter: new Adapter() });

describe('ConfirmationModal', () => {
  it('should be defined', () => {
    expect(ConfirmationModal).toBeDefined();
  });

  it('should render correctly', () => {
    const tree = mount(<ConfirmationModal />);
    expect(shallowToJson(tree)).toMatchSnapshot();
  });

  it('should have a cancel button', () => {
    const wrapper = shallow(<ConfirmationModal />);
    const ConfirmationModalComponent = wrapper.find("[data-test-id='cancel-submit-btn']");
    expect(ConfirmationModalComponent.length).toBe(1);
  });
});

Even my snapshot test shows that I have this data-test-id甚至我的快照测试也显示我有这个 data-test-id

exports[`ConfirmationModal should render correctly 1`] = `
<confirmationModal>
  <Styled(div)>
    <div
      className="css-d2ogfy"
    >
      <Styled(div)>
        <div
          className="css-ywnjte"
        >
          Confirm your submission
        </div>
      </Styled(div)>
      <Styled(p)>
        <p
          className="css-1v1a1rr"
        >
          The submission of your changes will directly affect the call center with which they are assigned.
        </p>
      </Styled(p)>
      <Styled(p)>
        <p
          className="css-1v1a1rr"
        >
          Are you sure that you want to proceed with these changes?
        </p>
      </Styled(p)>
      <Styled(div)>
        <div
          className="css-wqsxq7"
        >
          <Button
            dataTestId="cancel-submit-btn"
            name="Cancel"
          >
            <Styled(button)
              data-test-id="cancel-submit-btn"
            >
              <button
                className="css-2ba12r"
                data-test-id="cancel-submit-btn"
              >
                Cancel
              </button>
            </Styled(button)>
          </Button>
          <Button
            dataTestId="confirm-submit-btn"
            name="Confirm"
          >
            <Styled(button)
              data-test-id="confirm-submit-btn"
            >
              <button
                className="css-2ba12r"
                data-test-id="confirm-submit-btn"
              >
                Confirm
              </button>
            </Styled(button)>
          </Button>
        </div>
      </Styled(div)>
    </div>
  </Styled(div)>
</confirmationModal>
`;

but my test results with the error:但我的测试结果出现错误:

ConfirmationModal › should have a cancel button ConfirmationModal › 应该有一个取消按钮

expect(received).toBe(expected) // Object.is equality Expected: 1 Received: 0

How????如何???? it's right there...它就在那儿……

Answering because this question appeared first when I googled data-testid and enzyme.回答这个问题是因为我在谷歌上搜索 data-testid 和酵素时首先出现了这个问题。

Try this instead:试试这个:

wrapper.find({ "data-testid": "cancel-submit-btn" }).simulate("click");

This blog post covers react enzyme snapshot testing in detail.这篇博文详细介绍了反应酶快照测试。 I've added the example above in case the blog post is ever removed.我已经添加了上面的示例,以防博客文章被删除。

not sure what your ConfirmationModal component looks like, but my guess is that it wrapped in some styling as your snapshot suggests: <Styled(div)>不确定您的ConfirmationModal组件是什么样子,但我猜它包装在一些样式中,正如您的快照所暗示的那样: <Styled(div)>

Between your snapshot test and the one where you are trying to find data-test-id , the first one mounts the component when the second one only shallow render it, therefor my guess is your last test, if you check the snapshot, will indeed not have the whole component but instead probably something like that:在您的快照测试和您尝试查找data-test-id的那个之间,第一个在第二个仅浅渲染它时安装组件,因此我猜是您的最后一个测试,如果您检查快照,确实会没有整个组件,而是可能是这样的:

<confirmationModal>
  <Styled(div) />
</confirmationModal>

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

相关问题 存根读取单元测试与酶和玩笑反应 - stub fetch unit test react with enzyme and jest 单元测试反应成分,酶 - unit test a react component- jest, enzyme 单元测试-使用Jest和酶在React中链接 - Unit test - Link in React using Jest & Enzyme 开酶玩笑,为什么在我的 React 组件测试中没有触发 `toHaveBeenCalled`? - Jest with Enzyme, why is `toHaveBeenCalled` not triggering in my React Component test? 酶/反应单元测试 - Enzyme / React Unit test 类型错误:无法读取未定义的属性“getParam”。 使用玩笑和酶反应原生单元测试? - TypeError: Cannot read property 'getParam' of undefined. for react native unit test using jest and enzyme? 玩笑和酶单元测试:TypeError:无法读取未定义的属性“推送” - Jest and Enzyme unit test: TypeError: Cannot read property 'push' of undefined 在React上使用Jest和Enzyme调用clearInterval的单元测试方法 - Unit test method that calls clearInterval with Jest and Enzyme on React 如何使用Enzyme和Jest在单元测试中检查嵌套的React组件的值 - How to check the value of a nested React component in a unit test with Enzyme and Jest 如何使用带有Jest的酶在React Component中编写DataTable的单元测试? - How to write unit test for DataTable in React Component using Enzyme with Jest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM