this.innerRef = c} />;
},
render: function() {
return (
this.wrapperRef = c}
getContent={this.getInner}
/>
);
},
componentDidMount: function() {
- // TODO: Currently new refs aren't available on initial render
- },
- componentDidUpdate: function() {
// Check .props.title to make sure we got the right elements back
- this.wrapperRef.then(function(wrapper) {
- expect(wrapper.props.title).toBe("wrapper");
- refsResolved++;
- });
- this.innerRef.then(function(inner) {
- expect(inner.props.title).toEqual("inner");
- refsResolved++;
- });
- expect(refsResolved).toBe(0);
+ expect(this.wrapperRef.props.title).toBe('wrapper');
+ expect(this.innerRef.props.title).toBe('inner');
+ mounted = true;
}
});
var instance = ;
instance = ReactTestUtils.renderIntoDocument(instance);
- instance.forceUpdate();
- expect(refsResolved).toBe(2);
+ expect(mounted).toBe(true);
+ });
+
+ it('should call refs at the correct time', function() {
+ var log = [];
+
+ var Inner = React.createClass({
+ render: function() {
+ log.push(`inner ${this.props.id} render`);
+ return ;
+ },
+ componentDidMount: function() {
+ log.push(`inner ${this.props.id} componentDidMount`);
+ },
+ componentDidUpdate: function() {
+ log.push(`inner ${this.props.id} componentDidUpdate`);
+ },
+ componentWillUnmount: function() {
+ log.push(`inner ${this.props.id} componentWillUnmount`);
+ }
+ });
+
+ var Outer = React.createClass({
+ render: function() {
+ return (
+
+ {
+ log.push(`ref 1 got ${c ? `instance ${c.props.id}` : 'null'}`);
+ }}/>
+ {
+ log.push(`ref 2 got ${c ? `instance ${c.props.id}` : 'null'}`);
+ }}/>
+
+ );
+ },
+ componentDidMount: function() {
+ log.push('outer componentDidMount');
+ },
+ componentDidUpdate: function() {
+ log.push('outer componentDidUpdate');
+ },
+ componentWillUnmount: function() {
+ log.push('outer componentWillUnmount');
+ }
+ });
+
+ // mount, update, unmount
+ var el = document.createElement('div');
+ log.push('start mount');
+ React.render(, el);
+ log.push('start update');
+ React.render(, el);
+ log.push('start unmount');
+ React.unmountComponentAtNode(el);
+
+ expect(log).toEqual([
+ 'start mount',
+ 'inner 1 render',
+ 'inner 2 render',
+ 'inner 1 componentDidMount',
+ 'ref 1 got instance 1',
+ 'inner 2 componentDidMount',
+ 'ref 2 got instance 2',
+ 'outer componentDidMount',
+ 'start update',
+ // Previous (equivalent) refs get cleared
+ 'ref 1 got null',
+ 'inner 1 render',
+ 'ref 2 got null',
+ 'inner 2 render',
+ 'inner 1 componentDidUpdate',
+ 'ref 1 got instance 1',
+ 'inner 2 componentDidUpdate',
+ 'ref 2 got instance 2',
+ 'outer componentDidUpdate',
+ 'start unmount',
+ 'outer componentWillUnmount',
+ 'ref 1 got null',
+ 'inner 1 componentWillUnmount',
+ 'ref 2 got null',
+ 'inner 2 componentWillUnmount'
+ ]);
});
it('should correctly determine if a component is mounted', function() {