私はそれをクリックイベントのためにアクティブにするために、内部コンポーネントの状態にアクセスする必要があります。私の問題はを使用するとこれを許可しません。shallow
here私はdiveを使用してFormコンポーネントをフェッチし、Formから再び到達する必要のあるButtonコンポーネントを取得しようとしましたが、Formコンポーネントの長さがゼロのため、テストケースが失敗し続けるという問題があります。酵素 - 内部成分の状態を設定するには?
酵素:3.1.0
酵素アダプタ反応-15:私はEnzymeにかなり新しいです1.0.1"
、すべてのヘルプは、おかげ
を理解されるであろうcontactus.test.js:
test('It should simulate submit action ',()=>{
let contactUs = shallow(<ContactUs />);
sinon.spy(ContactUs.prototype, 'submitMessage');// Verify this method call
let form = contactUs.find(Form)
expect(form.length).toBe(1);//Failing over here
let button = form.dive().find(Button);
expect(button.length).toBe(1);
button.setState({disabled : false});//Need to achieve this
expect(button).toBeDefined();
expect(button.length).toBe(1);
expect(button.props().label).toBe('SEND MESSAGE');
button.find('a').get(0).simulate('click');
expect(ContactUs.prototype.submitMessage).toHaveProperty('callCount',
1);
});
contactus.js:
import React, {Component,PropTypes} from 'react';
import Form from './form';
import {sendSubscriptionMessage} from '../../network';
import Button from '../Fields/Button';
export default class ContactUs extends Component {
constructor(props) {
super(props);
this.state = {
contactData: {}
}
}
onChangeHandler(event) {
let value = event.target.value;
this.state.contactData[event.target.name] = value;
}
submitMessage(event) {
event.preventDefault();
sendSubscriptionMessage(this.state.contactData);
}
render() {
return (<div className = "row pattern-black contact logo-container" id = "contact">
<div className = "container" >
<h2 className = "sectionTitle f-damion c-white mTop100" >
Get in Touch!
<Form onChangeHandler = {
this.onChangeHandler.bind(this)
} >
<Button onClick = {
this.submitMessage.bind(this)
}
className = "gradientButton pink inverse mTop50"
label = "SEND MESSAGE"/>
</Form> </div>
</div>
);
}
}
ヘルプを得る機会を最大限に活用するには、コードを問題の核とし、テストのコードを提供することをお勧めします。 –