Skip to content

Commit

Permalink
Add TestUtils find/scry DOMComponent by prop
Browse files Browse the repository at this point in the history
Fixes facebook#1616, which asked for finding the ID. This is more general.
  • Loading branch information
chenglou committed Jun 6, 2014
1 parent eebcf9f commit c3bb082
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,39 @@ var ReactTestUtils = {

/**
* Finds all instance of components in the rendered tree that are DOM
* components with the class name matching `className`.
* components with the prop matching `propValue`.
* @return an array of all the matches.
*/
scryRenderedDOMComponentsWithProp: function(root, propName, propValue) {
return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
return ReactTestUtils.isDOMComponent(inst) &&
inst.props.hasOwnProperty(propName) &&
inst.props[propName] === propValue;
});
},

/**
* Like scryRenderedDOMComponentsWithProp but expects there to be one result,
* and returns that one result, or throws exception if there is any other
* number of matches besides one.
* @return {!ReactDOMComponent} The one match.
*/
findRenderedDOMComponentWithProp: function(root, propName, propValue) {
var all = ReactTestUtils.scryRenderedDOMComponentsWithProp(
root,
propName,
propValue
);
if (all.length !== 1) {
throw new Error(
'Did not find exactly one match for ' + propName + ':' + propValue
);
}
return all[0];
},

/**
* A handy version of `scryRenderedDOMComponentsWithProp` for class.
* @return an array of all the matches.
*/
scryRenderedDOMComponentsWithClass: function(root, className) {
Expand Down Expand Up @@ -164,7 +196,6 @@ var ReactTestUtils = {
return all[0];
},


/**
* Finds all instance of components in the rendered tree that are DOM
* components with the tag name matching `tagName`.
Expand Down

0 comments on commit c3bb082

Please sign in to comment.