Skip to content

Commit

Permalink
Fix ReactShallowRenderer callback bug on componentWillMount (facebook…
Browse files Browse the repository at this point in the history
  • Loading branch information
accordeiro committed Nov 9, 2017
1 parent 035eb3d commit 856cb2b
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ReactShallowRenderer {
'ReactShallowRenderer render(): Invalid component element.%s',
typeof element === 'function'
? ' Instead of passing a component class, make sure to instantiate ' +
'it by passing it to React.createElement.'
'it by passing it to React.createElement.'
: '',
);
// Show a special message for host elements since it's a common case.
Expand Down Expand Up @@ -139,6 +139,7 @@ class ReactShallowRenderer {
}

this._rendered = this._instance.render();
this._updater._invokeCallback();
// Intentionally do not call componentDidMount()
// because DOM refs are not available.
}
Expand Down Expand Up @@ -183,6 +184,7 @@ class ReactShallowRenderer {

if (shouldUpdate) {
this._rendered = this._instance.render();
this._updater._invokeCallback();
}
// Intentionally do not call componentDidUpdate()
// because DOM refs are not available.
Expand All @@ -192,31 +194,41 @@ class ReactShallowRenderer {
class Updater {
constructor(renderer) {
this._renderer = renderer;
this._callback = null;
this._publicInstance = null;
}

_updateCallback(callback, publicInstance) {
if (typeof callback === 'function') {
this._callback = callback;
this._publicInstance = publicInstance;
}
}

_invokeCallback() {
if (typeof this._callback === 'function' && this._publicInstance) {
this._callback.call(this._publicInstance);
}
}

isMounted(publicInstance) {
return !!this._renderer._element;
}

enqueueForceUpdate(publicInstance, callback, callerName) {
this._updateCallback(callback, publicInstance);
this._renderer._forcedUpdate = true;
this._renderer.render(this._renderer._element, this._renderer._context);

if (typeof callback === 'function') {
callback.call(publicInstance);
}
}

enqueueReplaceState(publicInstance, completeState, callback, callerName) {
this._updateCallback(callback, publicInstance);
this._renderer._newState = completeState;
this._renderer.render(this._renderer._element, this._renderer._context);

if (typeof callback === 'function') {
callback.call(publicInstance);
}
}

enqueueSetState(publicInstance, partialState, callback, callerName) {
this._updateCallback(callback, publicInstance);
const currentState = this._renderer._newState || publicInstance.state;

if (typeof partialState === 'function') {
Expand All @@ -229,10 +241,6 @@ class Updater {
};

this._renderer.render(this._renderer._element, this._renderer._context);

if (typeof callback === 'function') {
callback.call(publicInstance);
}
}
}

Expand Down

0 comments on commit 856cb2b

Please sign in to comment.