123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- import PropTypes from '../../lib/ReactPropTypes';
- import ReactTestUtils from "../../lib/ReactTestUtils";
- import React from '../../src/May';
- import { render, unmountComponentAtNode, findDOMNode } from '../../src/may-dom/MayDom'
- var ReactDOM = {
- render: render,
- unmountComponentAtNode: unmountComponentAtNode,
- findDOMNode: findDOMNode
- }
- React.render = render;
- // import React from "../../dist/ReactANU";
- // var ReactDOM = React;
- describe("ReactComponentLifeCycle-test", function() {
- // this.timeout(200000);
- it('should not reuse an instance when it has been unmounted', () => {
- var container = document.createElement('div');
- class StatefulComponent extends React.Component {
- state = {};
- render() {
- return <div />;
- }
- }
- var element = <StatefulComponent />;
- var firstInstance = ReactDOM.render(element, container);
- ReactDOM.unmountComponentAtNode(container);
- var secondInstance = ReactDOM.render(element, container);
- expect(firstInstance).not.toBe(secondInstance);
- });
- /**
- * If a state update triggers rerendering that in turn fires an onDOMReady,
- * that second onDOMReady should not fail.
- */
- it('it should fire onDOMReady when already in onDOMReady', () => {
- var _testJournal = [];
- class Child extends React.Component {
- componentDidMount() {
- _testJournal.push('Child:onDOMReady');
- }
- render() {
- return <div />;
- }
- }
- class SwitcherParent extends React.Component {
- constructor(props) {
- super(props);
- _testJournal.push('SwitcherParent:getInitialState');
- this.state = {showHasOnDOMReadyComponent: false};
- }
- componentDidMount() {
- _testJournal.push('SwitcherParent:onDOMReady');
- this.switchIt();
- }
- switchIt = () => {
- this.setState({showHasOnDOMReadyComponent: true});
- };
- render() {
- return (
- <div>
- {this.state.showHasOnDOMReadyComponent ? <Child /> : <div />}
- </div>
- );
- }
- }
- ReactTestUtils.renderIntoDocument(<SwitcherParent />);
- expect(_testJournal).toEqual([
- 'SwitcherParent:getInitialState',
- 'SwitcherParent:onDOMReady',
- 'Child:onDOMReady',
- ]);
- });
- // You could assign state here, but not access members of it, unless you
- // had provided a getInitialState method.
- it('throws when accessing state in componentWillMount', () => {
- class StatefulComponent extends React.Component {
- componentWillMount() {
- void this.state.yada;
- }
- render() {
- return <div />;
- }
- }
- var instance = <StatefulComponent />;
- expect(function() {
- instance = ReactTestUtils.renderIntoDocument(instance);
- }).toThrow();
- });
- it('should allow update state inside of componentWillMount', () => {
- class StatefulComponent extends React.Component {
- componentWillMount() {
- this.setState({stateField: 'something'});
- }
- render() {
- return <div />;
- }
- }
- var instance = <StatefulComponent />;
- expect(function() {
- instance = ReactTestUtils.renderIntoDocument(instance);
- }).not.toThrow();
- });
- it('should not allow update state inside of getInitialState', () => {
-
- class StatefulComponent extends React.Component {
- constructor(props, context) {
- super(props, context);
-
- this.state = {stateField: 'somethingelse'};
- // this.setState({stateField: 'something'});
- }
- render() {
- return <div>{this.state.stateField}</div>;
- }
- }
- var container = document.createElement('div');
- ReactDOM.render(<StatefulComponent />, container);
- expect(container.textContent).toBe("somethingelse");
- });
- it('should correctly determine if a component is mounted', () => {
- class Component extends React.Component {
-
- componentWillMount() {
- expect(this.isMounted()).toBeFalsy();
- }
- componentDidMount() {
- expect(this.isMounted()).toBeTruthy();
- }
- render() {
- expect(this.isMounted()).toBeFalsy();
- return <div />;
- }
- }
- var element = <Component />;
- var instance = ReactTestUtils.renderIntoDocument(element);
- expect(instance.isMounted()).toBeTruthy();
- });
- it('should correctly determine if a null component is mounted', () => {
- class Component extends React.Component {
-
- componentWillMount() {
- expect(this.isMounted()).toBeFalsy();
- }
- componentDidMount() {
- expect(this.isMounted()).toBeTruthy();
- }
- render() {
- expect(this.isMounted()).toBeFalsy();
- return null;
- }
- }
- var element = <Component />;
- var instance = ReactTestUtils.renderIntoDocument(element);
- expect(instance.isMounted()).toBeTruthy();
- })
- it('isMounted should return false when unmounted', () => {
- class Component extends React.Component {
- render() {
- return <div />;
- }
- }
- var container = document.createElement('div');
- var instance = ReactDOM.render(<Component />, container);
- // No longer a public API, but we can test that it works internally by
- // reaching into the updater.
- expect(instance.isMounted()).toBe(true);
- ReactDOM.unmountComponentAtNode(container);
- // expect(instance.isMounted()).toBe(false);
- });
- it('warns if findDOMNode is used inside render', () => {
-
- class Component extends React.Component {
- state = {isMounted: false};
- componentDidMount() {
- this.setState({isMounted: true});
- }
- render() {
- if (this.state.isMounted) {
- expect(ReactDOM.findDOMNode(this).tagName).toBe('DIV');
- }
- return <div />;
- }
- }
- ReactTestUtils.renderIntoDocument(<Component />);
- });
- it('should carry through each of the phases of setup', () => {
- var clone = function(o) {
- return JSON.parse(JSON.stringify(o));
- };
- var GET_INIT_STATE_RETURN_VAL = {
- hasWillMountCompleted: false,
- hasRenderCompleted: false,
- hasDidMountCompleted: false,
- hasWillUnmountCompleted: false,
- };
- var INIT_RENDER_STATE = {
- hasWillMountCompleted: true,
- hasRenderCompleted: false,
- hasDidMountCompleted: false,
- hasWillUnmountCompleted: false,
- };
- var DID_MOUNT_STATE = {
- hasWillMountCompleted: true,
- hasRenderCompleted: true,
- hasDidMountCompleted: false,
- hasWillUnmountCompleted: false,
- };
- var NEXT_RENDER_STATE = {
- hasWillMountCompleted: true,
- hasRenderCompleted: true,
- hasDidMountCompleted: true,
- hasWillUnmountCompleted: false,
- };
- var WILL_UNMOUNT_STATE = {
- hasWillMountCompleted: true,
- hasDidMountCompleted: true,
- hasRenderCompleted: true,
- hasWillUnmountCompleted: false,
- };
- var POST_WILL_UNMOUNT_STATE = {
- hasWillMountCompleted: true,
- hasDidMountCompleted: true,
- hasRenderCompleted: true,
- hasWillUnmountCompleted: true,
- };
- function getLifeCycleState(instance) {
- return instance.isMounted() ? 'MOUNTED' : 'UNMOUNTED';
- }
- spyOn(console, 'error');
- class LifeCycleComponent extends React.Component {
- constructor(props, context) {
- super(props, context);
- this._testJournal = {};
- var initState = {
- hasWillMountCompleted: false,
- hasDidMountCompleted: false,
- hasRenderCompleted: false,
- hasWillUnmountCompleted: false,
- };
- this._testJournal.returnedFromGetInitialState = clone(initState);
- this._testJournal.lifeCycleAtStartOfGetInitialState = getLifeCycleState(
- this,
- );
- this.state = initState;
- }
- componentWillMount() {
- this._testJournal.stateAtStartOfWillMount = clone(this.state);
- this._testJournal.lifeCycleAtStartOfWillMount = getLifeCycleState(this);
- this.state.hasWillMountCompleted = true;
- }
- componentDidMount() {
- this._testJournal.stateAtStartOfDidMount = clone(this.state);
- this._testJournal.lifeCycleAtStartOfDidMount = getLifeCycleState(this);
- this.setState({hasDidMountCompleted: true});
- }
- render() {
- var isInitialRender = !this.state.hasRenderCompleted;
- if (isInitialRender) {
- this._testJournal.stateInInitialRender = clone(this.state);
- this._testJournal.lifeCycleInInitialRender = getLifeCycleState(this);
- } else {
- this._testJournal.stateInLaterRender = clone(this.state);
- this._testJournal.lifeCycleInLaterRender = getLifeCycleState(this);
- }
- // you would *NEVER* do anything like this in real code!
- this.state.hasRenderCompleted = true;
- return (
- <div ref="theDiv">
- I am the inner DIV
- </div>
- );
- }
- componentWillUnmount() {
- this._testJournal.stateAtStartOfWillUnmount = clone(this.state);
- this._testJournal.lifeCycleAtStartOfWillUnmount = getLifeCycleState(
- this,
- );
- this.state.hasWillUnmountCompleted = true;
- }
- }
- // A component that is merely "constructed" (as in "constructor") but not
- // yet initialized, or rendered.
- //
- var container = document.createElement('div');
- var instance = ReactDOM.render(<LifeCycleComponent />, container);
- // getInitialState
- expect(instance._testJournal.returnedFromGetInitialState).toEqual(
- GET_INIT_STATE_RETURN_VAL,
- );
- expect(instance._testJournal.lifeCycleAtStartOfGetInitialState).toBe(
- 'UNMOUNTED',
- );
- // componentWillMount
- expect(instance._testJournal.stateAtStartOfWillMount).toEqual(
- instance._testJournal.returnedFromGetInitialState,
- );
- expect(instance._testJournal.lifeCycleAtStartOfWillMount).toBe('UNMOUNTED');
- // componentDidMount
- expect(instance._testJournal.stateAtStartOfDidMount).toEqual(
- DID_MOUNT_STATE,
- );
- expect(instance._testJournal.lifeCycleAtStartOfDidMount).toBe('MOUNTED');
- // initial render
- expect(instance._testJournal.stateInInitialRender).toEqual(
- INIT_RENDER_STATE,
- );
- expect(instance._testJournal.lifeCycleInInitialRender).toBe('UNMOUNTED');
- expect(getLifeCycleState(instance)).toBe('MOUNTED');
- // Now *update the component*
- instance.forceUpdate();
- // render 2nd time
- expect(instance._testJournal.stateInLaterRender).toEqual(NEXT_RENDER_STATE);
- expect(instance._testJournal.lifeCycleInLaterRender).toBe('MOUNTED');
- expect(getLifeCycleState(instance)).toBe('MOUNTED');
- ReactDOM.unmountComponentAtNode(container);
- expect(instance._testJournal.stateAtStartOfWillUnmount).toEqual(
- WILL_UNMOUNT_STATE,
- );
- // componentWillUnmount called right before unmount.
- expect(instance._testJournal.lifeCycleAtStartOfWillUnmount).toBe('MOUNTED');
- // But the current lifecycle of the component is unmounted.
- /*expect(getLifeCycleState(instance)).toBe('UNMOUNTED');
- expect(instance.state).toEqual(POST_WILL_UNMOUNT_STATE);*/
-
- });
- it('should allow state updates in componentDidMount', () => {
- /**
- * calls setState in an componentDidMount.
- */
- class SetStateInComponentDidMount extends React.Component {
- state = {
- stateField: this.props.valueToUseInitially,
- };
- componentDidMount() {
- this.setState({stateField: this.props.valueToUseInOnDOMReady});
- }
- render() {
- return <div />;
- }
- }
- var instance = (
- <SetStateInComponentDidMount
- valueToUseInitially="hello"
- valueToUseInOnDOMReady="goodbye"
- />
- );
- instance = ReactTestUtils.renderIntoDocument(instance);
- expect(instance.state.stateField).toBe('goodbye');
- });
- it('should call nested lifecycle methods in the right order', () => {
- var log;
- var logger = function(msg) {
- return function() {
- // return true for shouldComponentUpdate
- log.push(msg);
- return true;
- };
- };
- class Outer extends React.Component {
- componentWillMount = logger('outer componentWillMount');
- componentDidMount = logger('outer componentDidMount');
- componentWillReceiveProps = logger('outer componentWillReceiveProps');
- shouldComponentUpdate = logger('outer shouldComponentUpdate');
- componentWillUpdate = logger('outer componentWillUpdate');
- componentDidUpdate = logger('outer componentDidUpdate');
- componentWillUnmount = logger('outer componentWillUnmount');
- render() {
- return <div><Inner x={this.props.x} /></div>;
- }
- }
- class Inner extends React.Component {
- componentWillMount = logger('inner componentWillMount');
- componentDidMount = logger('inner componentDidMount');
- componentWillReceiveProps = logger('inner componentWillReceiveProps');
- shouldComponentUpdate = logger('inner shouldComponentUpdate');
- componentWillUpdate = logger('inner componentWillUpdate');
- componentDidUpdate = logger('inner componentDidUpdate');
- componentWillUnmount = logger('inner componentWillUnmount');
- render() {
- return <span>{this.props.x}</span>;
- }
- }
- var container = document.createElement('div');
- log = [];
- ReactDOM.render(<Outer x={17} />, container);
- expect(log).toEqual([
- 'outer componentWillMount',
- 'inner componentWillMount',
- 'inner componentDidMount',
- 'outer componentDidMount',
- ]);
- log = [];
- ReactDOM.render(<Outer x={42} />, container);
- expect(log).toEqual([
- 'outer componentWillReceiveProps',
- 'outer shouldComponentUpdate',
- 'outer componentWillUpdate',
- 'inner componentWillReceiveProps',
- 'inner shouldComponentUpdate',
- 'inner componentWillUpdate',
- 'inner componentDidUpdate',
- 'outer componentDidUpdate',
- ]);
- log = [];
- ReactDOM.unmountComponentAtNode(container);
- expect(log).toEqual([
- 'outer componentWillUnmount',
- 'inner componentWillUnmount',
- ]);
- });
- //暂不支持这种形式的component
- /*it('calls effects on module-pattern component', function() {
- const log = [];
- function Parent() {
- return {
- render() {
- expect(typeof this.props).toBe('object');
- log.push('render');
- return <Child />;
- },
- componentWillMount() {
- log.push('will mount');
- },
- componentDidMount() {
- log.push('did mount');
- },
- componentDidUpdate() {
- log.push('did update');
- },
- getChildContext() {
- return {x: 2};
- },
- };
- }
- Parent.childContextTypes = {
- x: PropTypes.number,
- };
- function Child(props, context) {
- expect(context.x).toBe(2);
- return <div />;
- }
- Child.contextTypes = {
- x: PropTypes.number,
- };
- const div = document.createElement('div');
- ReactDOM.render(<Parent ref={c => c && log.push('ref')} />, div);
- ReactDOM.render(<Parent ref={c => c && log.push('ref')} />, div);
- expect(log).toEqual([
- 'will mount',
- 'render',
- 'did mount',
- 'ref',
- 'render',
- 'did update',
- 'ref',
- ]);
- });*/
- });
|