-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
193 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { mount } from 'enzyme'; | ||
import React from 'react'; | ||
import { renderToString } from 'react-dom/server'; | ||
import track from '../withTrackingComponentDecorator'; | ||
import useTracking from '../useTracking'; | ||
|
||
describe('useTracking', () => { | ||
it('throws error if tracking context not present', () => { | ||
const ThrowMissingContext = () => { | ||
useTracking(); | ||
return <div>hi</div>; | ||
}; | ||
try { | ||
renderToString(<ThrowMissingContext />); | ||
} catch (error) { | ||
expect(error.message).toContain( | ||
'Attempting to call `useTracking` without a ReactTrackingContext present' | ||
); | ||
} | ||
}); | ||
|
||
it('dispatches tracking events from a useTracking hook tracking object', () => { | ||
const outerTrackingData = { | ||
page: 'Page', | ||
}; | ||
|
||
const dispatch = jest.fn(); | ||
|
||
const App = track(outerTrackingData, { dispatch })(() => { | ||
const tracking = useTracking(); | ||
|
||
expect(tracking.getTrackingData()).toEqual({ | ||
page: 'Page', | ||
}); | ||
|
||
return ( | ||
<button | ||
type="button" | ||
onClick={() => | ||
tracking.trackEvent({ | ||
event: 'buttonClick', | ||
}) | ||
} | ||
/> | ||
); | ||
}); | ||
|
||
const wrapper = mount(<App />); | ||
wrapper.simulate('click'); | ||
expect(dispatch).toHaveBeenCalledWith({ | ||
...outerTrackingData, | ||
event: 'buttonClick', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import merge from 'deepmerge'; | ||
import { useContext, useCallback } from 'react'; | ||
import { ReactTrackingContext } from './withTrackingComponentDecorator'; | ||
|
||
export default function useTracking() { | ||
const trackingContext = useContext(ReactTrackingContext); | ||
|
||
if (!(trackingContext && trackingContext.tracking)) { | ||
throw new Error( | ||
'Attempting to call `useTracking` ' + | ||
'without a ReactTrackingContext present. Did you forget to wrap the top of ' + | ||
'your component tree with `track`?' | ||
); | ||
} | ||
|
||
const trackEvent = useCallback( | ||
data => { | ||
return trackingContext.tracking.dispatch( | ||
merge(trackingContext.tracking.data, data) | ||
); | ||
}, | ||
[trackingContext.tracking.data] | ||
); | ||
|
||
return { | ||
getTrackingData: () => trackingContext.tracking.data, | ||
trackEvent, | ||
}; | ||
} |