【问题标题】:TypeError: Cannot read property 'bind' of undefined - ReactJS - Testing LibraryTypeError:无法读取未定义的属性“绑定” - ReactJS - 测试库
【发布时间】:2019-05-03 15:21:16
【问题描述】:

我正在为容器上的渲染部分编写测试,测试按钮是否正确渲染。以下是使用过的代码

it('should render translation inputs', () => {
            const wrapper = shallow(<COAForwardTranslations store ={storeData}/>)
            expect(wrapper.find('.btn-primary').length).toBe(1);

      });

我收到“TypeError: Cannot read property 'bind' of undefined”,你能帮帮我吗,我已经重新安装了

npm install --save-dev jest
npm install --save-dev babel-jest regenerator-runtime

但我仍然得到错误。

仅检查按钮是否在容器上正确呈现,我做错了什么。

class COAForwardTranslations extends Component
{
    constructor(props, context) {
        super(props, context);
        this.state = {
                SegmentMapRequest : {
                BranchCode: '',
                GroupId: '',
                GlCompanyCode: '',
                GlAccount: '',
                SourceMode: '',
                SourceSystem: '',
                JournalCategory: '',
                EffectiveDate: ''
            },
            segmentMapResponse:{}
        }
        this.handleRequestChange = this.handleRequestChange.bind(this);
        this.handleForwardTranslation = this.handleForwardTranslation.bind(this);
        this.handleReset = this.handleReset.bind(this);
    }

    handleRequestChange(e){
        let inputName = e.target.name;
        let inputValue = e.target.value;
        let requestCopy = Object.assign({}, this.state.SegmentMapRequest);
        requestCopy[inputName] = inputValue;
        this.setState({SegmentMapRequest: requestCopy});
    }
    handleForwardTranslation(e)
    {
        e.preventDefault();
        this.props.getForwardTranslation(this.state.SegmentMapRequest);
    }
handleReset()
    {
        this.setState({SegmentMapRequest: {
            BranchCode: '',
                GroupId: '',
                GlCompanyCode: '',
                GlAccount: '',
                SourceMode: '',
                SourceSystem: '',
                JournalCategory: '',
                EffectiveDate: ''
            },
            segmentMapResponse : {}
        });
    }

【问题讨论】:

  • 你能显示使用.bind的代码吗?
  • 我已经添加了代码。之前应该已经包含了。
  • 您在课堂上没有handleReset 函数。这就是为什么它抱怨它。从构造函数中移除this.handleReset = this.handleReset.bind(this); 或引入handleReset 函数。

标签: reactjs react-redux enzyme


【解决方案1】:

问题出在

this.handleReset = this.handleReset.bind(this);

this.handleReset 不存在于您的代码中,因此当您尝试对其调用 .bind() 时,它会引发该错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-16
    • 2019-05-07
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2022-07-22
    • 2017-08-13
    相关资源
    最近更新 更多