Skip to content

Latest commit

 

History

History
193 lines (133 loc) · 7.53 KB

mount.md

File metadata and controls

193 lines (133 loc) · 7.53 KB

Full Rendering API (mount(...))

Full DOM rendering is ideal for use cases where you have components that may interact with DOM apis, or may require the full lifecycle in order to fully test the component (ie, componentDidMount etc.)

Full DOM rendering requires that a full DOM API be available at the global scope. This means that it must be run in an environment that at least "looks like" a browser environment. If you do not want to run your tests inside of a browser, the recommended approach to using mount is to depend on a library called jsdom which is essentially a headless browser implemented completely in JS.

import { mount } from 'enzyme';

describe('<Foo />', () => {

  it('calls componentDidMount', () => {
    spy(Foo.prototype, 'componentDidMount');
    const wrapper = mount(<Foo />);
    expect(Foo.prototype.componentDidMount.calledOnce).to.equal(true);
  });

  it('allows us to set props', () => {
    const wrapper = mount(<Foo bar="baz" />);
    expect(wrapper.props().bar).to.equal("baz");
    wrapper.setProps({ bar: "foo" });
    expect(wrapper.props().bar).to.equal("foo");
  });

  it('simulates click events', () => {
    const onButtonClick = spy();
    const wrapper = mount(
      <Foo onButtonClick={onButtonClick} />
    );
    wrapper.find('button').simulate('click');
    expect(onButtonClick.calledOnce).to.equal(true);
  });

});

mount(node[, options]) => ReactWrapper

Arguments

  1. node (ReactElement): The node to render
  2. options (Object [optional]):
  • options.context: (Object [optional]): Context to be passed into the component
  • options.attachTo: (DOMElement [optional]): DOM Element to attach the component to.
  • options.childContextTypes: (Object [optional]): Merged contextTypes for all children of the wrapper.

Returns

ReactWrapper: The wrapper instance around the rendered output.

ReactWrapper API

Find every node in the render tree that matches the provided selector.

Find every node in the render tree that return true for the provided predicate function.

Remove nodes in the current wrapper that do not match the provided selector.

Remove nodes in the current wrapper that do not return true for the provided predicate function.

Returns whether or not a given node or array of nodes is somewhere in the render tree.

Returns whether or not the current root node has the given class name or not.

Returns whether or not the current node matches a provided selector.

Remove nodes in the current wrapper that match the provided selector. (inverse of .filter())

Get a wrapper with all of the children nodes of the current wrapper.

Returns a new wrapper with child at the specified index.

Get a wrapper with all of the parents (ancestors) of the current node.

Get a wrapper with the direct parent of the current node.

Get a wrapper with the first ancestor of the current node to match the provided selector.

Returns a CheerioWrapper of the current node's subtree.

Returns a string representation of the text nodes in the current render tree.

Returns a static HTML rendering of the current node.

Returns the node at the provided index of the current wrapper.

Returns a wrapper of the node at the provided index of the current wrapper.

Returns a wrapper of the first node of the current wrapper.

Returns a wrapper of the last node of the current wrapper.

Returns the state of the root component.

Returns the context of the root component.

Returns the props of the root component.

Returns the named prop of the root component.

Returns the key of the root component.

Simulates an event on the current node.

Manually sets state of the root component.

Manually sets props of the root component.

Manually sets context of the root component.

Returns the instance of the root component.

A method that un-mounts the component.

A method that re-mounts the component.

Calls .forceUpdate() on the root component instance.

Returns a string representation of the current render tree for debugging purposes.

Returns the type of the current node of the wrapper.

Iterates through each node of the current wrapper and executes the provided function

Maps the current array of nodes to another array.

Reduces the current array of nodes to a value

Reduces the current array of nodes to a value, from right to left.

Returns whether or not any of the nodes in the wrapper match the provided selector.

Returns whether or not any of the nodes in the wrapper pass the provided predicate function.

Returns whether or not all of the nodes in the wrapper match the provided selector.

Returns whether or not all of the nodes in the wrapper pass the provided predicate function.

Returns a wrapper of the node that matches the provided reference name.

Unmount the component from the DOM node it's attached to.