jeudi 4 février 2016

How to test methods and state defined on the parent component

Consider the following react code:

export default class parentComponent extends Component {

    someFunc() {
        /* lots of logic */

        this.setState({
            foo: bar
        });
    }

    render() {
        return(
           <div className="parent">
             <ChildComponent this.onSomeEvent={this.someFunc.bind(this)}>    </ChildComponent>
</div>
)
  }
}

What is the best way to test someFunc? I have two questions:

  1. How can I actually call the someFunc Method to test against it? Shallow rendering just gives me the output of the render. How do you go about testing a method that's usually called by a child component when you only have access to the component you're testing with shallow rendering?

  2. All of the shallow rendering examples I've looked at show how to test a component based on props passed to it. The only way I can think to test the parent component is to examine it's state as methods are called - because those methods update the state. Can examining state be done with shallow rendering? I can't find any examples of this.

Aucun commentaire:

Enregistrer un commentaire