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 rendering: ref mock builder #721

Open
charlierudolph opened this issue Dec 9, 2016 · 4 comments
Open

shallow rendering: ref mock builder #721

charlierudolph opened this issue Dec 9, 2016 · 4 comments
Labels
API: shallow feature request Issues asking for stuff that would be semver-minor

Comments

@charlierudolph
Copy link

Just an idea I had which is I believe the minimal I need in order to be to test some functionality with refs while still being able to use shallow rendering.

describe('refMockBuilder', () => {
  it('calls it for each ref with the a wrapper for the element', () => {
    const SimpleComponent = React.createClass({
      focus() {
        this.textInput.focus();
      },
      render() {
        return (
          <div>
            <input id='name' type='text' ref={(node) => { this.textInput = node; }} />
            <button onClick={this.focus}></button>
          </div>
        );
      }
    });
    mocks = {}
    const refMockBuilder = (wrapper) => {
      return mocks[wrapper.props().id] = {focus: sinon.stub()}
    }
    const wrapper = shallow(<SimpleComponent />, { refMockBuilder });
    wrapper.find('button').simulate('click')
    expect(mocks['name'].focus).to.have.been.called
  });
});

I'm happy to try and implement this if you like the idea.

@ljharb
Copy link
Member

ljharb commented Dec 9, 2016

Why the id?

@charlierudolph
Copy link
Author

The id is just being used as an example of a way to tell refs apart (in case you had multiple)

@aweary
Copy link
Collaborator

aweary commented Dec 9, 2016

react-test-renderer has an API for this called createNodeMock. If we supported this, it would be nice if it was consistent with that API.

@jquintozamora
Copy link

Do you have any plans to support createNodeMock on enzyme mount ?

@ljharb ljharb added API: shallow feature request Issues asking for stuff that would be semver-minor labels Aug 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: shallow feature request Issues asking for stuff that would be semver-minor
Projects
None yet
Development

No branches or pull requests

4 participants