jcgh582
jcgh582

Reputation: 909

What is the difference between enzymes shallow render and instance method

I do not understand how the following two lines are different

shallowRenderedComponent = shallow(<SomeComponent />)
shallowRenderedComponentInstance = shallowRenderedComponent.instance()

The enzyme docs are very vague and I could not find any comparison on the internet.

Thanks in advance

Upvotes: 1

Views: 547

Answers (1)

Jordan Bonitatis
Jordan Bonitatis

Reputation: 1547

shallow returns a wrapper object that has all of the methods enzyme define's in its docs (e.g., find, setProps, etc)

The instance is directly accessing an instantiated object of your React component. That is, the methods available on the instance are those that you wrote for that class in your application code.

As an example, if your React component looks like the below, your shallowRenderedComponentInstance will give you access to handleClick

class Example extends React.Component {
  handleClick = () {
    console.log("I was clicked");
  };

  render() {
    return (
      <pre>Hello, World!</pre>
    );
  }
}

Upvotes: 3

Related Questions