【问题标题】:React+Enzyme+Redux Error: Method “simulate” is meant to be run on 1 node. 0 found insteadReact+Enzyme+Redux 错误:方法“simulate”意味着在 1 个节点上运行。找到了 0 个
【发布时间】:2021-03-09 01:31:50
【问题描述】:

我是 Jest 和 Enzyme 的新手,我正在尝试找出解决此错误的方法。我已经尝试了所有可能的解决方案(还有那些在 stackoverflow 上的)。

如果这不是正确的方法,我将不胜感激。

我想测试什么?

一个组件(名为 Company),它有一个“添加”按钮,单击它会打开一个模式(由状态控制)。 因此,我想测试在按钮单击时,用于控制模态可见性的状态是否从“false”变为“true”。

到目前为止我的方法:

浏览文档和stackoverflow上的很多答案,我已经根据对我有用的方法修改了我的代码

Company.js

import React from 'react';
import { Modal Col, Label, Row, Card, CardBody, CardColumns, CardHeader } from 'reactstrap';
import { connect } from 'react-redux';
import MUIDataTable from "mui-datatables";

const columns = [/*table data headers */ ]

export class Company extends React.Component {

    state = {
        show: false,
     
        }
        
    }
  
    handleShow = () => {...}

   
    componentDidMount() {
        this.fetchCompanies();
    }

    
    fetchCompanies = () => { /*API calls */ }       

    render() {
const options = {
            filterType: 'checkbox',
            selectableRows:false,
            customToolbar:() => {
                return <Button className="btn btn-primary"  id="companyAdd" onClick={this.handleShow}>Add</Button>
            }
        }
        return (
            <div>
            <Col xs="12" lg="12">
                <MUIDataTable
                columns={columns}
                options={options}
            />
            </Col>
            <Col xs="12" lg="12">
                <Modal isOpen={this.state.showChangePwd}></Modal>                
        </div>
        );
    }

}

const mapDispatchToProps = dispatch => ({..});

function mapStateToProps(state) {..}

export default connect(mapStateToProps,mapDispatchToProps)(Company);

我正在尝试在选项对象(内部渲染)中找到按钮

Company.test.js

import ReactDOM from "react-dom";
import {Company} from "../Company";
import { cleanup} from "@testing-library/react";

import "@testing-library/jest-dom";
import { configure, mount, shallow , render} from "enzyme";

afterEach(cleanup)
it("renders without crashing", () => {
   expect(render(<Example/>))

})

it("renders button correctly", () => {
    const onButtonClickMock = jest.fn();
    const wrapper = shallow(<Example updateSelectedDashboard={onButtonClickMock}/>)
    expect(wrapper.state('show')).toEqual(false);
    const button = wrapper.find('companyAdd');
    button.simulate('click') 
    expect(wrapper.state('show')).toEqual(true);
})

我也尝试过.dive(),即使没有成功。

【问题讨论】:

    标签: unit-testing react-redux jestjs enzyme react-testing-library


    【解决方案1】:

    你忘记了你的 CSS 选择器 :)

    const button = wrapper.find('#companyAdd');
    

    【讨论】:

    • 这条线去哪儿了?
    • 这是一个按钮的定义,他在上面模拟点击。只是一个拼写错误的更正。我认为这很明显。
    • 它仍然给我同样的错误:(我什至尝试过&lt;button data-test ='companyAdd'&gt;Add &lt;/button&gt; and tried to find in test code with const wrapper = component.find([data-test='companyAdd']);`即使这也不起作用
    • 好的,所以我刚才注意到您使用shallow,在这种情况下find() 只会深入一层。您必须mount 您的组件或使用dive()(同样,可能还不够,使用console.log 或debug() 来检查此类元素)。
    • 我尝试过使用mountdebug() 确实帮助我找到了一个带有“companyAdd”类的按钮,但错误仍然存​​在。
    猜你喜欢
    • 2019-07-20
    • 1970-01-01
    • 2019-07-14
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2016-09-19
    • 2021-03-10
    相关资源
    最近更新 更多