From bf24c30aaa7d87e683ef781abd18702b441b6e54 Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Tue, 4 Nov 2014 12:21:55 -0800 Subject: [PATCH] Update TestUtils.findRenderedComponentWithType This function gets that actual React component rather than the element. This allows the tester to call functions like setState on the component as shown in the new test. --- react-addons/react-addons-tests.ts | 20 +++++++++++++------- react-addons/react-addons.d.ts | 5 +++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/react-addons/react-addons-tests.ts b/react-addons/react-addons-tests.ts index 4834d6e23d..7a835dfebe 100644 --- a/react-addons/react-addons-tests.ts +++ b/react-addons/react-addons-tests.ts @@ -113,21 +113,27 @@ React.addons.TestUtils.Simulate.click(node); React.addons.TestUtils.Simulate.change(node); React.addons.TestUtils.Simulate.keyDown(node, {key: "Enter"}); -var GoodbyeMessage = React.createClass({displayName: 'GoodbyeMessage', +var Greeting = React.createClass({displayName: 'Greeting', + getInitialState: function() { + return {morning: true}; + }, render: function() { - return React.DOM.div(null, "Goodbye ", (>this).props.name); + var me = >this; + return React.DOM.div(null, (me.state.morning ? "Hello" : "Goodbye "), me.props.name); } }); -React.addons.TestUtils.renderIntoDocument(GoodbyeMessage({name: "John"})); + +var root = React.addons.TestUtils.renderIntoDocument(Greeting({name: "John"})); +var greeting = React.addons.TestUtils.findRenderedComponentWithType(root, Greeting); +greeting.setState({ + morning: false +}); var isImportant: boolean; var isRead: boolean; var cx = React.addons.classSet; -var classes = cx({ +var classes: string = cx({ 'message': true, 'message-important': isImportant, 'message-read': isRead }); - - - diff --git a/react-addons/react-addons.d.ts b/react-addons/react-addons.d.ts index 35fc5f1fb9..686dd05632 100644 --- a/react-addons/react-addons.d.ts +++ b/react-addons/react-addons.d.ts @@ -93,8 +93,9 @@ declare module React { findRenderedDOMComponentWithClass(tree: ReactElement, className: string): ReactElement; scryRenderedDOMComponentsWithTag(tree: ReactElement, className: string): ReactElement[]; findRenderedDOMComponentWithTag(tree: ReactElement, tagName: string): ReactElement; - scryFindRenderedComponentsWithTag(tree: ReactElement, componentClass: Function): ReactElement[]; - findRenderedComponentWithType(tree: ReactElement, componentClass: Function): ReactElement; + scryRenderedComponentsWithTag(tree: ReactElement, componentClass: Function): ReactElement[]; + findRenderedComponentWithType

(tree: ReactElement, componentClass: ReactComponentFactory

): Component; + scryRenderedComponentsWithType

(tree: ReactElement, componentClass: ReactComponentFactory

): Component[]; } export interface SyntheticEventData {