Skip to content
Permalink
Browse files

[enzyme-adapter-react-helper] Ensure that the correct version of an a…

…dapter gets installed

Particularly when only specifying a major version of React to match against. When we pass in something like `16`, we really mean "any React 16 version". Essentially >= 16 and < 17.

To get that behavior, we need to treat our input as a Range, and get its intersection with the value we test against. This is basically where we were prior to airbnb#2116, except that pre-release version support has been dropped, which fixes the issue that PR was attempting to fix.
  • Loading branch information...
ahuth committed May 15, 2019
1 parent 152493a commit bddb81b13c55a2f61b702b678e7d16c92cd16a27
@@ -1,30 +1,30 @@
import semver from 'semver';

export default function getAdapterForReactVersion(reactVersion) {
const normalizedVersion = semver.coerce(reactVersion);
const versionRange = semver.validRange(reactVersion);

if (semver.satisfies(normalizedVersion, '^16.4.0-0')) {
if (semver.intersects(versionRange, '^16.4.0')) {
return 'enzyme-adapter-react-16';
}
if (semver.satisfies(normalizedVersion, '~16.3.0-0')) {
if (semver.intersects(versionRange, '~16.3.0')) {
return 'enzyme-adapter-react-16.3';
}
if (semver.satisfies(normalizedVersion, '~16.2')) {
if (semver.intersects(versionRange, '~16.2')) {
return 'enzyme-adapter-react-16.2';
}
if (semver.satisfies(normalizedVersion, '~16.0.0-0 || ~16.1')) {
if (semver.intersects(versionRange, '~16.0.0 || ~16.1')) {
return 'enzyme-adapter-react-16.1';
}
if (semver.satisfies(normalizedVersion, '^15.5.0')) {
if (semver.intersects(versionRange, '^15.5.0')) {
return 'enzyme-adapter-react-15';
}
if (semver.satisfies(normalizedVersion, '15.0.0 - 15.4.x')) {
if (semver.intersects(versionRange, '15.0.0 - 15.4.x')) {
return 'enzyme-adapter-react-15.4';
}
if (semver.satisfies(normalizedVersion, '^0.14.0')) {
if (semver.intersects(versionRange, '^0.14.0')) {
return 'enzyme-adapter-react-14';
}
if (semver.satisfies(normalizedVersion, '^0.13.0')) {
if (semver.intersects(versionRange, '^0.13.0')) {
return 'enzyme-adapter-react-13';
}

@@ -4,6 +4,8 @@ import getAdapterForReactVersion from 'enzyme-adapter-react-helper/src/getAdapte
describe('enzyme-adapter-react-helper', () => {
describe('getAdapterForReactVersion', () => {
it('returns "enzyme-adapter-react-16" when intended', () => {
expect(getAdapterForReactVersion('16')).to.equal('enzyme-adapter-react-16');

expect(getAdapterForReactVersion('16.8')).to.equal('enzyme-adapter-react-16');
expect(getAdapterForReactVersion('16.8.0')).to.equal('enzyme-adapter-react-16');
expect(getAdapterForReactVersion('16.8.6')).to.equal('enzyme-adapter-react-16');
@@ -45,6 +47,8 @@ describe('enzyme-adapter-react-helper', () => {
});

it('returns "enzyme-adapter-react-15" when intended', () => {
expect(getAdapterForReactVersion('15')).to.equal('enzyme-adapter-react-15');

expect(getAdapterForReactVersion('15.6')).to.equal('enzyme-adapter-react-15');
expect(getAdapterForReactVersion('15.6.0')).to.equal('enzyme-adapter-react-15');
expect(getAdapterForReactVersion('15.6.1')).to.equal('enzyme-adapter-react-15');

0 comments on commit bddb81b

Please sign in to comment.
You can’t perform that action at this time.