react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / test / reactComponentExpect.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 reactComponentExpect9* @nolint10*/1112"use strict";1314var ReactTestUtils = require('ReactTestUtils');1516var assign = require('Object.assign');1718function reactComponentExpect(instance) {19if (instance instanceof reactComponentExpect) {20return instance;21}2223if (!(this instanceof reactComponentExpect)) {24return new reactComponentExpect(instance);25}2627this._instance = instance;28expect(typeof instance).toBe('object');29expect(typeof instance.constructor).toBe('function');30expect(ReactTestUtils.isElement(instance)).toBe(false);31}3233assign(reactComponentExpect.prototype, {34// Getters -------------------------------------------------------------------3536/**37* @instance: Retrieves the backing instance.38*/39instance: function() {40return this._instance;41},4243/**44* There are two types of components in the world.45* - A component created via React.createClass() - Has a single child46* subComponent - the return value from the .render() function. This47* function @subComponent expects that this._instance is component created48* with React.createClass().49* - A primitive DOM component - which has many renderedChildren, each of50* which may have a name that is unique with respect to its siblings. This51* method will fail if this._instance is a primitive component.52*53* TL;DR: An instance may have a subComponent (this._renderedComponent) or54* renderedChildren, but never both. Neither will actually show up until you55* render the component (simply instantiating is not enough).56*/57expectRenderedChild: function() {58this.toBeCompositeComponent();59return new reactComponentExpect(this.instance()._renderedComponent);60},6162/**63* The nth child of a DOMish component instance that is not falsy.64*/65expectRenderedChildAt: function(childIndex) {66// Currently only dom components have arrays of children, but that will67// change soon.68this.toBeDOMComponent();69var renderedChildren = this.instance()._renderedChildren || {};70for (var name in renderedChildren) {71if (!renderedChildren.hasOwnProperty(name)) {72continue;73}74if (renderedChildren[name]) {75if (renderedChildren[name]._mountIndex === childIndex) {76return new reactComponentExpect(renderedChildren[name]);77}78}79}80throw new Error('Child:' + childIndex + ' is not found');81},8283toBeDOMComponentWithChildCount: function(n) {84this.toBeDOMComponent();85expect(this.instance()._renderedChildren).toBeTruthy();86var len = Object.keys(this.instance()._renderedChildren).length;87expect(len).toBe(n);88return this;89},9091toBeDOMComponentWithNoChildren: function() {92this.toBeDOMComponent();93expect(this.instance()._renderedChildren).toBeFalsy();94return this;95},9697// Matchers ------------------------------------------------------------------9899toBeComponentOfType: function(convenienceConstructor) {100var type = typeof convenienceConstructor === 'string' ?101convenienceConstructor :102convenienceConstructor.type;103expect(104this.instance()._currentElement.type === type105).toBe(true);106return this;107},108109/**110* A component that is created with React.createClass. Just duck typing111* here.112*/113toBeCompositeComponent: function() {114expect(115typeof this.instance().render === 'function' &&116typeof this.instance().setState === 'function'117).toBe(true);118return this;119},120121toBeCompositeComponentWithType: function(convenienceConstructor) {122this.toBeCompositeComponent();123expect(124this.instance()._currentElement.type === convenienceConstructor.type125).toBe(true);126return this;127},128129toBeTextComponent: function() {130expect(ReactTestUtils.isTextComponent(this.instance())).toBe(true);131return this;132},133134toBePresent: function() {135expect(this.instance()).toBeTruthy();136return this;137},138139/**140* A terminal type of component representing some virtual dom node. Just duck141* typing here.142*/143toBeDOMComponent: function() {144expect(ReactTestUtils.isDOMComponent(this.instance())).toBe(true);145return this;146},147148/**149* @deprecated150* @see toBeComponentOfType151*/152toBeDOMComponentWithTag: function(tag) {153this.toBeDOMComponent();154expect(this.instance().tagName).toBe(tag.toUpperCase());155return this;156},157158/**159* Check that internal state values are equal to a state of expected values.160*/161scalarStateEqual: function(stateNameToExpectedValue) {162expect(this.instance()).toBeTruthy();163for (var stateName in stateNameToExpectedValue) {164if (!stateNameToExpectedValue.hasOwnProperty(stateName)) {165continue;166}167expect(this.instance().state[stateName])168.toEqual(stateNameToExpectedValue[stateName]);169}170return this;171},172173/**174* Check a set of props are equal to a set of expected values - only works175* with scalars.176*/177scalarPropsEqual: function(propNameToExpectedValue) {178expect(this.instance()).toBeTruthy();179for (var propName in propNameToExpectedValue) {180if (!propNameToExpectedValue.hasOwnProperty(propName)) {181continue;182}183expect(this.instance().props[propName])184.toEqual(propNameToExpectedValue[propName]);185}186return this;187},188189/**190* Check a set of props are equal to a set of expected values - only works191* with scalars.192*/193scalarContextEqual: function(contextNameToExpectedValue) {194expect(this.instance()).toBeTruthy();195for (var contextName in contextNameToExpectedValue) {196if (!contextNameToExpectedValue.hasOwnProperty(contextName)) {197continue;198}199expect(this.instance().context[contextName])200.toEqual(contextNameToExpectedValue[contextName]);201}202return this;203}204});205206module.exports = reactComponentExpect;207208209