Introduction
While testing react components, a developer may come across some components that interact with DOM APIs, need to test components wrapped in higher-order components, or require the entire lifecycle to test the component thoroughly. Full DOM Rendering is perfectly ideal for such use cases.
Full DOM rendering requires that an entire DOM API is available at the global scope. That means that you must run it in an environment similar to a browser environment. If you don't want to run your tests inside a browser, depending on a library called jsdom is the recommended approach for using mount. jsdom is essentially a headless browser implemented entirely in JavaScript.
While writing a test, it is worth noting that, unlike shallow or static rendering, full rendering mounts the component in the DOM, meaning the tests can affect each other if they are all using the same DOM. So if necessary, use .unmount() or something similar for cleanup.
React Wrapper API
Wrapper components surround unknown components and provide a default structure to display the child components. React provides Wrapper API, which is helpful to test user interface (UI) elements that users can repeatedly use throughout a design, like modals, template pages, and information tiles.
The React Wrapper API contains these multiple useful methods, which return the given object:
- .find(selector) => ReactWrapper: Finds every node in the render tree that matches the selector you provide.
- .findWhere(predicate) => ReactWrapper: Finds every node in the render tree that returns true for the predicate function you provide.
- .filter(selector) => ReactWrapper: Removes nodes in the current wrapper that do not match the selector you provide.
- .filterWhere(predicate) => ReactWrapper: Removes nodes in the current wrapper that do not return true for the predicate function you provide.
- .contains(node) => Boolean: Returns whether or not a node you give is somewhere in the render tree.
- .hasClass(className) => Boolean: Returns boolean for whether or not the current root node has the class name or not gave.
- .is(selector) => Boolean: Returns boolean for whether or not the current node matches a selector you provide.
- .not(selector) => ReactWrapper: Remove boolean for nodes in the current wrapper that match the selector you provide. It is inverse of .filter().
- .children() => ReactWrapper: Returns a wrapper with all the children nodes of the current wrapper.
- .parents() => ReactWrapper: Returns a react wrapper with all the parents of the current node.
- .parent() => ReactWrapper: Returns a react wrapper with the direct parent of the current node.
- .closest(selector) => ReactWrapper: Returns a react wrapper with the first ancestor of the current node to match the selector you provide.
- .text() => String: Returns a string for the current render tree's text nodes.
- .get(index) => ReactWrapper: ​​Returns the node of the current wrapper at the index you provide.
- .at(index) => ReactWrapper: Returns a react wrapper of the node of the current wrapper at the index you provide.
- .first() => ReactWrapper: Returns a wrapper of the current wrapper’s first node.
- .last() => ReactWrapper: Returns a wrapper of the current wrapper’s last node.
- .state([key]) => Any: Returns the root component’s state.
- .props() => Object: Return’s the root component’s props.
- .prop(key) => Any: Return’s the root component’s named prop.
- .simulate(event[, data]) => ReactWrapper: Simulates an event on the current node and returns a wrapper.
- .setState(nextState) => ReactWrapper: Sets the state of the root component manually.
- .setProps(nextProps) => ReactWrapper: Sets the props of the root component manually.
- .instance() => ReactComponent: Returns the root component’s instance.
- .update() => ReactWrapper: Calls .forceUpdate() on the instance of the root component and returns a wrapper.
- .type() => String|Function: Returns the wrapper’s type of the current node.
- .forEach(fn) => ReactWrapper: Iterates through every node of the current react wrapper and executes the function you provide.
- .map(fn) => Array: Maps nodes’ current array to another array.
- reduce(fn[, initialValue]) => Any: Reduces the nodes’ current array to a value.
- reduceRight(fn[, initialValue]) => Any: Reduces the nodes’ current array to a value, from right to left.
- some(selector) => Boolean: Returns boolean for whether or not any of the nodes in the wrapper match the selector you provide.
- someWhere(predicate) => Boolean: Returns boolean for whether or not any of the nodes in the wrapper pass the predicate function you provide.
- every (selector) => Boolean: Returns boolean for whether or not all of the nodes in the wrapper match the selector you provide.
- everyWhere(selector) => Boolean: Returns boolean for whether or not any of the nodes in the wrapper pass the predicate function you provide.






