diff --git a/src/browser/dom/__tests__/DOMChildrenOperations-test.js b/src/browser/dom/__tests__/DOMChildrenOperations-test.js new file mode 100644 index 0000000000000..24b6710c19f96 --- /dev/null +++ b/src/browser/dom/__tests__/DOMChildrenOperations-test.js @@ -0,0 +1,169 @@ +/** + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @jsx React.DOM + * @emails react-core + */ + +"use strict"; + +var React = require('React'); +var ReactTestUtils = require('ReactTestUtils'); + +describe('DOMChildrenOperations', function() { + describe('processUpdates', function() { + var iframeItem1 = {iframe: true, key: 'iframe1'}; + var iframeItem2 = {iframe: true, key: 'iframe2'}; + var listOfItems = []; + + for (var i = 0; i < 20; i++) { + if (i % 2) { + listOfItems.push({index: i}); + } else { + listOfItems.push({key: i, index: i}); + } + } + + var TestComponent = React.createClass({ + getInitialState: function() { + return {items: this.props.items}; + }, + mutateChildren: function() { + var temp = listOfItems.slice(); + var items = this.props.items.slice(); + var count = Math.floor(Math.random() * 15) + 5; + + if (this.props.reload) { + for (var i = 0; i < count; i++) { + items.unshift(items.pop()); + } + } + + for (var i = 0; i < count; i++) { + var from = Math.floor(Math.random() * temp.length); + var to = Math.floor(Math.random() * (items.length + 1)); + var item = temp.splice(from, 1)[0]; + items.splice(to, 0, item); + } + + this.setState({ + items: items + }); + }, + expectImmovableReloaded: function() { + var items = this.state.items; + var childNodes = this.getDOMNode().childNodes; + var reloaded = false; + + for (var i = 0; i < items.length; i++) { + if (items[i].iframe) { + reloaded = reloaded || + !(childNodes[i].contentWindow && + childNodes[i].contentWindow.reactKey); + } + } + + expect(reloaded).toBe(true); + }, + componentDidMount: function() { + var items = this.state.items; + var childNodes = this.getDOMNode().childNodes; + + for (var i = 0; i < items.length; i++) { + if (items[i].iframe) { + childNodes[i].contentWindow.reactKey = items[i].key; + } + } + }, + componentDidUpdate: function() { + var items = this.state.items; + var childNodes = this.getDOMNode().childNodes; + + expect(childNodes.length).toBe(items.length); + + for (var i = 0; i < items.length; i++) { + if (items[i].iframe) { + if (!this.props.reload) { + expect( + childNodes[i].contentWindow && + childNodes[i].contentWindow.reactKey + ).toBe(items[i].key); + } + } else { + expect(childNodes[i].textContent).toBe(i + ':' + items[i].index); + } + } + }, + render: function() { + return ( +
+ {this.state.items.map(function(item, i) { + if (item.iframe) { + return