[react] ReactTestUtils custom typeguards

This commit is contained in:
Vincent Siao
2016-03-14 01:46:14 -07:00
parent 1917a4122a
commit 5e85e146c6
3 changed files with 31 additions and 7 deletions
+19 -2
View File
@@ -220,9 +220,12 @@ domNode = ReactDOM.findDOMNode(domNode);
// React Elements
// --------------------------------------------------------------------------
var type = element.type;
var type: React.ComponentClass<Props> = element.type;
var elementProps: Props = element.props;
var key = element.key;
var key: React.Key = element.key;
var t: React.ReactType<any>;
var name = typeof t === "string" ? t : t.displayName;
//
// React Components
@@ -543,6 +546,20 @@ var foundComponent: ModernComponent = TestUtils.findRenderedComponentWithType(
var foundComponents: ModernComponent[] = TestUtils.scryRenderedComponentsWithType(
inst, ModernComponent);
// ReactTestUtils custom type guards
var emptyElement: React.ReactElement<{}>;
if (TestUtils.isElementOfType(emptyElement, StatelessComponent)) {
emptyElement.props.foo;
}
var anyInstance: Element | React.Component<any, any>;
if (TestUtils.isDOMComponent(anyInstance)) {
anyInstance.getAttribute("className");
} else if (TestUtils.isCompositeComponent(anyInstance)) {
anyInstance.props;
}
//
// TransitionGroup addon
// --------------------------------------------------------------------------