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

Disabled property not rendered correctly #517

Closed
hptk opened this issue Jul 28, 2016 · 7 comments
Closed

Disabled property not rendered correctly #517

hptk opened this issue Jul 28, 2016 · 7 comments

Comments

@hptk
Copy link

hptk commented Jul 28, 2016

First of all I'm using chai-enzyme, but the nature of the error I'm experiencing suggests to me that the issue lies with the rendering and not the assertions.

Code:

import React, { Component } from 'react';
import cx from 'classnames';

class CarInsuranceEditor extends Component {
  constructor() {
    super();
    this.state = {
      disabled: true
    };
  }

  handleSubmit = () => {};

  render() {
    const disabled = this.state.disabled;
    return (
      <div>
        <button
          disabled={disabled}
          onClick={this.handleSubmit}
          className={cx('stb-btn', disabled && 'disabled')}
        >
          Commit
        </button>
      </div>
    );
  }
}

export default CarInsuranceEditor;

Tests:

import React from 'react';
import { shallow } from 'enzyme';
import CarInsuranceEditor from './CarInsuranceEditor.js';

describe('<CarInsuranceEditor />', () => {
  it('should disable button by default', () => {
    const wrapper = shallow(<CarInsuranceEditor />);
    const commitButton = wrapper.find('button');
    expect(commitButton).to.be.disabled();
  });

  it('should enable button on state change', () => {
    const wrapper = shallow(<CarInsuranceEditor />);
    const commitButton = wrapper.find('button');
    expect(commitButton).to.be.disabled();
    console.log(wrapper.state());
    wrapper.setState({ disabled: !wrapper.state().disabled });
    console.log(wrapper.state());
    expect(commitButton).to.not.be.disabled();
  });
});

test output:

 <CarInsuranceEditor />
    ✓ should disable button by default
{ disabled: true }
{ disabled: false }
    1) should enable button on state change
...
AssertionError: expected the node in <CarInsuranceEditor /> not to be disabled
     HTML:     
     <button disabled="" class="stb-btn disabled">Commit</button>

It seems to me like an error that disabled={false} in the react rendering code results in disabled="". In the browser DOM the state successfully changes and the button no longer has the disabled property at all. Also note that the class name has not changed from disabled either (should not be there), which is even harder for me to understand.

@ljharb
Copy link
Member

ljharb commented Jul 28, 2016

After you call wrapper.setState, you need to call wrapper.update().

@hptk
Copy link
Author

hptk commented Jul 28, 2016

Still fails for me, no change in behaviour or output

@ljharb
Copy link
Member

ljharb commented Jul 28, 2016

I think after the update, you'll need to re-find the button you want to check.

@hptk
Copy link
Author

hptk commented Jul 28, 2016

Passes now. Thanks.

Is this intended and/or good behaviour? As someone having used this lib for a few weeks only it was natural to me that when the component was actually rerendered (as the console output proves?) the wrapper would reflect this.

@ljharb
Copy link
Member

ljharb commented Jul 28, 2016

The component doesn't re-render automatically when you set state, is all. Separately, once it's rerendered, previous references to wrapper children are invalid, and need to be refetched.

There's been some discussion about setState automatically triggering update, but you'd still need to re-find the button.

@hptk
Copy link
Author

hptk commented Jul 28, 2016

I would as mentioned find it natural for update to be triggered automatically.

The need to re-find makes more sense, refreshing references when you remove old and add new components/data/something is an action you do all the time in a lot of languages/situations so that's not new or unreasonable.

I do also realise that my understanding is limited and that I do not know all the consequences of auto-triggering update. There's probably good reasons for it not being default.

@ljharb
Copy link
Member

ljharb commented Jul 28, 2016

I think #360 might be relevant, and should both help answer questions as well as be the right place to ask further :-)

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