Skip to content

Commit

Permalink
Merge pull request #1267 from cozy/feat/tracker
Browse files Browse the repository at this point in the history
Feat/tracker
  • Loading branch information
probot-auto-merge[bot] authored Nov 25, 2019
2 parents 0e35dd9 + d6c9307 commit 88fdea8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
21 changes: 21 additions & 0 deletions react/helpers/tracker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,24 @@ export const resetTracker = () => {
trackerInstance = null
}
}
/**
*
* @param {array} event Event to track ['Drive', 'action', 'label']
* @param {object} trackerForTest Mocked Tracker for test purpose
*/
export const trackEvent = (event, trackerForTest) => {
const tracker = trackerForTest || getTracker()
if (tracker) {
let trackEventArray = []
if (event && event[0]) {
//Like that, we can do trackEvent(['Drive', 'option1']) without thinking of adding this `trackEvent` attr
if (event[0] !== 'trackEvent') {
trackEventArray = event.unshift('trackEvent')
}

trackEventArray = event
}

tracker.push(trackEventArray)
}
}
29 changes: 29 additions & 0 deletions react/helpers/tracker.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as trackerFile from './tracker'

jest.mock('./tracker', () => ({
...require.requireActual('./tracker'),
getTracker: jest.fn()
}))
describe('tracker / piwik action', () => {
beforeEach(() => {
trackerFile.getTracker.mockImplementation(() => ({ push: jest.fn() }))
})

afterEach(() => {
jest.resetAllMocks()
jest.restoreAllMocks()
})

it('should track event', () => {
const tracker = trackerFile.getTracker()
trackerFile.trackEvent(['trackEvent', 'toto'], tracker)
expect(tracker.push).toHaveBeenCalledWith(['trackEvent', 'toto'])
})
it('should add or not the trackEvent item in the array', () => {
const tracker = trackerFile.getTracker()
trackerFile.trackEvent(['toto'], tracker)
expect(tracker.push).toHaveBeenCalledWith(['trackEvent', 'toto'])
trackerFile.trackEvent(['trackEvent', 'foo'], tracker)
expect(tracker.push).toHaveBeenCalledWith(['trackEvent', 'foo'])
})
})

0 comments on commit 88fdea8

Please sign in to comment.