Skip to content

Commit b3f2b3a

Browse files
authored
⬆️ react 16 / jest / delete promise support (#77)
1 parent cd438d9 commit b3f2b3a

File tree

10 files changed

+1189
-478
lines changed

10 files changed

+1189
-478
lines changed

build/core.js

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
88

99
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
1010

11-
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /* eslint react/prop-types: 0 */
12-
13-
1411
var _react = require('react');
1512

1613
var _react2 = _interopRequireDefault(_react);
@@ -23,7 +20,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
2320

2421
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
2522

26-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
23+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* eslint react/prop-types: 0 */
24+
2725

2826
var getTypeOf = function getTypeOf(something) {
2927
var getType = {};
@@ -41,11 +39,6 @@ var isString = function isString(stringToCheck) {
4139
return type && type === '[object String]';
4240
};
4341

44-
// https://github.com/then/is-promise/blob/master/index.js
45-
var isPromise = function isPromise(promise) {
46-
return !!promise && ((typeof promise === 'undefined' ? 'undefined' : _typeof(promise)) === 'object' || typeof promise === 'function') && typeof promise.then === 'function';
47-
};
48-
4942
var getDisplayName = function getDisplayName(c) {
5043
return c.displayName || c.name || 'Component';
5144
};
@@ -59,7 +52,6 @@ exports.default = function () {
5952
var loadFunctionName = isString(load) ? load : 'load';
6053
var isPrintArray = Array.isArray(print);
6154
var isPrintFunction = isFunction(print);
62-
var isPrintPromise = isPromise(print);
6355
var isLoadFunction = isFunction(load);
6456

6557
var isLoaded = function isLoaded(props, state, context) {
@@ -87,10 +79,6 @@ exports.default = function () {
8779
return !!print(props, context);
8880
}
8981

90-
if (isPrintPromise) {
91-
return state.promiseLoaded;
92-
}
93-
9482
// Anything else
9583
return !!print;
9684
};
@@ -115,10 +103,7 @@ exports.default = function () {
115103
}
116104

117105
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = _class.__proto__ || Object.getPrototypeOf(_class)).call.apply(_ref2, [this].concat(args))), _this), _this.state = {
118-
props: {},
119-
promiseLoaded: false
120-
}, _this.promiseResolved = function () {
121-
_this.setState({ promiseLoaded: true });
106+
props: {}
122107
}, _this.omitLoadInProps = function (props) {
123108
var isLoadAFunction = isFunction(props[loadFunctionName]);
124109

@@ -139,23 +124,11 @@ exports.default = function () {
139124
_createClass(_class, [{
140125
key: 'componentWillMount',
141126
value: function componentWillMount() {
142-
var _this2 = this;
143-
144127
// Load from hoc argument
145128
if (isLoadFunction) {
146129
load(this.props, this.context);
147130
}
148131

149-
if (isPrintPromise) {
150-
print.then(function (value) {
151-
_this2.promiseResolved();
152-
return value;
153-
}).catch(function (error) {
154-
_this2.promiseResolved();
155-
throw error;
156-
});
157-
}
158-
159132
// Load from props
160133
if (this.omitLoadInProps(this.props)) {
161134
this.props[loadFunctionName](this.props, this.context);

misc/rebuild_refs.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

misc/requestAnimationFrame.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global.requestAnimationFrame = (cb) => {
2+
setTimeout(cb, 0)
3+
}

misc/testSetup.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
import { jsdom } from 'jsdom'
2-
import chai from 'chai'
3-
import spies from 'chai-spies'
1+
import './requestAnimationFrame'
2+
import Enzyme from 'enzyme'
3+
import Adapter from 'enzyme-adapter-react-16'
44

5-
global.document = jsdom('<!doctype html><html><body></body></html>')
6-
global.window = document.defaultView
7-
global.navigator = global.window.navigator
8-
9-
/** Configuring chai. */
10-
chai.should()
11-
chai.config.includeStack = true
12-
chai.config.truncateThreshold = 0
13-
chai.use(spies)
14-
15-
// Make console.error a real failure
16-
console.error = (message) => { // eslint-disable-line no-console
17-
throw new Error(message)
18-
}
5+
Enzyme.configure({ adapter: new Adapter() })

package.json

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
{
22
"name": "hoc-react-loader",
3-
"version": "6.1.0",
3+
"version": "6.1.1",
44
"description": "Higher order component to call a load function from props at mount.",
55
"main": "build/index.js",
66
"peerDependencies": {
7-
"prop-types": "^15.5.8",
8-
"react": "^15.3.0",
9-
"tinycolor2": "^1.4.1"
7+
"prop-types": "^15.x",
8+
"react": "^15.x || ^16.x",
9+
"tinycolor2": "^1.x"
10+
},
11+
"scripts": {
12+
"lint": "find src -iname \"*.jsx\" -exec eslint {} +; find src -iname \"*.js\" -exec eslint {} +;",
13+
"build": "cross-env BABEL_ENV=cjs babel --ignore \"*.spec.js\" ./src/ --out-dir build",
14+
"test": "jest --collectCoverageFrom=src/**/*.js --collectCoverageFrom=src/**/*.jsx",
15+
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls",
16+
"ci": "npm-run-all --parallel lint coveralls"
17+
1018
},
1119
"devDependencies": {
1220
"babel-cli": "^6.24.0",
1321
"babel-core": "^6.24.0",
1422
"babel-eslint": "^7.2.1",
23+
"babel-jest": "^21.2.0",
1524
"babel-preset-es2015": "^6.24.0",
1625
"babel-preset-es2017": "^6.22.0",
1726
"babel-preset-react": "^6.23.0",
@@ -21,32 +30,25 @@
2130
"chai-spies": "^0.7.1",
2231
"coveralls": "^2.12.0",
2332
"cross-env": "^3.2.4",
24-
"enzyme": "^2.8.0",
33+
"enzyme": "^3.1.0",
34+
"enzyme-adapter-react-16": "^1.0.2",
2535
"eslint": "^3.18.0",
2636
"eslint-config-airbnb": "^14.1.0",
2737
"eslint-plugin-import": "^2.2.0",
2838
"eslint-plugin-jsx-a11y": "^4.0.0",
2939
"eslint-plugin-react": "^6.10.3",
40+
"jest": "^21.2.1",
3041
"jsdom": "^9.12.0",
3142
"lodash": "^4.17.4",
3243
"mocha": "^3.2.0",
3344
"npm-run-all": "^4.1.1",
3445
"nyc": "^10.2.0",
35-
"prop-types": "^15.5.8",
36-
"react": "^15.3.0",
37-
"react-addons-test-utils": "^15.4.2",
38-
"react-dom": "^15.3.0",
39-
"react-test-renderer": "^15.5.4",
46+
"prop-types": "^15.6.0",
47+
"react": "^16.0.0",
48+
"react-dom": "^16.0.0",
49+
"react-test-renderer": "^16.0.0",
4050
"tinycolor2": "^1.4.1"
4151
},
42-
"scripts": {
43-
"lint": "find src -iname \"*.jsx\" -exec eslint {} +; find src -iname \"*.js\" -exec eslint {} +;",
44-
"build": "cross-env BABEL_ENV=cjs babel --ignore \"*.spec.js\" ./src/ --out-dir build",
45-
"test": "mocha --recursive --compilers js:babel-register --require ./misc/testSetup.js \"src/**/*.spec.js\" ",
46-
"coverage": "nyc --extension .jsx npm test",
47-
"coveralls": "nyc --extension .jsx npm test && nyc report --reporter=text-lcov | coveralls",
48-
"ci": "npm-run-all --parallel lint coveralls"
49-
},
5052
"repository": {
5153
"type": "git",
5254
"url": "git+https://github.com/Zenika/hoc-react-loader.git"
@@ -92,5 +94,10 @@
9294
"import/no-unresolved": 0,
9395
"react/forbid-prop-types": 0
9496
}
97+
},
98+
"jest": {
99+
"setupFiles": [
100+
"./misc/testSetup.js"
101+
]
95102
}
96103
}

src/TailSpin/TailSpin.spec.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
/* eslint-env mocha */
1+
/* eslint-env jest */
2+
23
/* eslint-disable
3-
no-unused-expressions,
4-
react/jsx-filename-extension,
5-
import/no-extraneous-dependencies
4+
react/jsx-filename-extension
65
*/
76

87
import React from 'react'
98
import { mount } from 'enzyme'
10-
import blanket from 'blanket' // eslint-disable-line
11-
import TailSpin from './TailSpin'
9+
import TailSpin from './index'
1210

1311
const testColor = (component, color) => {
14-
component.find('circle').props().fill.should.be.deep.equals(color)
12+
expect(component.find('circle').props().fill).toEqual(color)
1513
}
1614

1715
describe('TailSpin', () => {
@@ -60,5 +58,3 @@ describe('TailSpin', () => {
6058
)
6159
})
6260
})
63-
64-
/* eslint-enable no-unused-expressions */
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`hoc-react-loader print parameter should not print Component -loaded props- 1`] = `"<div class=\\"tailspin\\">TailSpin</div>"`;
4+
5+
exports[`hoc-react-loader print parameter should not print Component -loaded props- 2`] = `"<div class=\\"tailspin\\">TailSpin</div>"`;
6+
7+
exports[`hoc-react-loader print parameter should not print Component -print as a value- 1`] = `"<div class=\\"tailspin\\">TailSpin</div>"`;
8+
9+
exports[`hoc-react-loader print parameter should not print Component -print as function- 1`] = `"<div class=\\"tailspin\\">TailSpin</div>"`;
10+
11+
exports[`hoc-react-loader print parameter should not print Component -print as function- 2`] = `
12+
Array [
13+
Array [
14+
Object {
15+
"some": "props",
16+
},
17+
Object {},
18+
],
19+
]
20+
`;
21+
22+
exports[`hoc-react-loader print parameter should print Component -empty parameters- 1`] = `"<div class=\\"component\\">Component</div>"`;
23+
24+
exports[`hoc-react-loader print parameter should print Component -loaded props- 1`] = `"<div class=\\"component\\">Component</div>"`;
25+
26+
exports[`hoc-react-loader print parameter should print Component -loaded props- 2`] = `"<div class=\\"component\\">Component</div>"`;
27+
28+
exports[`hoc-react-loader print parameter should print Component -print as a value- 1`] = `"<div class=\\"component\\">Component</div>"`;
29+
30+
exports[`hoc-react-loader print parameter should print Component -print as an array- 1`] = `"<div class=\\"tailspin\\">TailSpin</div>"`;
31+
32+
exports[`hoc-react-loader print parameter should print Component -print as an array- 2`] = `"<div class=\\"tailspin\\">TailSpin</div>"`;
33+
34+
exports[`hoc-react-loader print parameter should print Component -print as an array- 3`] = `"<div class=\\"tailspin\\">TailSpin</div>"`;
35+
36+
exports[`hoc-react-loader print parameter should print Component -print as an array- 4`] = `"<div class=\\"component\\">Component</div>"`;
37+
38+
exports[`hoc-react-loader print parameter should print Component -print as function- 1`] = `"<div class=\\"component\\">Component</div>"`;
39+
40+
exports[`hoc-react-loader print parameter should print Component -print as function- 2`] = `
41+
Array [
42+
Array [
43+
Object {
44+
"some": "props",
45+
},
46+
Object {},
47+
],
48+
]
49+
`;
50+
51+
exports[`hoc-react-loader print parameter should print Component -undefined parameters- 1`] = `"<div class=\\"component\\">Component</div>"`;

src/core.jsx

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ const isString = (stringToCheck) => {
1717
return type && type === '[object String]'
1818
}
1919

20-
// https://github.com/then/is-promise/blob/master/index.js
21-
const isPromise = (promise) => {
22-
return !!promise && (typeof promise === 'object' || typeof promise === 'function') && typeof promise.then === 'function'
23-
}
24-
2520
const getDisplayName = c => c.displayName || c.name || 'Component'
2621

2722
export default (
@@ -34,7 +29,6 @@ export default (
3429
const loadFunctionName = isString(load) ? load : 'load'
3530
const isPrintArray = Array.isArray(print)
3631
const isPrintFunction = isFunction(print)
37-
const isPrintPromise = isPromise(print)
3832
const isLoadFunction = isFunction(load)
3933

4034
const isLoaded = (props, state, context) => {
@@ -59,10 +53,6 @@ export default (
5953
return !!print(props, context)
6054
}
6155

62-
if (isPrintPromise) {
63-
return state.promiseLoaded
64-
}
65-
6656
// Anything else
6757
return !!print
6858
}
@@ -75,11 +65,8 @@ export default (
7565

7666
state = {
7767
props: {},
78-
promiseLoaded: false,
7968
}
8069

81-
promiseResolved = () => { this.setState({ promiseLoaded: true }) }
82-
8370
omitLoadInProps = (props) => {
8471
const isLoadAFunction = isFunction(props[loadFunctionName])
8572

@@ -103,18 +90,6 @@ export default (
10390
load(this.props, this.context)
10491
}
10592

106-
if (isPrintPromise) {
107-
print
108-
.then((value) => {
109-
this.promiseResolved()
110-
return value
111-
})
112-
.catch((error) => {
113-
this.promiseResolved()
114-
throw error
115-
})
116-
}
117-
11893
// Load from props
11994
if (this.omitLoadInProps(this.props)) {
12095
this.props[loadFunctionName](this.props, this.context)

0 commit comments

Comments
 (0)