0

I am new in Unit Testing for React. I have a question regarding the if statement. I need test the function of componentwillreceiveprops, I know I can use spy = sinon.stub(component.props,"componentwillreceiveprops") method to let spy be called.

But in my component, there is an if statement, such as

componentwillreceiveprops(nextProps){
    if(this.props.name!==nextProps.name) {
        this.setState({
            oldstate: newstate
        })
    }
}

I can make the componentWillReceiveProps function called. If I remove the if statement, the setState function can be called either. But what I have in my component contains a if statement, it is not called when I run the unit testing. Is there any way to make this if statement called? Thank you in advance.

Pranay Tripathi
  • 1,614
  • 1
  • 16
  • 24
H.Alex
  • 13
  • 5
  • Sorry for my typo, should be: this.setState({ oldstate: newstate }); inside. – H.Alex Nov 16 '18 at 01:24
  • Notice that componentWillReceiveProps is deprecated. getDerivedStateFromProps that it's supposed to be replaced with is easier to test. – Estus Flask Nov 16 '18 at 06:51

1 Answers1

1

componentwillreceiveprops should be componentWillReceiveProps

Tyler McGinnis
  • 34,836
  • 16
  • 72
  • 77