Skip to content

Commit 598e437

Browse files
committed
Fix enzyme 3 errors
Change in behavior in enzyme 3. Mount wrapper is a component, which has no classes. For a brief discussion see: enzymejs/enzyme#1170 Enzyme 3 enables increased life cycle calls by default, which were disabled in enzyme 2. Further discussion: enzymejs/enzyme#1145 Workaround for React 16 requestAnimationFrame error in jest Further discussion: https://stackoverflow.com/a/43950142/583830
1 parent 703d267 commit 598e437

13 files changed

+33
-43
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
"tsx",
2727
"js"
2828
],
29+
"setupFiles": [
30+
"<rootDir>/src/jest/setupFile.ts"
31+
],
32+
"setupTestFrameworkScriptFile": "<rootDir>/src/jest/setupTestFrameworkScriptFile.ts",
2933
"browser": true,
3034
"verbose": true
3135
},
@@ -85,7 +89,7 @@
8589
"prettier": "1.7.0",
8690
"prop-types": "15.5.10",
8791
"react": "^16.0.0",
88-
"react-dom": "16.0.0",
92+
"react-dom": "^16.0.0",
8993
"react-test-renderer": "^16.0.0",
9094
"recompose": "0.25.0",
9195
"ts-jest": "21.0.1",

src/__tests__/cluster.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ import { withContext } from 'recompose';
1515
import { mount } from 'enzyme';
1616
const PropTypes = require('prop-types'); // tslint:disable-line
1717

18-
const Enzyme = require('enzyme');
19-
const Adapter = require('enzyme-adapter-react-16');
20-
Enzyme.configure({ adapter: new Adapter() });
21-
2218
const coordinates = [
2319
[-12.408741828510017, 58.16339752811908],
2420
[-5.668629523822517, 50.06970856327533],

src/__tests__/geojson-layer.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { withContext } from 'recompose';
44
import { mount } from 'enzyme';
55
const PropTypes = require('prop-types'); // tslint:disable-line
66

7-
const Enzyme = require('enzyme');
8-
const Adapter = require('enzyme-adapter-react-16');
9-
Enzyme.configure({ adapter: new Adapter() });
10-
117
describe('GeoJSONLayer', () => {
128
let GeoJSONLayerWithContext: any;
139
let addLayerMock = jest.fn();

src/__tests__/layer.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { withContext } from 'recompose';
44
import { mount } from 'enzyme';
55
const PropTypes = require('prop-types'); // tslint:disable-line
66

7-
const Enzyme = require('enzyme');
8-
const Adapter = require('enzyme-adapter-react-16');
9-
Enzyme.configure({ adapter: new Adapter() });
10-
117
describe('Layer', () => {
128
let LayerWithContext: any;
139
let addLayerMock = jest.fn();

src/__tests__/map.test.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@ import * as React from 'react';
1515
import ReactMapboxGl, { FitBounds } from '../map';
1616
import { mount } from 'enzyme';
1717

18-
const Enzyme = require('enzyme');
19-
const Adapter = require('enzyme-adapter-react-16');
20-
Enzyme.configure({ adapter: new Adapter() });
21-
2218
describe('Map', () => {
2319
let mapState;
24-
console.log('Enzyme');
25-
console.log(Enzyme);
2620
beforeEach(() => {
2721
mockfitBounds = jest.fn();
2822
mockon = jest.fn();

src/__tests__/popup.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import { withContext } from 'recompose';
1212
import { mount } from 'enzyme';
1313
const PropTypes = require('prop-types'); // tslint:disable-line
1414

15-
const Enzyme = require('enzyme');
16-
const Adapter = require('enzyme-adapter-react-16');
17-
Enzyme.configure({ adapter: new Adapter() });
18-
1915
describe('Popup', () => {
2016
let PopupWithContext: any;
2117

@@ -50,7 +46,11 @@ describe('Popup', () => {
5046
const wrapper = mount(
5147
<PopupWithContext className="custom-classname" coordinates={[0, 0]} />
5248
);
53-
54-
expect(wrapper.find('Popup').hasClass(defaultClassName[0])).toEqual(true);
49+
expect(
50+
wrapper
51+
.find('Popup')
52+
.childAt(0)
53+
.hasClass(defaultClassName[0])
54+
).toEqual(true);
5555
});
5656
});

src/__tests__/rotation-control.test.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import * as React from 'react';
22
import RotationControl from '../rotation-control';
33
import { shallow } from 'enzyme';
44

5-
const Enzyme = require('enzyme');
6-
const Adapter = require('enzyme-adapter-react-16');
7-
Enzyme.configure({ adapter: new Adapter() });
8-
95
describe('RotationControl', () => {
106
it('should render the component', () => {
11-
const wrapper = shallow(<RotationControl />);
7+
const wrapper = shallow(<RotationControl />, {
8+
disableLifecycleMethods: true
9+
});
1210

1311
expect(wrapper).toBeDefined();
1412
});

src/__tests__/scale-control.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { shallow } from 'enzyme';
44
import { withContext } from 'recompose';
55
const PropTypes = require('prop-types'); // tslint:disable-line
66

7-
const Enzyme = require('enzyme');
8-
const Adapter = require('enzyme-adapter-react-16');
9-
Enzyme.configure({ adapter: new Adapter() });
10-
117
describe('ScaleControl', () => {
128
let ScaleControlWithContext: any;
139

src/__tests__/source.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { withContext } from 'recompose';
44
import { mount } from 'enzyme';
55
const PropTypes = require('prop-types'); // tslint:disable-line
66

7-
const Enzyme = require('enzyme');
8-
const Adapter = require('enzyme-adapter-react-16');
9-
Enzyme.configure({ adapter: new Adapter() });
10-
117
describe('Source', () => {
128
let SourceWithContext: any;
139
let addSourceMock: any;

src/__tests__/zoom-control.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import * as React from 'react';
22
import ZoomControl from '../zoom-control';
33
import { shallow } from 'enzyme';
44

5-
const Enzyme = require('enzyme');
6-
const Adapter = require('enzyme-adapter-react-16');
7-
Enzyme.configure({ adapter: new Adapter() });
8-
95
describe('ZoomControl', () => {
106
it('should render the component', () => {
117
const wrapper = shallow(<ZoomControl />);

src/jest/setupFile.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as React from 'react';
2+
3+
// Resolution for requestAnimationFrame not supported in jest error :
4+
// https://github.com/facebook/react/issues/9102#issuecomment-283873039
5+
// https://stackoverflow.com/a/43950142/583830
6+
7+
global.window = global;
8+
window.addEventListener = () => {}; // tslint:disable-line
9+
window.requestAnimationFrame = () => {
10+
throw new Error('requestAnimationFrame is not supported in Node');
11+
};
12+
13+
module.exports = React;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as Enzyme from 'enzyme';
2+
import * as Adapter from 'enzyme-adapter-react-16';
3+
4+
Enzyme.configure({ adapter: new Adapter() });

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"lib",
3232
"src/__tests__",
3333
"example",
34-
"lib"
34+
"lib",
35+
"src/jest"
3536
]
3637
}

0 commit comments

Comments
 (0)