Skip to content

Using React 16 createRef #1704

@gthomas-appfolio

Description

@gthomas-appfolio

I'm trying to reference new React 16 refs in my enzyme 3 tests so that I can call .simulate, etc on internal references.

Simplified example:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.textInput = React.createRef();
  }

  render() {
    return (
      <input ref={this.textInput} />
    );
  }
}

Test:

component = mount(<MyComponent />);
const input = component.instance().textInput.current;
input.simulate('change', { ... });

However I get errors like:

TypeError: component.instance(...).textInput.current.simulate is not a function

This is a simplified example so I'm not looking for alternate ways of finding the input - I'm curious if and how we can use and test using React 16 references?

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