react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / utils / __tests__ / cloneWithProps-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";1213require('mock-modules')14.dontMock('cloneWithProps')15.dontMock('emptyObject');1617var mocks = require('mocks');1819var React;20var ReactTestUtils;2122var onlyChild;23var cloneWithProps;24var emptyObject;2526describe('cloneWithProps', function() {2728beforeEach(function() {29React = require('React');30ReactTestUtils = require('ReactTestUtils');31onlyChild = require('onlyChild');32cloneWithProps = require('cloneWithProps');33emptyObject = require('emptyObject');34});3536it('should clone a DOM component with new props', function() {37var Grandparent = React.createClass({38render: function() {39return <Parent><div className="child" /></Parent>;40}41});42var Parent = React.createClass({43render: function() {44return (45<div className="parent">46{cloneWithProps(onlyChild(this.props.children), {className: 'xyz'})}47</div>48);49}50});51var component = ReactTestUtils.renderIntoDocument(<Grandparent />);52expect(component.getDOMNode().childNodes[0].className)53.toBe('xyz child');54});5556it('should clone a composite component with new props', function() {5758var Child = React.createClass({59render: function() {60return <div className={this.props.className} />;61}62});6364var Grandparent = React.createClass({65render: function() {66return <Parent><Child className="child" /></Parent>;67}68});69var Parent = React.createClass({70render: function() {71return (72<div className="parent">73{cloneWithProps(onlyChild(this.props.children), {className: 'xyz'})}74</div>75);76}77});78var component = ReactTestUtils.renderIntoDocument(<Grandparent />);79expect(component.getDOMNode().childNodes[0].className)80.toBe('xyz child');81});8283it('should warn when cloning with refs', function() {84var Grandparent = React.createClass({85render: function() {86return <Parent><div ref="yolo" /></Parent>;87}88});89var Parent = React.createClass({90render: function() {91return (92<div>93{cloneWithProps(onlyChild(this.props.children), {className: 'xyz'})}94</div>95);96}97});9899var _warn = console.warn;100101try {102console.warn = mocks.getMockFunction();103104var component = ReactTestUtils.renderIntoDocument(<Grandparent />);105expect(component.refs).toBe(emptyObject);106expect(console.warn.mock.calls.length).toBe(1);107} finally {108console.warn = _warn;109}110});111112it('should transfer the key property', function() {113var Component = React.createClass({114render: function() {115return null;116}117});118var clone = cloneWithProps(<Component />, {key: 'xyz'});119expect(clone.key).toBe('xyz');120});121122it('should transfer children', function() {123var Component = React.createClass({124render: function() {125expect(this.props.children).toBe('xyz');126return <div />;127}128});129130ReactTestUtils.renderIntoDocument(131cloneWithProps(<Component />, {children: 'xyz'})132);133});134135it('should shallow clone children', function() {136var Component = React.createClass({137render: function() {138expect(this.props.children).toBe('xyz');139return <div />;140}141});142143ReactTestUtils.renderIntoDocument(144cloneWithProps(<Component>xyz</Component>, {})145);146});147148it('should support keys and refs', function() {149var Component = React.createClass({150render: function() {151return <div />;152}153});154155var Parent = React.createClass({156render: function() {157var clone =158cloneWithProps(this.props.children, {key: 'xyz', ref: 'xyz'});159expect(clone.key).toBe('xyz');160expect(clone.ref).toBe('xyz');161return <div>{clone}</div>;162}163});164165var Grandparent = React.createClass({166render: function() {167return <Parent><Component key="abc" /></Parent>;168}169});170171ReactTestUtils.renderIntoDocument(<Grandparent />);172});173174it('should overwrite props', function() {175var Component = React.createClass({176render: function() {177expect(this.props.myprop).toBe('xyz');178return <div />;179}180});181182ReactTestUtils.renderIntoDocument(183cloneWithProps(<Component myprop="abc" />, {myprop: 'xyz'})184);185});186});187188189