enzymejs / enzymejs/enzyme

ReactWrapper::setProps() can only be called on the root

Open
#1,925 35 comments 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
19.8k
Forks
2k
PR merge metrics
No merged PRs in 30d

Description

### Current behavior

My team is trying to test a component that consumes a MobX wrapper like this:

```
let component = mount(



);
```

In this simplified example, `Awesome`'s implementation would look something like this:

```
@inject('store')
@observer
class Awesome extends React.Component {

componentDidMount() {
this.props.store.awesomePerson = this.props.name;
}

componentDidUpdate() {
this.props.store.awesomePerson = this.props.name;
}

componentDidUnmount() {
delete this.props.store.awesomePerson;
}

render() {
return

You're awesome { this.prop.name }

;
}
}
```

We're trying to test the `componentDidUpdate` method. Because we need to use a `Provider`, we can't call `setProps` on the root element. Instead, we tried to do this:

```
component.find('Awesome').setProps({ name: "Nicole" });
```

Unfortunately, this produces this error in Enzyme:

```
ReactWrapper::setProps() can only be called on the root
```

It looks like this issue has been [opened before](https://github.com/airbnb/enzyme/issues/1289) with `setState`, but doesn't seem to be working for `setProps`.

### Expected behavior

We should be able to set the props of a child component.

### Your environment

#### API

- [ ] shallow
- [x] mount
- [ ] render

#### Version

| library | version
| ------------------- | -------
| enzyme | 3.4.2
| react | 16.4.2
| react-dom | 16.4.2
| react-test-renderer | N/A
| adapter (below) | 1.2.0

#### Adapter

- [x] enzyme-adapter-react-16
- [ ] enzyme-adapter-react-16.3
- [ ] enzyme-adapter-react-16.2
- [ ] enzyme-adapter-react-16.1
- [ ] enzyme-adapter-react-15
- [ ] enzyme-adapter-react-15.4
- [ ] enzyme-adapter-react-14
- [ ] enzyme-adapter-react-13
- [ ] enzyme-adapter-react-helper
- [ ] others ( )

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.