react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / __tests__ / ReactCompositeComponentState-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;2021describe('ReactCompositeComponent-state', function() {2223beforeEach(function() {24React = require('React');25ReactTestUtils = require('ReactTestUtils');26reactComponentExpect = require('reactComponentExpect');2728TestComponent = React.createClass({29peekAtState: function(from, state) {30if (state) {31this.props.stateListener(from, state && state.color);32} else {33this.props.stateListener(34from,35this.state && this.state.color,36this._pendingState && this._pendingState.color37);38}39},4041setFavoriteColor: function(nextColor) {42this.setState({color: nextColor});43},4445getInitialState: function() {46this.peekAtState('getInitialState');47return {color: 'red'};48},4950render: function() {51this.peekAtState('render');52return <div>{this.state.color}</div>;53},5455componentWillMount: function() {56this.peekAtState('componentWillMount-start');57this.setState({color: 'sunrise'});58this.peekAtState('componentWillMount-after-sunrise');59this.setState({color: 'orange'});60this.peekAtState('componentWillMount-end');61},6263componentDidMount: function() {64this.peekAtState('componentDidMount-start');65this.setState({color: 'yellow'});66this.peekAtState('componentDidMount-end');67},6869componentWillReceiveProps: function(newProps) {70this.peekAtState('componentWillReceiveProps-start');71if (newProps.nextColor) {72this.setState({color: newProps.nextColor});73}74this.peekAtState('componentWillReceiveProps-end');75},7677shouldComponentUpdate: function(nextProps, nextState) {78this.peekAtState('shouldComponentUpdate-currentState');79this.peekAtState('shouldComponentUpdate-nextState', nextState);80return true;81},8283componentWillUpdate: function(nextProps, nextState) {84this.peekAtState('componentWillUpdate-currentState');85this.peekAtState('componentWillUpdate-nextState', nextState);86},8788componentDidUpdate: function(prevProps, prevState) {89this.peekAtState('componentDidUpdate-currentState');90this.peekAtState('componentDidUpdate-prevState', prevState);91},9293componentWillUnmount: function() {94this.peekAtState('componentWillUnmount');95}96});9798});99100it('should support setting state', function() {101var stateListener = mocks.getMockFunction();102var instance = <TestComponent stateListener={stateListener} />;103instance = ReactTestUtils.renderIntoDocument(instance);104instance.setProps({nextColor: 'green'});105instance.setFavoriteColor('blue');106instance.forceUpdate();107instance.unmountComponent();108109expect(stateListener.mock.calls).toEqual([110// there is no state when getInitialState() is called111[ 'getInitialState', null, null ],112[ 'componentWillMount-start', 'red', null ],113// setState() only enqueues a pending state.114[ 'componentWillMount-after-sunrise', 'red', 'sunrise' ],115[ 'componentWillMount-end', 'red', 'orange' ],116// pending state has been applied117[ 'render', 'orange', null ],118[ 'componentDidMount-start', 'orange', null ],119// componentDidMount() called setState({color:'yellow'}), currently this120// occurs inline.121// In a future where setState() is async, this test result will change.122[ 'shouldComponentUpdate-currentState', 'orange', null ],123[ 'shouldComponentUpdate-nextState', 'yellow' ],124[ 'componentWillUpdate-currentState', 'orange', null ],125[ 'componentWillUpdate-nextState', 'yellow' ],126[ 'render', 'yellow', null ],127[ 'componentDidUpdate-currentState', 'yellow', null ],128[ 'componentDidUpdate-prevState', 'orange' ],129// componentDidMount() finally closes.130[ 'componentDidMount-end', 'yellow', null ],131[ 'componentWillReceiveProps-start', 'yellow', null ],132// setState({color:'green'}) only enqueues a pending state.133[ 'componentWillReceiveProps-end', 'yellow', 'green' ],134[ 'shouldComponentUpdate-currentState', 'yellow', null ],135[ 'shouldComponentUpdate-nextState', 'green' ],136[ 'componentWillUpdate-currentState', 'yellow', null ],137[ 'componentWillUpdate-nextState', 'green' ],138[ 'render', 'green', null ],139[ 'componentDidUpdate-currentState', 'green', null ],140[ 'componentDidUpdate-prevState', 'yellow' ],141// setFavoriteColor('blue')142[ 'shouldComponentUpdate-currentState', 'green', null ],143[ 'shouldComponentUpdate-nextState', 'blue' ],144[ 'componentWillUpdate-currentState', 'green', null ],145[ 'componentWillUpdate-nextState', 'blue' ],146[ 'render', 'blue', null ],147[ 'componentDidUpdate-currentState', 'blue', null ],148[ 'componentDidUpdate-prevState', 'green' ],149// forceUpdate()150[ 'componentWillUpdate-currentState', 'blue', null ],151[ 'componentWillUpdate-nextState', 'blue' ],152[ 'render', 'blue', null ],153[ 'componentDidUpdate-currentState', 'blue', null ],154[ 'componentDidUpdate-prevState', 'blue' ],155// unmountComponent()156// state is available within `componentWillUnmount()`157[ 'componentWillUnmount', 'blue', null ]158]);159});160});161162163