Skip to content

Shallow rendering: setState after setProps calls componentWillReceiveProps #1746

@maxkostow

Description

@maxkostow

Describe the bug
On a shallow wrapper, calling setState after calling setProps triggers componenetWillReceiveProps which it should not.

I believe this has something to do with https://github.com/airbnb/enzyme/blob/9b4d0276f57e54be06aca6c3636120b3c4053310/packages/enzyme/src/ShallowWrapper.js#L361 which clones the props causing
https://github.com/facebook/react/blob/be4533af7d29f020499d3c01931e55ef8666a419/packages/react-test-renderer/src/ReactShallowRenderer.js#L183
to be true on the subsequent setState call
https://github.com/airbnb/enzyme/blob/9b4d0276f57e54be06aca6c3636120b3c4053310/packages/enzyme/src/ShallowWrapper.js#L441

To Reproduce

const React = require('react')
const Enzyme = require('enzyme') // 3.4.1
const Adapter = require('enzyme-adapter-react-16.3') // 1.0.0

Enzyme.configure({ adapter: new Adapter(), disableLifecycleMethods: true })

class A extends React.Component {
    constructor(props) {
        super(props)
        this.state = { a: 0 }
    }

    componentWillReceiveProps() {
        this.setState({ a: 1 })
    }

    render() {
        return React.createElement('div', {}, this.state.a)
    }
}

const e = Enzyme.shallow(React.createElement(A))

e.setState({ a: 2 })
console.log(e.state('a')) // logs `2`

e.setProps({})
console.log(e.state('a')) // logs `1`

e.setState({ a: 3 })
console.log(e.state('a')) // also logs `1` but should log `3`

Expected behavior
componenetWillReceiveProps should not be called after calling setState

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions