react-shallow-testutils
Replacement for TestUtils when using React's shallow rendering.
npm install react-shallow-testutils
React Versions
Version | React version |
---|---|
0.4.0 | 0.13 |
0.5.0 | 0.14 |
2.0.0 | 15 |
3.0.0 | 15.5.4 |
3.0.1 | ^15.5.4 & ^16 |
Usage
All the functions are exported separately. This means you can pull in just the ones you want like this:
; ;
However, if you want everything then you can do this:
; ShallowTestUtils;
isComponentOfType
Returns whether a ReactElement
is of a particular type.
boolean
;;; { return <div />;} { return <OtherComponent />;} const renderer = ; const tree1 = renderer;; const tree2 = renderer;;
isDOMComponent
Returns whether the supplied ReactElement
is a DOM component or not
boolean
;;; { return <div />;} const renderer = ; const tree = renderer;;
findAll
Traverses the tree and returns all elements that satisfy the function test
. A lot of the other functions are implemented in terms of this one.
array
;;; { return <div> <span /> <span /> <span /> </div> ;} const renderer = ; const tree = renderer;const spans = ;;
findAllWithType
Finds all instances of elements in the tree with a type that matches
type
. This is like both React's scryRenderedDOMComponentsWithTag
and scryRenderedComponentsWithType
as you can supply a component class or a DOM tag.
array
;;; { return <div />;} { return <div> <span /> <MyOtherComponent /> <span /> </div> ;} const renderer = ; const tree = renderer;;;
findWithType
Find only one instance of an element in the tree with a type that matches
type
. This is like both React's findRenderedDOMComponentWithTag
and findRenderedComponentWithType
as you can supply a component class or a DOM tag. It will throw an error if not exactly one instance is found.
ReactElement
;;; { return <div />;} { return <div> <span /> <MyOtherComponent /> <span /> </div> ;} const renderer = ; const tree = renderer;not;;
findAllWithClass
Finds all elements in the tree with a className
prop that matches className
. This is different to React's scryRenderedDOMComponentsWithClass
in that it will check all components and not just DOM components.
You can pass a className
like test-class.test-class--modified
to find an element that has both classes.
array
;;; { return <div />;} { return <div> <span className="my-span" /> <MyOtherComponent /> <span className="my-span" /> </div> ;} const renderer = ; const tree = renderer;;;
findWithClass
Find only one element in the tree with a className
prop that matches className
. This is different to React's findRenderedDOMComponentWithClass
in that it will check all components and not just DOM components. It will throw an error if not exactly one instance is found.
You can pass a className
like test-class.test-class--modified
to find an element that has both classes.
ReactElement
;;; { return <div className="my-div" />;} { return <div> <span className="my-span" /> <MyOtherComponent /> <span className="my-span" /> </div> ;} const renderer = ; const tree = renderer;not;; // More than 1
findWithRef
Find only one element in the tree with a ref
prop that matches ref
. This is only useful for a ref
that has been defined as a string and not as a function.
ReactElement
;;; { return <div> <span className="my-span" /> <div className="div-ref-class" ref="div-ref" /> <span className="my-span" /> </div> ;} const renderer = ; const tree = renderer;;