diff --git a/packages/react-test-renderer/src/ReactShallowRenderer.js b/packages/react-test-renderer/src/ReactShallowRenderer.js index 1958f43d7bd79..bd2f318d95aed 100644 --- a/packages/react-test-renderer/src/ReactShallowRenderer.js +++ b/packages/react-test-renderer/src/ReactShallowRenderer.js @@ -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. @@ -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. } @@ -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. @@ -192,6 +194,21 @@ 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) { @@ -199,24 +216,19 @@ class Updater { } 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') { @@ -229,10 +241,6 @@ class Updater { }; this._renderer.render(this._renderer._element, this._renderer._context); - - if (typeof callback === 'function') { - callback.call(publicInstance); - } } }