From 73b73ba74f33b7852d9fd27802733f162ff60b29 Mon Sep 17 00:00:00 2001 From: Stuart Colville Date: Fri, 3 Jun 2016 20:44:54 +0100 Subject: [PATCH] Fix tracking and add tests --- src/disco/containers/InstallButton.js | 13 +-- .../disco/containers/TestInstallButton.js | 98 +++++++++++++++++++ 2 files changed, 105 insertions(+), 6 deletions(-) diff --git a/src/disco/containers/InstallButton.js b/src/disco/containers/InstallButton.js index 7de8e9e1475..09cc58094c6 100644 --- a/src/disco/containers/InstallButton.js +++ b/src/disco/containers/InstallButton.js @@ -7,6 +7,7 @@ import config from 'config'; import { AddonManager } from 'disco/addonManager'; import { DOWNLOADING, + EXTENSION_TYPE, INSTALLED, INSTALL_CATEGORY, THEME_INSTALL, @@ -115,7 +116,7 @@ export function makeProgressHandler(dispatch, guid) { }; } -export function mapDispatchToProps(dispatch) { +export function mapDispatchToProps(dispatch, { _tracking = tracking } = {}) { if (config.get('server')) { return {}; } @@ -135,13 +136,13 @@ export function mapDispatchToProps(dispatch) { install({ guid, installURL, name }) { const addonManager = new AddonManager(guid, installURL, makeProgressHandler(dispatch, guid)); dispatch({type: 'START_DOWNLOAD', payload: {guid}}); - tracking.sendEvent({action: 'addon', category: INSTALL_CATEGORY, label: name}); + _tracking.sendEvent({action: 'addon', category: INSTALL_CATEGORY, label: name}); return addonManager.install(); }, installTheme(node, guid, name, _themeAction = themeAction) { _themeAction(node, THEME_INSTALL); - tracking.sendEvent({action: 'theme', category: INSTALL_CATEGORY, label: name}); + _tracking.sendEvent({action: 'theme', category: INSTALL_CATEGORY, label: name}); return new Promise((resolve) => { setTimeout(() => { dispatch({type: 'INSTALL_STATE', payload: {guid, status: INSTALLED}}); @@ -154,10 +155,10 @@ export function mapDispatchToProps(dispatch) { const addonManager = new AddonManager(guid, installURL); dispatch({type: 'START_UNINSTALL', payload: {guid}}); const action = { - ADDON_TYPE: 'addon', - THEME_TYPE: 'theme', + [EXTENSION_TYPE]: 'addon', + [THEME_TYPE]: 'theme', }[type] || 'invalid'; - tracking.sendEvent({action, category: UNINSTALL_CATEGORY, label: name}); + _tracking.sendEvent({action, category: UNINSTALL_CATEGORY, label: name}); return addonManager.uninstall() .then(() => dispatch({type: 'UNINSTALL_COMPLETE', payload: {guid}})); }, diff --git a/tests/client/disco/containers/TestInstallButton.js b/tests/client/disco/containers/TestInstallButton.js index e8e9b663275..d1aee96a86e 100644 --- a/tests/client/disco/containers/TestInstallButton.js +++ b/tests/client/disco/containers/TestInstallButton.js @@ -11,11 +11,13 @@ import { } from 'disco/containers/InstallButton'; import { DOWNLOADING, + INSTALL_CATEGORY, INSTALLED, INSTALLING, THEME_INSTALL, THEME_TYPE, UNINSTALLED, + UNINSTALL_CATEGORY, UNINSTALLING, UNKNOWN, } from 'disco/constants'; @@ -276,6 +278,25 @@ describe('', () => { }); }); + it('tracks an addon inntall', () => { + stubAddonManager(); + const name = 'hai-addon'; + const type = 'extension'; + const dispatch = sinon.spy(); + const fakeTracking = { + sendEvent: sinon.spy(), + }; + const { install } = mapDispatchToProps(dispatch, {_tracking: fakeTracking}); + return install({guid, installURL, name, type}) + .then(() => { + assert(fakeTracking.sendEvent.calledWith({ + action: 'addon', + category: INSTALL_CATEGORY, + label: 'hai-addon', + })); + }); + }); + it('should dispatch START_DOWNLOAD', () => { stubAddonManager(); const dispatch = sinon.spy(); @@ -303,6 +324,63 @@ describe('', () => { }); }); + it('tracks an addon uninstall', () => { + stubAddonManager(); + const dispatch = sinon.spy(); + const name = 'whatevs'; + const type = 'extension'; + const fakeTracking = { + sendEvent: sinon.spy(), + }; + const { uninstall } = mapDispatchToProps(dispatch, {_tracking: fakeTracking}); + return uninstall({guid, installURL, name, type}) + .then(() => { + assert.ok(fakeTracking.sendEvent.calledWith({ + action: 'addon', + category: UNINSTALL_CATEGORY, + label: 'whatevs', + }), 'correctly called'); + }); + }); + + it('tracks a theme uninstall', () => { + stubAddonManager(); + const dispatch = sinon.spy(); + const name = 'whatevs'; + const type = 'persona'; + const fakeTracking = { + sendEvent: sinon.spy(), + }; + const { uninstall } = mapDispatchToProps(dispatch, {_tracking: fakeTracking}); + return uninstall({guid, installURL, name, type}) + .then(() => { + assert(fakeTracking.sendEvent.calledWith({ + action: 'theme', + category: UNINSTALL_CATEGORY, + label: 'whatevs', + })); + }); + }); + + it('tracks a unknown type uninstall', () => { + stubAddonManager(); + const dispatch = sinon.spy(); + const name = 'whatevs'; + const type = 'foo'; + const fakeTracking = { + sendEvent: sinon.spy(), + }; + const { uninstall } = mapDispatchToProps(dispatch, {_tracking: fakeTracking}); + return uninstall({guid, installURL, name, type}) + .then(() => { + assert(fakeTracking.sendEvent.calledWith({ + action: 'invalid', + category: UNINSTALL_CATEGORY, + label: 'whatevs', + })); + }); + }); + it('should dispatch START_UNINSTALL', () => { stubAddonManager(); const dispatch = sinon.spy(); @@ -332,6 +410,26 @@ describe('', () => { })); }); }); + + it('tracks a theme install', () => { + const name = 'hai-theme'; + const guid = '{install-theme}'; + const node = sinon.stub(); + const dispatch = sinon.spy(); + const spyThemeAction = sinon.spy(); + const fakeTracking = { + sendEvent: sinon.spy(), + }; + const { installTheme } = mapDispatchToProps(dispatch, {_tracking: fakeTracking}); + return installTheme(node, guid, name, spyThemeAction) + .then(() => { + assert(fakeTracking.sendEvent.calledWith({ + action: 'theme', + category: INSTALL_CATEGORY, + label: 'hai-theme', + })); + }); + }); }); describe('mapDispatchToProps', () => {