Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

'use strict';

var keyMirror = require('keyMirror');

var React;
var ReactDOM;
var ReactInstanceMap;
Expand Down Expand Up @@ -68,25 +66,24 @@ var POST_WILL_UNMOUNT_STATE = {
/**
* Every React component is in one of these life cycles.
*/
var ComponentLifeCycle = keyMirror({
type ComponentLifeCycle =
/**
* Mounted components have a DOM node representation and are capable of
* receiving new props.
*/
MOUNTED: null,
'MOUNTED' |
/**
* Unmounted components are inactive and cannot receive new props.
*/
UNMOUNTED: null,
});
'UNMOUNTED';

function getLifeCycleState(instance) {
function getLifeCycleState(instance): ComponentLifeCycle {
var internalInstance = ReactInstanceMap.get(instance);
// Once a component gets mounted, it has an internal instance, once it
// gets unmounted, it loses that internal instance.
return internalInstance ?
ComponentLifeCycle.MOUNTED :
ComponentLifeCycle.UNMOUNTED;
'MOUNTED' :
'UNMOUNTED';
}

/**
Expand Down Expand Up @@ -407,30 +404,30 @@ describe('ReactComponentLifeCycle', function() {
GET_INIT_STATE_RETURN_VAL
);
expect(instance._testJournal.lifeCycleAtStartOfGetInitialState)
.toBe(ComponentLifeCycle.UNMOUNTED);
.toBe('UNMOUNTED');

// componentWillMount
expect(instance._testJournal.stateAtStartOfWillMount).toEqual(
instance._testJournal.returnedFromGetInitialState
);
expect(instance._testJournal.lifeCycleAtStartOfWillMount)
.toBe(ComponentLifeCycle.MOUNTED);
.toBe('MOUNTED');

// componentDidMount
expect(instance._testJournal.stateAtStartOfDidMount)
.toEqual(DID_MOUNT_STATE);
expect(instance._testJournal.lifeCycleAtStartOfDidMount).toBe(
ComponentLifeCycle.MOUNTED
'MOUNTED'
);

// render
expect(instance._testJournal.stateInInitialRender)
.toEqual(INIT_RENDER_STATE);
expect(instance._testJournal.lifeCycleInInitialRender).toBe(
ComponentLifeCycle.MOUNTED
'MOUNTED'
);

expect(getLifeCycleState(instance)).toBe(ComponentLifeCycle.MOUNTED);
expect(getLifeCycleState(instance)).toBe('MOUNTED');

// Now *update the component*
instance.forceUpdate();
Expand All @@ -439,22 +436,22 @@ describe('ReactComponentLifeCycle', function() {
expect(instance._testJournal.stateInLaterRender)
.toEqual(NEXT_RENDER_STATE);
expect(instance._testJournal.lifeCycleInLaterRender).toBe(
ComponentLifeCycle.MOUNTED
'MOUNTED'
);

expect(getLifeCycleState(instance)).toBe(ComponentLifeCycle.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(
ComponentLifeCycle.MOUNTED
'MOUNTED'
);

// But the current lifecycle of the component is unmounted.
expect(getLifeCycleState(instance)).toBe(ComponentLifeCycle.UNMOUNTED);
expect(getLifeCycleState(instance)).toBe('UNMOUNTED');
expect(instance.state).toEqual(POST_WILL_UNMOUNT_STATE);
});

Expand Down