react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / test / createHierarchyRenderer.js
81152 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* @providesModule createHierarchyRenderer9*/1011var React = require('React');1213/**14* Creates a render method that makes it easier to create, render, and inspect a15* hierarchy of mock React component classes.16*17* A component class is created for each of the supplied render methods. Each18* render method is invoked with the classes created using the render methods19* that come after it in the supplied list of render methods.20*21* var renderHierarchy = createHierarchyRenderer(22* function ComponentA(ComponentB, ComponentC) {...},23* function ComponentB(ComponentC) {...},24* function ComponentC() {...}25* );26*27* When the hierarchy is invoked, a two-dimensional array is returned. Each28* array corresponds to a supplied render method and contains the instances29* returned by that render method in the order it was invoked.30*31* var instances = renderHierarchy(32* function(ComponentA[, ComponentB, ComponentC]) {33* React.render(<ComponentA />, ...);34* })35* );36* instances[0][0]; // First return value of first render method.37* instances[1][0]; // First return value of second render method.38* instances[1][1]; // Second return value of second render method.39*40* Refs should be used to reference components that are not the return value of41* render methods.42*43* expect(instances[0][0].refs.X).toBe(...);44*45* NOTE: The component classes created for each render method are re-used for46* each invocation of the hierarchy renderer. If new classes are needed, you47* should re-execute `createHierarchyRenderer` with the same arguments.48*49* @param {array<function>} ...renderMethods50* @return {function}51*/52function createHierarchyRenderer(...renderMethods) {53var instances;54var Components = renderMethods.reduceRight(55function(Components, renderMethod, depth) {56var Component = React.createClass({57displayName: renderMethod.name,58render: function() {59instances[depth].push(this);60return renderMethod.apply(this, Components);61}62});63return [Component].concat(Components);64},65[]66);67/**68* @param {function} renderComponent69* @return {array<array<*>>}70*/71function renderHierarchy(renderComponent) {72instances = renderMethods.map(() => []);73renderComponent.apply(null, Components);74return instances;75}76return renderHierarchy;77}7879module.exports = createHierarchyRenderer;808182