Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-tracking",
"version": "7.2.1",
"version": "7.2.2",
"description": "Declarative tracking for React apps.",
"keywords": [
"declarative",
Expand Down
9 changes: 6 additions & 3 deletions src/__tests__/trackEventMethodDecorator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ describe('trackEventMethodDecorator', () => {
const dummyData = {};
const trackingData = jest.fn(() => dummyData);
const trackEvent = jest.fn();
const spyTestEvent = jest.fn();
const eventData = 'eventData';
const spyTestEvent = jest.fn(() => eventData);
const dummyArgument = 'x';

class TestClass {
Expand All @@ -90,11 +91,12 @@ describe('trackEventMethodDecorator', () => {
}

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

// Access the trackingData arguments
const trackingDataArguments = trackingData.mock.calls[0];

expect(result).toEqual(eventData);
expect(trackingData).toHaveBeenCalledTimes(1);
expect(trackingDataArguments[0]).toEqual(myTC.props);
expect(trackingDataArguments[1]).toEqual(myTC.state);
Expand Down Expand Up @@ -131,11 +133,12 @@ describe('trackEventMethodDecorator', () => {
}

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

// Access the trackingData arguments
const trackingDataArguments = trackingData.mock.calls[0];

expect(result).toEqual(dummyResolve);
expect(trackingData).toHaveBeenCalledTimes(1);
expect(trackingDataArguments[0]).toEqual(myTC.props);
expect(trackingDataArguments[1]).toEqual(myTC.state);
Expand Down
13 changes: 9 additions & 4 deletions src/trackEventMethodDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ export default function trackEventMethodDecorator(trackingData = {}) {

const fn = Reflect.apply(decoratedFn, this, args);
if (Promise && Promise.resolve(fn) === fn) {
return fn.then(trackEvent.bind(this)).catch(error => {
trackEvent(null, error);
throw error;
});
return fn
.then(trackEvent.bind(this))
.then(() => {
return fn;
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: mind rewriting as:

.then(() => fn)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed with d7adf1f
I also added an e2e test as suggested.

When writing the test, I noticed that the tracking function breaks if a promise is rejected. It will fail on { value } since value is set to null after reject. So when using the example in the readme:

@track((props, state, methodArgs, [{ value }, err]) => { } <-- will give TypeError

.catch(error => { trackEvent(null, error); throw error; }); <-- trackEventMethodDecorators.js line 28, value is set to null

So the happy flow is working now, but maybe this should be fixed as well. (changing null to {}?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yea that makes sense, good catch! Mind making that change as a separate PR (will need an accompanying test update)

.catch(error => {
trackEvent(null, error);
throw error;
});
}
trackEvent();
return fn;
Expand Down