Skip to content

ReactWrapper::setState() can only be called on the root #1289

@wongjiahau

Description

@wongjiahau

Preface

I had went through #814 and #361 , but I still don't get the answer I want.

Explanation

I know you may say that in practice you should only test a child component in isolation, but I'm in a situation where the child component cannot be rendered without the parent component.
For example, because I'm using React Material-UI, I got to mount the element-to-be-tested inside a theme provider, if not error will be thrown during mounting :

const wrapper = mount(
        <MuiThemeProvider>
            <SubjectListView subjects={subjects}/>
        </MuiThemeProvider>);

Problem

I need to set the state of SubjectListView but then I got this ReactWrapper::setState() can only be called on the root error.

Conclusion

I really hope I can call setState() on child element, if not I have to wrap all my components with a <MuiThemeProvider> which is obviously not a good thing.

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