Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Access to component state data #45

Merged
merged 7 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/__tests__/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jest.setMock('../dispatchTrackingEvent', dispatchTrackingEvent);
const testDataContext = { testDataContext: true };
const testData = { testData: true };
const dispatch = jest.fn();
const testState = { booleanState: true };

describe('e2e', () => {
// eslint-disable-next-line global-require
Expand Down Expand Up @@ -362,6 +363,35 @@ describe('e2e', () => {
expect(dispatch).toHaveBeenCalledTimes(2); // pageview event and simulated button click
});

it('dispatches state data when components contain state', () => {
@track(testDataContext, { dispatch })
class TestOptions extends React.Component {
constructor() {
super();
this.state = {
booleanState: true,
};
}

@track((props, state) => ({ booleanState: state.booleanState }))
exampleMethod = () => {}

render() {
this.exampleMethod();
return <div />;
}
}

mount(<TestOptions />);


expect(dispatchTrackingEvent).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
...testDataContext,
...testState,
});
});

it('logs a console error when there is already a process defined on context', () => {
global.console.error = jest.fn();
const process = () => {};
Expand Down
40 changes: 40 additions & 0 deletions src/__tests__/trackEventMethodDecorator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,44 @@ describe('trackEventMethodDecorator', () => {
expect(trackEvent).toHaveBeenCalledWith(dummyData);
expect(spyTestEvent).toHaveBeenCalledWith('x');
});

it('properly passes through the correct arguments when trackingData is a function', () => {
const dummyData = {};
const trackingData = jest.fn(() => dummyData);
const trackEvent = jest.fn();
const spyTestEvent = jest.fn();
const dummyArgument = 'x';

class TestClass {
constructor() {
this.props = {
tracking: {
trackEvent,
},
};
this.state = {
myState: "someState",
};
}

@trackEventMethodDecorator(trackingData)
handleTestEvent = spyTestEvent
}

const myTC = new TestClass();
myTC.handleTestEvent(dummyArgument);

// Access the trackingData arguments
const trackingDataArguments = trackingData.mock.calls[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


expect(trackingData).toHaveBeenCalledTimes(1);
expect(trackingDataArguments[0]).toEqual(myTC.props);
expect(trackingDataArguments[1]).toEqual(myTC.state);
// Here we have access to the raw `arguments` object, which is not an actual Array,
// so in order to compare, we convert the arguments to an array.
expect(Array.from(trackingDataArguments[2])).toEqual([dummyArgument]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I tried:

expect(trackingData).toHaveBeenCalledWith(myTC.props, myTC.state, spyTestEvent.mock.calls[0]) 
// also tried ['x'] or [dummyArgument] in this case

However, you end up comparing an argument with an Array, which will not resolve. Hence we extract the arguments one by one, convert this specific case to an Array, which ends up comparing just fine 馃憣

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thanks for the comment as well 馃憤


expect(trackEvent).toHaveBeenCalledWith(dummyData);
expect(spyTestEvent).toHaveBeenCalledWith(dummyArgument);
});
});
2 changes: 1 addition & 1 deletion src/trackEventMethodDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function trackEventMethodDecorator(trackingData = {}) {
return makeClassMemberDecorator(decoratedFn => function decorateClassMember() {
if (this.props && this.props.tracking && typeof this.props.tracking.trackEvent === 'function') {
const thisTrackingData = typeof trackingData === 'function'
? trackingData(this.props, arguments)
? trackingData(this.props, this.state, arguments)
: trackingData;
this.props.tracking.trackEvent(thisTrackingData);
}
Expand Down