2016-10-03 70 views
2

我有一个组件需要一个函数类型prop,有没有人试图浅化一个需要函数定义的组件?如何在酶中定义一个函数类型prop?

handleClick: React.PropTypes.func.isRequired

这是函数的定义

handleClick: function(){ 
    this.props.handleClick(this.props.conversation) 
     } 

这是我的测试

wrapper=shallow(<ConversationItem conversation={conversation1} active={true}/>); 

怎么会在我的测试酶使用此功能道具???

回答

1

我使用模拟测试。 Jest/Sinon有嘲弄的支持。测试使用玩笑和酶活写入的一个例子是:

describe('Add',() => { 
    let add; 
    let onAdd; 

    beforeEach(() => { 
    onAdd = jest.fn(); 
    add = mount(<Add onAdd={onAdd} />); 
    }); 

    it('Button click calls onAdd',() => { 
    const button = add.find('button').first(); 
    const input = add.find('input').first(); 
    input.simulate('change', { target: { value: 'Name 4' } }); 
    button.simulate('click'); 
    expect(onAdd).toBeCalledWith('Name 4'); 
}); 

}); 

的更多信息可以在CodeMentor tutorial找到。

相关问题