[英]how to access props in mocha test in react
我正在尝试在我当前的测试用例中访问道具,但是一旦我从包装器 class 中获得一个实例,道具就会得到 null ,它没有通过测试用例
我尝试了 props() 方法来访问道具,但到目前为止还没有运气。 有没有其他方法可以访问道具并在摩卡咖啡中进行测试?
下面是我的代码
import React from 'react'
const Button = ({children,label,onClick,type,btnClass,disabled,..rest})=>{
let button = onClick ? (
<button type={type} className={btnClass} onClick={onClick}
disabled={disabled} {...rest}>
<span className="flex flex-row justify-center">
{children}
{label}
</span>
</button>
): (<button type={type} className={btnClass}
disabled={disabled} {...rest}>
<span className="flex flex-row justify-center">
{children}
{label}
</span>
</button>)
return <span>{button}</span>
};
export default Button;
测试用例:
import {mount} from 'enzyme';
import {expect} from 'chai'
describe("button component", () => {
it("props should be available", () => {
let props = {
label: 'default',
onClick: ()=>{return;}
};
const wrapper = mount(<Button {...props} />);
expect(wrapper.instance().props.label).to.be(props.label);
});
});
您是否尝试过使用shallow
而不是mount
? 并替换to.equal
为toEqual
。
import {shallow} from 'enzyme';
import {expect} from 'chai';
describe("button component", () => {
it("props should be available", () => {
let props = {
label: 'default',
onClick: ()=>{return;}
};
const wrapper = shallow(<Button {...props} />);
expect(wrapper.instance().props.label).toEqual(props.label);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.