react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / __tests__ / ReactMultiChild-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 mocks = require('mocks');1415describe('ReactMultiChild', function() {16var React;1718beforeEach(function() {19require('mock-modules').dumpCache();20React = require('React');21});2223describe('reconciliation', function() {24it('should update children when possible', function() {25var container = document.createElement('div');2627var mockMount = mocks.getMockFunction();28var mockUpdate = mocks.getMockFunction();29var mockUnmount = mocks.getMockFunction();3031var MockComponent = React.createClass({32componentDidMount: mockMount,33componentDidUpdate: mockUpdate,34componentWillUnmount: mockUnmount,35render: function() {36return <span />;37}38});3940expect(mockMount.mock.calls.length).toBe(0);41expect(mockUpdate.mock.calls.length).toBe(0);42expect(mockUnmount.mock.calls.length).toBe(0);4344React.render(<div><MockComponent /></div>, container);4546expect(mockMount.mock.calls.length).toBe(1);47expect(mockUpdate.mock.calls.length).toBe(0);48expect(mockUnmount.mock.calls.length).toBe(0);4950React.render(<div><MockComponent /></div>, container);5152expect(mockMount.mock.calls.length).toBe(1);53expect(mockUpdate.mock.calls.length).toBe(1);54expect(mockUnmount.mock.calls.length).toBe(0);55});5657it('should replace children with different constructors', function() {58var container = document.createElement('div');5960var mockMount = mocks.getMockFunction();61var mockUnmount = mocks.getMockFunction();6263var MockComponent = React.createClass({64componentDidMount: mockMount,65componentWillUnmount: mockUnmount,66render: function() {67return <span />;68}69});7071expect(mockMount.mock.calls.length).toBe(0);72expect(mockUnmount.mock.calls.length).toBe(0);7374React.render(<div><MockComponent /></div>, container);7576expect(mockMount.mock.calls.length).toBe(1);77expect(mockUnmount.mock.calls.length).toBe(0);7879React.render(<div><span /></div>, container);8081expect(mockMount.mock.calls.length).toBe(1);82expect(mockUnmount.mock.calls.length).toBe(1);83});8485it('should replace children with different owners', function() {86var container = document.createElement('div');8788var mockMount = mocks.getMockFunction();89var mockUnmount = mocks.getMockFunction();9091var MockComponent = React.createClass({92componentDidMount: mockMount,93componentWillUnmount: mockUnmount,94render: function() {95return <span />;96}97});9899var WrapperComponent = React.createClass({100render: function() {101return this.props.children || <MockComponent />;102}103});104105expect(mockMount.mock.calls.length).toBe(0);106expect(mockUnmount.mock.calls.length).toBe(0);107108React.render(<WrapperComponent />, container);109110expect(mockMount.mock.calls.length).toBe(1);111expect(mockUnmount.mock.calls.length).toBe(0);112113React.render(114<WrapperComponent><MockComponent /></WrapperComponent>,115container116);117118expect(mockMount.mock.calls.length).toBe(2);119expect(mockUnmount.mock.calls.length).toBe(1);120});121122it('should replace children with different keys', function() {123var container = document.createElement('div');124125var mockMount = mocks.getMockFunction();126var mockUnmount = mocks.getMockFunction();127128var MockComponent = React.createClass({129componentDidMount: mockMount,130componentWillUnmount: mockUnmount,131render: function() {132return <span />;133}134});135136expect(mockMount.mock.calls.length).toBe(0);137expect(mockUnmount.mock.calls.length).toBe(0);138139React.render(<div><MockComponent key="A" /></div>, container);140141expect(mockMount.mock.calls.length).toBe(1);142expect(mockUnmount.mock.calls.length).toBe(0);143144React.render(<div><MockComponent key="B" /></div>, container);145146expect(mockMount.mock.calls.length).toBe(2);147expect(mockUnmount.mock.calls.length).toBe(1);148});149});150151describe('innerHTML', function() {152var setInnerHTML;153154// Only run this suite if `Element.prototype.innerHTML` can be spied on.155var innerHTMLDescriptor = Object.getOwnPropertyDescriptor(156Element.prototype,157'innerHTML'158);159if (!innerHTMLDescriptor) {160return;161}162163beforeEach(function() {164Object.defineProperty(Element.prototype, 'innerHTML', {165set: setInnerHTML = jasmine.createSpy().andCallFake(166innerHTMLDescriptor.set167)168});169});170171it('should only set `innerHTML` once on update', function() {172var container = document.createElement('div');173174React.render(175<div>176<p><span /></p>177<p><span /></p>178<p><span /></p>179</div>,180container181);182// Warm the cache used by `getMarkupWrap`.183React.render(184<div>185<p><span /><span /></p>186<p><span /><span /></p>187<p><span /><span /></p>188</div>,189container190);191expect(setInnerHTML).toHaveBeenCalled();192var callCountOnMount = setInnerHTML.callCount;193194React.render(195<div>196<p><span /><span /><span /></p>197<p><span /><span /><span /></p>198<p><span /><span /><span /></p>199</div>,200container201);202expect(setInnerHTML.callCount).toBe(callCountOnMount + 1);203});204});205});206207208