Skip to content

Commit

Permalink
Implement adapter.invokeSetStateCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and ljharb committed Jan 4, 2018
1 parent 04076f6 commit ba82e1d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
11 changes: 11 additions & 0 deletions packages/enzyme-adapter-react-15.4/src/ReactFifteenFourAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import {
propsWithKeysAndRef,
} from 'enzyme-adapter-utils';

const MINOR_VERSION = React.version.split('.')[1];
const CALLING_SETSTATE_CALLBACK_WITH_UNDEFINED = MINOR_VERSION >= 4;

function compositeTypeToNodeType(type) {
switch (type) {
case 0:
Expand Down Expand Up @@ -252,6 +255,14 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
createElement(...args) {
return React.createElement(...args);
}

invokeSetStateCallback(instance, callback) {
if (CALLING_SETSTATE_CALLBACK_WITH_UNDEFINED) {
callback.call(instance, undefined);
} else {
callback.call(instance);
}
}
}

module.exports = ReactFifteenFourAdapter;
4 changes: 4 additions & 0 deletions packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ class ReactFifteenAdapter extends EnzymeAdapter {
createElement(...args) {
return React.createElement(...args);
}

invokeSetStateCallback(instance, callback) {
callback.call(instance, undefined);
}
}

module.exports = ReactFifteenAdapter;
9 changes: 7 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { ITERATOR_SYMBOL, withSetStateAllowed, sym } from 'enzyme/build/Utils';
import './_helpers/setupAdapters';
import { createClass } from './_helpers/react-compat';
import { describeIf, itIf, itWithData, generateEmptyRenderData } from './_helpers';
import { REACT013, REACT014, REACT16, is } from './_helpers/version';
import { REACT013, REACT014, REACT16, REACT154, REACT155, REACT156, is } from './_helpers/version';

// The shallow renderer in react 16 does not yet support batched updates. When it does,
// we should be able to go un-skip all of the tests that are skipped with this flag.
const BATCHING = !REACT16;


// some React versions pass undefined as an argument of setState callback.
const CALLING_SETSTATE_CALLBACK_WITH_UNDEFINED = REACT154 || REACT155 || REACT156;

const getElementPropSelector = prop => x => x.props[prop];
const getWrapperPropSelector = prop => x => x.prop(prop);

Expand Down Expand Up @@ -1378,10 +1382,11 @@ describe('shallow', () => {
}
const wrapper = shallow(<Foo />);
expect(wrapper.state()).to.eql({ id: 'foo' });
wrapper.setState({ id: 'bar' }, function callback() {
wrapper.setState({ id: 'bar' }, function callback(...args) {
expect(wrapper.state()).to.eql({ id: 'bar' });
expect(this.state).to.eql({ id: 'bar' });
expect(wrapper.find('div').prop('className')).to.eql('bar');
expect(args).to.eql(CALLING_SETSTATE_CALLBACK_WITH_UNDEFINED ? [undefined] : []);
});
});

Expand Down
2 changes: 2 additions & 0 deletions packages/enzyme-test-suite/test/_helpers/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const [major, minor] = VERSION.split('.');
export const REACT013 = VERSION.slice(0, 4) === '0.13';
export const REACT014 = VERSION.slice(0, 4) === '0.14';
export const REACT15 = major === '15';
export const REACT154 = REACT15 && minor >= 4;
export const REACT155 = REACT15 && minor >= 5;
export const REACT156 = REACT15 && minor >= 6;
export const REACT16 = major === '16';

export function gt(v) { return semver.gt(VERSION, v); }
Expand Down
5 changes: 5 additions & 0 deletions packages/enzyme/src/EnzymeAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class EnzymeAdapter {
createElement(type, props, ...children) {
throw unimplementedError('createElement', 'EnzymeAdapter');
}

// eslint-disable-next-line class-methods-use-this
invokeSetStateCallback(instance, callback) {
callback.call(instance);
}
}

EnzymeAdapter.MODES = {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ class ShallowWrapper {
this.update();
// call the setState callback
if (callback) {
callback.call(instance);
adapter.invokeSetStateCallback(instance, callback);
}
});
});
Expand Down

0 comments on commit ba82e1d

Please sign in to comment.