Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shallow rendering: setState after setProps calls componentWillReceiveProps #1746

Closed
maxkostow opened this issue Aug 9, 2018 · 4 comments
Closed

Comments

@maxkostow
Copy link

maxkostow commented Aug 9, 2018

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

@ljharb
Copy link
Member

ljharb commented Aug 9, 2018

Interesting, thanks for the report.

@ljharb
Copy link
Member

ljharb commented Aug 11, 2018

(Possibly related to #1742)

@ljharb ljharb changed the title Shallow rendering: setState after setProps calls componenetWillReceiveProps Shallow rendering: setState after setProps calls componentWillReceiveProps Aug 11, 2018
@ghost
Copy link

ghost commented Aug 12, 2018

I'd be happy to submit a PR regarding this issue! Can probably submit in a week or two.

@ljharb
Copy link
Member

ljharb commented Aug 21, 2018

Fixed in #1779.

@ljharb ljharb closed this as completed Aug 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants