lundi 23 avril 2018

How to test a method which is resolving a promise and then changing state in React?

  1. need to test that when button is clicked and after promise resolve state.message === 'loggedIn successfully'

class Login extends Component {

constructor() {
    this.onLoginClick = this.onLoginClick.bind(this); 
}

fetchLogin() {
    return new Promise(function (resolve) {
        reollve({ success: true });
    })
}

onLoginClick() {
    let that = this;
    fetchLogin().then(function ({ success }) {
        success ?
            that.setState({ message: 'loggedIn successfully' }) :
            that.setState({ message: 'Fail' });

    })
}

render() {
    return (<div>
        <button onClick={this.onLoginClick}></button>
    </div>)
}

}

Aucun commentaire:

Enregistrer un commentaire