Skip to content

Commit bfafa8a

Browse files
committedMar 9, 2019
Merge branch 'release/1.4.0'
2 parents b0554ad + a386a4b commit bfafa8a

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed
 

‎package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-component-update",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "Extends the native React Component to streamline updates",
55
"main": "lib/index.js",
66
"files": [
@@ -73,8 +73,5 @@
7373
"testMatch": [
7474
"**/src/*.spec.js?(x)"
7575
]
76-
},
77-
"dependencies": {
78-
"lodash.wrap": "^4.1.1"
7976
}
8077
}

‎src/withEvents.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
import wrap from 'lodash.wrap';
2-
31
function noop() {}
42

3+
/**
4+
* Intercept calls to nativeFunc and pass the arguments to customWrapperFunc.
5+
* @param {function=} nativeFunc
6+
* @param {function} customWrapperFunc
7+
*/
8+
function wrap(nativeFunc = noop, customWrapperFunc) {
9+
return function wrappedNativeFunc(...args) {
10+
customWrapperFunc.call(this, nativeFunc.bind(this), ...args);
11+
};
12+
}
13+
514
export default function withEvents(config) {
6-
function willMountCustom(nativeFunc = noop, ...args) {
15+
function willMountCustom(nativeFunc, ...args) {
716
const result = nativeFunc(...args);
817
this.componentWillMountOrReceiveProps(this.props);
918
return result;
1019
}
1120

12-
function didMountCustom(nativeFunc = noop, ...args) {
21+
function didMountCustom(nativeFunc, ...args) {
1322
const result = nativeFunc(...args);
1423
this.componentDidMountOrUpdate(this.props, this.state);
1524
return result;
1625
}
1726

18-
function willReceivePropsCustom(nativeFunc = noop, ...args) {
27+
function willReceivePropsCustom(nativeFunc, ...args) {
1928
const result = nativeFunc(...args);
2029
this.componentWillMountOrReceiveProps(...args);
2130
return result;
2231
}
2332

24-
function didUpdateCustom(nativeFunc = noop, ...args) {
33+
function didUpdateCustom(nativeFunc, ...args) {
2534
const result = nativeFunc(...args);
2635
this.componentDidMountOrUpdate(...args);
2736
return result;

‎src/withEvents.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ describe('withEvents extension with overrides', () => {
197197
expect(userComponentWillMount).to.have.been.calledBefore(callbackWill);
198198
});
199199

200+
it('runs user code in override on mount with "this" context of component', () => {
201+
expect(userComponentWillMount).to.have.been.calledOn(component.instance());
202+
});
203+
200204
it('runs on props update', () => {
201205
component.setProps(getUniqueProps());
202206
expect(callbackWill).to.have.been.calledTwice();
@@ -207,6 +211,11 @@ describe('withEvents extension with overrides', () => {
207211
expect(userComponentWillReceiveProps).to.have.been.calledBefore(callbackWill.secondCall);
208212
});
209213

214+
it('runs user code in override on props update with "this" context of component', () => {
215+
component.setProps(getUniqueProps());
216+
expect(userComponentWillReceiveProps).to.have.been.calledOn(component.instance());
217+
});
218+
210219
it('does not run on state update', () => {
211220
component.setState(getUniqueState());
212221
expect(callbackWill).to.have.been.calledOnce();
@@ -222,6 +231,10 @@ describe('withEvents extension with overrides', () => {
222231
expect(userComponentDidMount).to.have.been.calledBefore(callbackDid);
223232
});
224233

234+
it('runs user code in override on mount with "this" context of component', () => {
235+
expect(userComponentDidMount).to.have.been.calledOn(component.instance());
236+
});
237+
225238
it('runs on props update', () => {
226239
component.setProps(getUniqueProps());
227240
expect(callbackDid).to.have.been.calledTwice();
@@ -232,6 +245,11 @@ describe('withEvents extension with overrides', () => {
232245
expect(userComponentDidUpdate).to.have.been.calledBefore(callbackDid.secondCall);
233246
});
234247

248+
it('runs user code in override on props update with "this" context of component', () => {
249+
component.setProps(getUniqueProps());
250+
expect(userComponentDidUpdate).to.have.been.calledOn(component.instance());
251+
});
252+
235253
it('runs on state update', () => {
236254
component.setState(getUniqueState());
237255
expect(callbackDid).to.have.been.calledTwice();
@@ -241,5 +259,10 @@ describe('withEvents extension with overrides', () => {
241259
component.setState(getUniqueState());
242260
expect(userComponentDidUpdate).to.have.been.calledBefore(callbackDid.secondCall);
243261
});
262+
263+
it('runs user code in override on state update with "this" context of component', () => {
264+
component.setState(getUniqueState());
265+
expect(userComponentDidUpdate).to.have.been.calledOn(component.instance());
266+
});
244267
});
245268
});

0 commit comments

Comments
 (0)