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

setState on shallowWrapper needs explicit update #1451

Closed
nikhilexe opened this issue Dec 25, 2017 · 3 comments · Fixed by #1453
Closed

setState on shallowWrapper needs explicit update #1451

nikhilexe opened this issue Dec 25, 2017 · 3 comments · Fixed by #1453
Labels

Comments

@nikhilexe
Copy link

After upgrading to Enzyme V3, we started observing that now we need explicit update after setState. I read the migration guide and somehow i didn't get sense this is expected and hence raising issue here, please correct me if my understanding is wrong.

Here is the sample component

export class ComponentA { 

    public render(): JSX.Element {

        return (
            <div className={"myClass"}>
                {
                    this.state.myState ?
                        this._getComponentB() :
                        this.props.children
                }
            </div>
        );
    }

    private _getComponentB(): JSX.Element {
        return (
            <ComponentB
                text={this.props.text} />
        );
    }
}

In the test I am trying to do following, see in the setState callback I have to explicit update now to make test pass. Is this expected? I feel this is common scenario and enzyme should handle this.

let shallowWrapper = Enzyme.shallow(<ComponentA text="text" />);

shallowWrapper.setState({
                    isLoading: false
                }, () => {
			shallowWrapper.update();
			// Assertion here
		});
@isnifer
Copy link

isnifer commented Dec 26, 2017

Just got this too!

@koba04
Copy link
Contributor

koba04 commented Dec 26, 2017

What about moving your assertion to after the setState?
The following example, you don't need to call shallowWrapper.update() manually.

let shallowWrapper = Enzyme.shallow(<ComponentA text="text" />);

shallowWrapper.setState({isLoading: false});
// Assertion here

But I think it's an unexpected behavior for developers so I've created a PR to fix this. #1453

@nikhilexe
Copy link
Author

Thanks @koba04 for raising PR to fix this issue, moving out assertion after setState will work, but its good to have support of callback without any explicit update. For now I updated my test, hoping it get fixed in next version of enzyme

@ljharb ljharb added the bug label Jan 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants