react: react-test-renderer/shallow setState callback is never called
Do you want to request a feature or report a bug?
Bug, following on from https://github.com/airbnb/enzyme/issues/953
What is the current behavior?
In node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js, the callback second argument to setState is never called.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/84v837e9/).
import ShallowRenderer from 'react-test-renderer/shallow'
const renderer = new ShallowRenderer()
renderer.render(<YourComponent />)
const instance = renderer.getMountedInstance()
instance.methodThatCallsSetState()
// => callback passed into setState as the second argument within methodThatCallsSetState is never called
The shallow renderer code is as follows:
Updater.prototype.enqueueSetState = function enqueueSetState(publicInstance, partialState, callback, callerName) {
if (typeof partialState === 'function') {
partialState = partialState(publicInstance.state, publicInstance.props);
}
this._renderer._newState = _extends({}, publicInstance.state, partialState);
this._renderer.render(this._renderer._element, this._renderer._context);
};
As you can see, the callback is ignored. In the deep renderer (node_modules/react-test-renderer/cjs/react-test-renderer.development.js), the callback is used:
enqueueSetState: function (instance, partialState, callback) {
var fiber = ReactInstanceMap_1.get(instance);
var priorityLevel = getPriorityContext(fiber, false);
callback = callback === undefined ? null : callback;
{
warnOnInvalidCallback(callback, 'setState');
}
addUpdate$1(fiber, partialState, callback, priorityLevel);
scheduleUpdate(fiber, priorityLevel);
}
What is the expected behavior?
The callback is called.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React v16, react-test-renderer v16. I don’t know if this worked before.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 15 (7 by maintainers)
@koba04 @bvaughn All done! See #10106
Looks correct, unfortunately. The
ReactShallowRendererEntryimplementation doesn’t use thecallbackparam at all .@Leeds-eBooks You’re looking for
ReactTestUtils-test.Please feel free to assign the PR with this failing test to me. I can review and fix.
Search for existing tests importing shallow renderer? I bet it’s called something like ReactShallowRenderer-test.