微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Reactjs:TypeError:无法读取undefined的属性’propTypes’

请我为下面的reactjs页面一个unitTest.
export default class Collapsible extends React.Component {
    static propTypes = {
        title: React.PropTypes.string,children: React.PropTypes.any,};

    render() {
        const { title } = this.props;
        return (
            <details>
                <summary>{title}</summary>
                {this.props.children}
            </details>
        );
    }
}

继0730之后,我在下面写了我的测试

describe('Collapsible',()=>{
    it('works',()=>{
        let renderer = createRenderer();
        renderer.render(<Collapsible title="MyTitle"><span>HEllo</span></Collapsible>);
        let actualElement = renderer.getRenderOutput();
        let expectedElement = (<details><summary>title</summary>Details</details>);
        expect(actualElement).toEqual(expectedElement);                     
    });
});

但是,我的测试是在上面的标题中抛出错误,我怀疑我在Collapsible上的道具(即标题和孩子)没有从测试中分配.请问我该如何解决这个问题?非常感谢任何帮助或指导.

解决方法

谢谢你的时间.事实证明我错误地将Collapsible导入测试文件.以下是我之前的导入方式
import React from 'react';
import expect from 'expect';
import {createRenderer} from 'react-addons-test-utils';
import { Collapsible }  from '../Collapsible.js';

改为之后

import React from 'react';
import expect from 'expect';
import {createRenderer} from 'react-addons-test-utils';
import Collapsible  from '../Collapsible';

它似乎工作.我以前导入Collapsible作为现有的变量/对象.通过阅读文档和很少的教程,我意识到.

原文地址:https://www.jb51.cc/js/155201.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐