react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / __tests__ / ReactComponent-test.js
81155 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @emails react-core9*/1011"use strict";1213var React;14var ReactTestUtils;1516var reactComponentExpect;1718describe('ReactComponent', function() {19beforeEach(function() {20React = require('React');21ReactTestUtils = require('ReactTestUtils');22reactComponentExpect = require('reactComponentExpect');23});2425it('should throw on invalid render targets', function() {26var container = document.createElement('div');27// jQuery objects are basically arrays; people often pass them in by mistake28expect(function() {29React.render(<div></div>, [container]);30}).toThrow(31'Invariant Violation: _registerComponent(...): Target container ' +32'is not a DOM element.'33);3435expect(function() {36React.render(<div></div>, null);37}).toThrow(38'Invariant Violation: _registerComponent(...): Target container ' +39'is not a DOM element.'40);41});4243it('should throw when supplying a ref outside of render method', function() {44var instance = <div ref="badDiv" />;45expect(function() {46instance = ReactTestUtils.renderIntoDocument(instance);47}).toThrow();48});4950it('should throw when attempting to hijack a ref', function() {51var Component = React.createClass({52render: function() {53var child = this.props.child;54this.attachRef('test', child);55return child;56}57});5859var childInstance = ReactTestUtils.renderIntoDocument(<span />);60var instance = <Component child={childInstance} />;6162expect(function() {63instance = ReactTestUtils.renderIntoDocument(instance);64}).toThrow(65'Invariant Violation: attachRef(test, ...): Only a component\'s owner ' +66'can store a ref to it.'67);68});6970it('should support refs on owned components', function() {71var innerObj = {}, outerObj = {};7273var Wrapper = React.createClass({7475getObject: function() {76return this.props.object;77},7879render: function() {80return <div>{this.props.children}</div>;81}8283});8485var Component = React.createClass({86render: function() {87var inner = <Wrapper object={innerObj} ref="inner" />;88var outer = <Wrapper object={outerObj} ref="outer">{inner}</Wrapper>;89return outer;90},91componentDidMount: function() {92expect(this.refs.inner.getObject()).toEqual(innerObj);93expect(this.refs.outer.getObject()).toEqual(outerObj);94}95});9697var instance = <Component />;98instance = ReactTestUtils.renderIntoDocument(instance);99});100101it('should not have refs on unmounted components', function() {102var Parent = React.createClass({103render: function() {104return <Child><div ref="test" /></Child>;105},106componentDidMount: function() {107expect(this.refs && this.refs.test).toEqual(undefined);108}109});110var Child = React.createClass({111render: function() {112return <div />;113}114});115116var instance = <Parent child={<span />} />;117instance = ReactTestUtils.renderIntoDocument(instance);118});119120it('should correctly determine if a component is mounted', function() {121var Component = React.createClass({122componentWillMount: function() {123expect(this.isMounted()).toBeFalsy();124},125componentDidMount: function() {126expect(this.isMounted()).toBeTruthy();127},128render: function() {129return <div/>;130}131});132133var element = <Component />;134135var instance = ReactTestUtils.renderIntoDocument(element);136expect(instance.isMounted()).toBeTruthy();137});138139it('should know its simple mount depth', function() {140var Owner = React.createClass({141render: function() {142return <Child ref="child" />;143}144});145146var Child = React.createClass({147render: function() {148return <div />;149}150});151152var instance = <Owner />;153instance = ReactTestUtils.renderIntoDocument(instance);154expect(instance._mountDepth).toBe(0);155expect(instance.refs.child._mountDepth).toBe(1);156});157158it('should know its (complicated) mount depth', function() {159var Box = React.createClass({160render: function() {161return <div ref="boxDiv">{this.props.children}</div>;162}163});164165var Child = React.createClass({166render: function() {167return <span ref="span">child</span>;168}169});170171var Switcher = React.createClass({172getInitialState: function() {173return {tabKey: 'hello'};174},175176render: function() {177var child = this.props.children;178179return (180<Box ref="box">181<div182ref="switcherDiv"183style={{184display: this.state.tabKey === child.key ? '' : 'none'185}}>186{child}187</div>188</Box>189);190}191});192193var App = React.createClass({194render: function() {195return (196<Switcher ref="switcher">197<Child key="hello" ref="child" />198</Switcher>199);200}201});202203var root = <App />;204root = ReactTestUtils.renderIntoDocument(root);205206expect(root._mountDepth).toBe(0);207expect(root.refs.switcher._mountDepth).toBe(1);208expect(root.refs.switcher.refs.box._mountDepth).toBe(2);209expect(root.refs.switcher.refs.switcherDiv._mountDepth).toBe(4);210expect(root.refs.child._mountDepth).toBe(5);211expect(root.refs.switcher.refs.box.refs.boxDiv._mountDepth).toBe(3);212expect(root.refs.child.refs.span._mountDepth).toBe(6);213});214});215216217