Skip to content

Commit 459801d

Browse files
williardxtizmagik
authored andcommitted
Don't invoke trackEvent when tracking data is null or undefined (#105)
* Don't invoke trackEvent when tracking data is null or undefined * Update README Co-Authored-By: williardx <[email protected]>
1 parent aa5fc1a commit 459801d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ When tracking asynchronous methods, you can also receive the resolved or rejecte
292292
// ...
293293
```
294294

295+
If the function returns a falsy value (e.g. `false`, `null` or `undefined`) then the tracking call will not be made.
296+
295297
### Accessing data stored in the component's `props` and `state`
296298

297299
Further runtime data, such as the component's `props` and `state`, are available as follows:

Diff for: src/__tests__/trackEventMethodDecorator.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,34 @@ describe('trackEventMethodDecorator', () => {
148148
expect(spyTestEvent).toHaveBeenCalledWith(dummyArgument);
149149
});
150150

151+
[null, undefined].forEach(value => {
152+
it(`does not call trackEvent if the data is ${value}`, () => {
153+
const trackingData = jest.fn(() => value);
154+
const trackEvent = jest.fn();
155+
const spyTestEvent = jest.fn();
156+
157+
class TestClass {
158+
constructor() {
159+
this.props = {
160+
tracking: {
161+
trackEvent,
162+
},
163+
};
164+
}
165+
166+
@trackEventMethodDecorator(trackingData)
167+
handleTestEvent = spyTestEvent;
168+
}
169+
170+
const myTC = new TestClass();
171+
myTC.handleTestEvent('x');
172+
173+
expect(trackingData).toHaveBeenCalledTimes(1);
174+
expect(trackEvent).not.toHaveBeenCalled();
175+
expect(spyTestEvent).toHaveBeenCalledWith('x');
176+
});
177+
});
178+
151179
it('properly calls trackData when an async method has resolved', async () => {
152180
const dummyData = {};
153181
const trackingData = jest.fn(() => dummyData);

Diff for: src/trackEventMethodDecorator.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export default function trackEventMethodDecorator(trackingData = {}) {
1414
typeof trackingData === 'function'
1515
? trackingData(this.props, this.state, args, promiseArguments)
1616
: trackingData;
17-
this.props.tracking.trackEvent(thisTrackingData);
17+
if (thisTrackingData) {
18+
this.props.tracking.trackEvent(thisTrackingData);
19+
}
1820
}
1921
};
2022

0 commit comments

Comments
 (0)