react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / __tests__ / ReactCompositeComponentMixin-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');1415var React;16var ReactTestUtils;17var reactComponentExpect;1819var TestComponent;20var TestComponentWithPropTypes;21var TestComponentWithReverseSpec;22var mixinPropValidator;23var componentPropValidator;2425describe('ReactCompositeComponent-mixin', function() {2627beforeEach(function() {28React = require('React');29ReactTestUtils = require('ReactTestUtils');30reactComponentExpect = require('reactComponentExpect');31mixinPropValidator = mocks.getMockFunction();32componentPropValidator = mocks.getMockFunction();3334var MixinA = {35propTypes: {36propA: function() {}37},38componentDidMount: function() {39this.props.listener('MixinA didMount');40}41};4243var MixinB = {44mixins: [MixinA],45propTypes: {46propB: function() {}47},48componentDidMount: function() {49this.props.listener('MixinB didMount');50}51};5253var MixinBWithReverseSpec = {54componentDidMount: function() {55this.props.listener('MixinBWithReverseSpec didMount');56},57mixins: [MixinA]58};5960var MixinC = {61statics: {62staticC: function() {}63},64componentDidMount: function() {65this.props.listener('MixinC didMount');66}67};6869var MixinD = {70propTypes: {71value: mixinPropValidator72}73};7475TestComponent = React.createClass({76mixins: [MixinB, MixinC, MixinD],77statics: {78staticComponent: function() {}79},80propTypes: {81propComponent: function() {}82},83componentDidMount: function() {84this.props.listener('Component didMount');85},86render: function() {87return <div />;88}89});9091TestComponentWithReverseSpec = React.createClass({92render: function() {93return <div />;94},95componentDidMount: function() {96this.props.listener('Component didMount');97},98mixins: [MixinBWithReverseSpec, MixinC, MixinD]99});100101TestComponentWithPropTypes = React.createClass({102mixins: [MixinD],103propTypes: {104value: componentPropValidator105},106render: function() {107return <div />;108}109});110});111112it('should support merging propTypes and statics', function() {113var listener = mocks.getMockFunction();114var instance = <TestComponent listener={listener} />;115instance = ReactTestUtils.renderIntoDocument(instance);116117var instancePropTypes = instance.constructor.propTypes;118119expect('propA' in instancePropTypes).toBe(true);120expect('propB' in instancePropTypes).toBe(true);121expect('propComponent' in instancePropTypes).toBe(true);122123expect('staticC' in TestComponent).toBe(true);124expect('staticComponent' in TestComponent).toBe(true);125});126127it('should support chaining delegate functions', function() {128var listener = mocks.getMockFunction();129var instance = <TestComponent listener={listener} />;130instance = ReactTestUtils.renderIntoDocument(instance);131132expect(listener.mock.calls).toEqual([133['MixinA didMount'],134['MixinB didMount'],135['MixinC didMount'],136['Component didMount']137]);138});139140it('should chain functions regardless of spec property order', function() {141var listener = mocks.getMockFunction();142var instance = <TestComponentWithReverseSpec listener={listener} />;143instance = ReactTestUtils.renderIntoDocument(instance);144145expect(listener.mock.calls).toEqual([146['MixinA didMount'],147['MixinBWithReverseSpec didMount'],148['MixinC didMount'],149['Component didMount']150]);151});152153it('should validate prop types via mixins', function() {154expect(TestComponent.type.propTypes).toBeDefined();155expect(TestComponent.type.propTypes.value)156.toBe(mixinPropValidator);157});158159it('should override mixin prop types with class prop types', function() {160// Sanity check...161expect(componentPropValidator).toNotBe(mixinPropValidator);162// Actually check...163expect(TestComponentWithPropTypes.type.propTypes)164.toBeDefined();165expect(TestComponentWithPropTypes.type.propTypes.value)166.toNotBe(mixinPropValidator);167expect(TestComponentWithPropTypes.type.propTypes.value)168.toBe(componentPropValidator);169});170});171172173