Skip to content

Commit

Permalink
Fix issue 59 (#60)
Browse files Browse the repository at this point in the history
* e2e test - will dispatch different data if props changed

* Fix Issue #59 dispatch after state updates should be reflected in tracking data

* Fix eslint

* Fix eslint

* Fix eslint
  • Loading branch information
tanhauhau authored and tizmagik committed Oct 17, 2017
1 parent dcbb8e3 commit 17da891
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
53 changes: 53 additions & 0 deletions src/__tests__/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,57 @@ describe('e2e', () => {

expect(global.console.error).toHaveBeenCalledTimes(1);
});

it('will dispatch different data if props changed', () => {
@track(props => ({ data: props.data }))
class Top extends React.Component {
render() {
return this.props.children;
}
}

@track({ page: 'Page' })
class Page extends React.Component {
@track({ event: 'buttonClick' })
handleClick = jest.fn();
render() {
return <span onClick={this.handleClick}>Click Me</span>; // eslint-disable-line jsx-a11y/no-static-element-interactions
}
}

@track({}, { dispatch })
class App extends React.Component {
constructor(props) {
super(props);
this.state = { data: 1 };
}
render() {
return (
<div>
<button onClick={() => this.setState({ data: 2 })} />
<Top data={this.state.data}>
<Page />
</Top>
</div>
);
}
}

const wrappedApp = mount(<App />);

wrappedApp.find('span').simulate('click');
expect(dispatch).toHaveBeenCalledWith({
data: 1,
event: 'buttonClick',
page: 'Page',
});

wrappedApp.find('button').simulate('click');
wrappedApp.find('span').simulate('click');
expect(dispatch).toHaveBeenCalledWith({
data: 2,
event: 'buttonClick',
page: 'Page',
});
});
});
30 changes: 19 additions & 11 deletions src/withTrackingComponentDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,7 @@ export default function withTrackingComponentDecorator(
);
}

this.ownTrackingData =
typeof trackingData === 'function'
? trackingData(props)
: trackingData;
this.contextTrackingData =
(this.context.tracking && this.context.tracking.data) || {};
this.trackingData = merge(
{},
this.contextTrackingData,
this.ownTrackingData
);
this.computeTrackingData(props, context);
}

static displayName = `WithTracking(${decoratedComponentName})`;
Expand All @@ -64,6 +54,20 @@ export default function withTrackingComponentDecorator(
);
}

computeTrackingData(props, context) {
this.ownTrackingData =
typeof trackingData === 'function'
? trackingData(props)
: trackingData;
this.contextTrackingData =
(context.tracking && context.tracking.data) || {};
this.trackingData = merge(
{},
this.contextTrackingData,
this.ownTrackingData
);
}

getChildContext() {
return {
tracking: {
Expand Down Expand Up @@ -103,6 +107,10 @@ export default function withTrackingComponentDecorator(
}
}

componentWillReceiveProps(nextProps, nextContext) {
this.computeTrackingData(nextProps, nextContext);
}

tracking = {
trackEvent: this.trackEvent,
getTrackingData: () => this.trackingData,
Expand Down

0 comments on commit 17da891

Please sign in to comment.