Skip to content

Commit b2b5002

Browse files
authored
Fix dispatchOnMount behavior when process() is defined (#103)
1 parent 585da27 commit b2b5002

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/__tests__/withTrackingComponentDecorator.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,34 @@ describe('withTrackingComponentDecorator', () => {
150150
});
151151
});
152152

153+
describe('with process option from parent is falsey and dispatchOnMount option is true', () => {
154+
const props = { props: 1 };
155+
const trackingContext = { page: 1 };
156+
const process = jest.fn(() => null);
157+
const context = { context: 1, tracking: { process } };
158+
const dispatchOnMount = true;
159+
160+
@withTrackingComponentDecorator(trackingContext, { dispatchOnMount })
161+
class TestComponent {
162+
static displayName = 'TestComponent';
163+
}
164+
165+
const myTC = new TestComponent(props, context);
166+
167+
beforeEach(() => {
168+
mockDispatchTrackingEvent.mockClear();
169+
});
170+
171+
it('dispatches only once when process and dispatchOnMount functions are passed', () => {
172+
myTC.componentDidMount();
173+
expect(process).toHaveBeenCalled();
174+
expect(mockDispatchTrackingEvent).toHaveBeenCalledWith({
175+
page: 1,
176+
});
177+
expect(mockDispatchTrackingEvent).toHaveBeenCalledTimes(1);
178+
});
179+
});
180+
153181
describe('with a prop called tracking that has two functions as keys', () => {
154182
const dummyData = { page: 1 };
155183

src/withTrackingComponentDecorator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default function withTrackingComponentDecorator(
7777
);
7878
} else if (typeof contextProcess === 'function') {
7979
const processed = contextProcess(this.ownTrackingData);
80-
if (processed) {
80+
if (processed || dispatchOnMount === true) {
8181
this.trackEvent(processed);
8282
}
8383
} else if (typeof dispatchOnMount === 'function') {

0 commit comments

Comments
 (0)