react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / __tests__ / ReactMount-test.js
81159 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 mocks = require('mocks');1415describe('ReactMount', function() {16var React = require('React');17var ReactMount = require('ReactMount');18var ReactTestUtils = require('ReactTestUtils');1920describe('constructAndRenderComponentByID', function() {21it('throws if given an id for a component that doesn\'t exist', function() {22expect(function() {23ReactMount.constructAndRenderComponentByID(24function dummyComponentConstructor() {},25{},26'SOME_ID_THAT_DOESNT_EXIST'27);28}).toThrow();29});30});3132it('throws when given a string', function() {33expect(function() {34ReactTestUtils.renderIntoDocument('div');35}).toThrow(36'Invariant Violation: renderComponent(): Invalid component element. ' +37'Instead of passing an element string, make sure to instantiate it ' +38'by passing it to React.createElement.'39);40});4142it('throws when given a factory', function() {43var Component = React.createClass({44render: function() {45return <div />;46}47});48expect(function() {49ReactTestUtils.renderIntoDocument(Component);50}).toThrow(51'Invariant Violation: renderComponent(): Invalid component element. ' +52'Instead of passing a component class, make sure to instantiate it ' +53'by passing it to React.createElement.'54);55});5657it('should render different components in same root', function() {58var container = document.createElement('container');59document.documentElement.appendChild(container);6061ReactMount.render(<div></div>, container);62expect(container.firstChild.nodeName).toBe('DIV');6364ReactMount.render(<span></span>, container);65expect(container.firstChild.nodeName).toBe('SPAN');66});6768it('should unmount and remount if the key changes', function() {69var container = document.createElement('container');7071var mockMount = mocks.getMockFunction();72var mockUnmount = mocks.getMockFunction();7374var Component = React.createClass({75componentDidMount: mockMount,76componentWillUnmount: mockUnmount,77render: function() {78return <span>{this.props.text}</span>;79}80});8182expect(mockMount.mock.calls.length).toBe(0);83expect(mockUnmount.mock.calls.length).toBe(0);8485ReactMount.render(<Component text="orange" key="A" />, container);86expect(container.firstChild.innerHTML).toBe('orange');87expect(mockMount.mock.calls.length).toBe(1);88expect(mockUnmount.mock.calls.length).toBe(0);8990// If we change the key, the component is unmounted and remounted91ReactMount.render(<Component text="green" key="B" />, container);92expect(container.firstChild.innerHTML).toBe('green');93expect(mockMount.mock.calls.length).toBe(2);94expect(mockUnmount.mock.calls.length).toBe(1);9596// But if we don't change the key, the component instance is reused97ReactMount.render(<Component text="blue" key="B" />, container);98expect(container.firstChild.innerHTML).toBe('blue');99expect(mockMount.mock.calls.length).toBe(2);100expect(mockUnmount.mock.calls.length).toBe(1);101});102103it('should reuse markup if rendering to the same target twice', function() {104var container = document.createElement('container');105var instance1 = React.render(<div />, container);106var instance2 = React.render(<div />, container);107108expect(instance1 === instance2).toBe(true);109});110});111112113