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 .setState bug from v3.4.2 occurs in v3.6.0 when render return an array #1837

Closed
almeida-samuel opened this issue Sep 25, 2018 · 2 comments

Comments

@almeida-samuel
Copy link

The Component

import React from 'react';
class DankComponent extends React.Component {
  constructor(props) {
    super(props)
    this.state = {dankField: true}
    this.toogleDankField = this.toogleDankField.bind(this);
  }

  toogleDankField() {
    this.setState({dankField: !this.state.dankField})
  }

  render() {
    return [
      <button onClick={this.toogleDankField}>Dank Button</button>,
      <NotDankComponent dankField={this.state.dankField}/>
    ];
  }
}

The Test

import React from 'react';
import { shallow } from 'enzyme';
import DankComponent from 'components/DankComponent';

describe('<DankComponent />', () => {
  let wrapper;
  let props = {};

  const mountComponent = (props = {}) => {
    wrapper = shallow(<DankComponent {...props} />);
  };

  describe('at initial mount', () => {
    beforeEach(() => {
      mountContainer(props);
    });

    describe('when button is clicked', () => {
      it('change state value of dankField to true', () => {
        const button = wrapper.at(0).at(0);
        button.simulate('click')
        expect(wrapper).toMatchSnapshot();
      });
    })
  });
});

The Error

 Method “setState” is only meant to be run on a single node. 2 found instead.
 toogleDankField() {
  this.setState({dankField: !this.state.dankField})
        ^
 }

Expected behavior
The test should pass.

Desktop (please complete the following information):

  • "enzyme": "3.6.0"
  • "enzyme-adapter-react-16": "1.5.0"
  • "react": "16.5.2"
  • "jest": "20.0.4"
@almeida-samuel
Copy link
Author

The error vanishes if we wrap the array content returned by render in a <div> or any other component.

@almeida-samuel almeida-samuel changed the title shallow .setState bug from v3.4.2 occurs when render return an array shallow .setState bug from v3.4.2 occurs in 3.6.0 when render return an array Sep 25, 2018
@almeida-samuel almeida-samuel changed the title shallow .setState bug from v3.4.2 occurs in 3.6.0 when render return an array shallow .setState bug from v3.4.2 occurs in v3.6.0 when render return an array Sep 25, 2018
@ljharb
Copy link
Member

ljharb commented Sep 25, 2018

enzyme does not yet support components rendering non-nodes (arrays and strings). See #1149.

In the meantime, wrap it in a React.Fragment / <>.

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

No branches or pull requests

2 participants