react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / __tests__ / ReactPropTransferer-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;15var reactComponentExpect;1617var TestComponent;1819describe('ReactPropTransferer', function() {2021beforeEach(function() {22require('mock-modules').dumpCache();2324React = require('React');25ReactTestUtils = require('ReactTestUtils');26reactComponentExpect = require('reactComponentExpect');2728// We expect to get a warning from transferPropsTo since it's deprecated29spyOn(console, 'warn');3031TestComponent = React.createClass({32render: function() {33var result = this.transferPropsTo(34<input35className="textinput"36style={{display: 'block', color: 'green'}}37type="text"38value=""39/>40);41expect(console.warn).toHaveBeenCalled();42return result;43}44});45});4647it('should leave explicitly specified properties intact', function() {48var instance = <TestComponent type="radio" />;49instance = ReactTestUtils.renderIntoDocument(instance);5051reactComponentExpect(instance)52.expectRenderedChild()53.toBeComponentOfType('input')54.scalarPropsEqual({55className: 'textinput',56style: {display: 'block', color: 'green'},57type: 'text',58value: ''59});60});6162it('should transfer unspecified properties', function() {63var instance = <TestComponent placeholder="Type here..." />;64instance = ReactTestUtils.renderIntoDocument(instance);6566reactComponentExpect(instance)67.expectRenderedChild()68.toBeComponentOfType('input')69.scalarPropsEqual({placeholder: 'Type here...'});70});7172it('should transfer using merge strategies', function() {73var instance =74<TestComponent75className="hidden_elem"76style={{width: '100%', display: 'none'}}77/>;78instance = ReactTestUtils.renderIntoDocument(instance);7980reactComponentExpect(instance)81.expectRenderedChild()82.toBeComponentOfType('input')83.scalarPropsEqual({84className: 'textinput hidden_elem',85style: {86color: 'green',87display: 'block',88width: '100%'89}90});91});9293it('should not transfer children', function() {94var ChildrenTestComponent = React.createClass({95render: function() {96return this.transferPropsTo(<div />);97}98});99100var instance =101<ChildrenTestComponent>102<span>Hello!</span>103</ChildrenTestComponent>;104105instance = ReactTestUtils.renderIntoDocument(instance);106reactComponentExpect(instance)107.expectRenderedChild()108.toBeDOMComponentWithTag('div')109.toBeDOMComponentWithNoChildren();110});111112it('should not transfer ref or key', function() {113var TestComponent = React.createClass({114render: function() {115expect(this.props.ref).toBeUndefined();116expect(this.props.key).toBeUndefined();117return <div />;118}119});120var OuterTestComponent = React.createClass({121render: function() {122return this.transferPropsTo(<TestComponent />);123}124});125var OuterOuterTestComponent = React.createClass({126render: function() {127return <OuterTestComponent ref="testref" key="testkey" />;128}129});130131ReactTestUtils.renderIntoDocument(<OuterOuterTestComponent />);132});133134it('should not transferPropsTo() a component you don\'t own', function() {135var Parent = React.createClass({136render: function() {137return <Child><span /></Child>;138}139});140141var Child = React.createClass({142render: function() {143return this.transferPropsTo(this.props.children);144}145});146147expect(function() {148ReactTestUtils.renderIntoDocument(<Parent />);149}).toThrow(150'Invariant Violation: ' +151'Child: You can\'t call transferPropsTo() on a component that you ' +152'don\'t own, span. ' +153'This usually means you are calling transferPropsTo() on a component ' +154'passed in as props or children.'155);156});157158it('uses the default instead of the transferred prop (regress)', function() {159160var Child = React.createClass({161162getDefaultProps: function() {163return {164x: 2165};166},167168render: function() {169expect(this.props.x).toBe(2);170return <div />;171}172173});174175var Parent = React.createClass({176177render: function() {178return this.transferPropsTo(<Child />);179}180181});182183ReactTestUtils.renderIntoDocument(<Parent x={5} />);184185});186187});188189190