Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wimpyprogrammer committed Mar 9, 2019
2 parents b0554ad + a386a4b commit bfafa8a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-component-update",
"version": "1.3.0",
"version": "1.4.0",
"description": "Extends the native React Component to streamline updates",
"main": "lib/index.js",
"files": [
Expand Down Expand Up @@ -73,8 +73,5 @@
"testMatch": [
"**/src/*.spec.js?(x)"
]
},
"dependencies": {
"lodash.wrap": "^4.1.1"
}
}
21 changes: 15 additions & 6 deletions src/withEvents.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import wrap from 'lodash.wrap';

function noop() {}

/**
* Intercept calls to nativeFunc and pass the arguments to customWrapperFunc.
* @param {function=} nativeFunc
* @param {function} customWrapperFunc
*/
function wrap(nativeFunc = noop, customWrapperFunc) {
return function wrappedNativeFunc(...args) {
customWrapperFunc.call(this, nativeFunc.bind(this), ...args);
};
}

export default function withEvents(config) {
function willMountCustom(nativeFunc = noop, ...args) {
function willMountCustom(nativeFunc, ...args) {
const result = nativeFunc(...args);
this.componentWillMountOrReceiveProps(this.props);
return result;
}

function didMountCustom(nativeFunc = noop, ...args) {
function didMountCustom(nativeFunc, ...args) {
const result = nativeFunc(...args);
this.componentDidMountOrUpdate(this.props, this.state);
return result;
}

function willReceivePropsCustom(nativeFunc = noop, ...args) {
function willReceivePropsCustom(nativeFunc, ...args) {
const result = nativeFunc(...args);
this.componentWillMountOrReceiveProps(...args);
return result;
}

function didUpdateCustom(nativeFunc = noop, ...args) {
function didUpdateCustom(nativeFunc, ...args) {
const result = nativeFunc(...args);
this.componentDidMountOrUpdate(...args);
return result;
Expand Down
23 changes: 23 additions & 0 deletions src/withEvents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ describe('withEvents extension with overrides', () => {
expect(userComponentWillMount).to.have.been.calledBefore(callbackWill);
});

it('runs user code in override on mount with "this" context of component', () => {
expect(userComponentWillMount).to.have.been.calledOn(component.instance());
});

it('runs on props update', () => {
component.setProps(getUniqueProps());
expect(callbackWill).to.have.been.calledTwice();
Expand All @@ -207,6 +211,11 @@ describe('withEvents extension with overrides', () => {
expect(userComponentWillReceiveProps).to.have.been.calledBefore(callbackWill.secondCall);
});

it('runs user code in override on props update with "this" context of component', () => {
component.setProps(getUniqueProps());
expect(userComponentWillReceiveProps).to.have.been.calledOn(component.instance());
});

it('does not run on state update', () => {
component.setState(getUniqueState());
expect(callbackWill).to.have.been.calledOnce();
Expand All @@ -222,6 +231,10 @@ describe('withEvents extension with overrides', () => {
expect(userComponentDidMount).to.have.been.calledBefore(callbackDid);
});

it('runs user code in override on mount with "this" context of component', () => {
expect(userComponentDidMount).to.have.been.calledOn(component.instance());
});

it('runs on props update', () => {
component.setProps(getUniqueProps());
expect(callbackDid).to.have.been.calledTwice();
Expand All @@ -232,6 +245,11 @@ describe('withEvents extension with overrides', () => {
expect(userComponentDidUpdate).to.have.been.calledBefore(callbackDid.secondCall);
});

it('runs user code in override on props update with "this" context of component', () => {
component.setProps(getUniqueProps());
expect(userComponentDidUpdate).to.have.been.calledOn(component.instance());
});

it('runs on state update', () => {
component.setState(getUniqueState());
expect(callbackDid).to.have.been.calledTwice();
Expand All @@ -241,5 +259,10 @@ describe('withEvents extension with overrides', () => {
component.setState(getUniqueState());
expect(userComponentDidUpdate).to.have.been.calledBefore(callbackDid.secondCall);
});

it('runs user code in override on state update with "this" context of component', () => {
component.setState(getUniqueState());
expect(userComponentDidUpdate).to.have.been.calledOn(component.instance());
});
});
});

0 comments on commit bfafa8a

Please sign in to comment.