From b7e165cdba4cee551a930d34f18e6ddfc6515e0e Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Wed, 15 Jun 2022 09:15:08 +0300 Subject: [PATCH] Include reason for checking for updates in analytic event Signed-off-by: Janne Savolainen --- .../analytics-for-installing-update.test.ts | 101 +++++++++++++++--- .../downgrading-version-update.test.ts | 2 +- .../installing-update.test.ts | 4 +- .../selection-of-update-stability.test.ts | 18 ++-- .../check-for-updates-tray-item.injectable.ts | 2 +- ...process-checking-for-updates.injectable.ts | 4 +- ...periodical-check-for-updates.injectable.ts | 4 +- .../menu/application-menu-items.injectable.ts | 5 +- 8 files changed, 104 insertions(+), 36 deletions(-) diff --git a/src/behaviours/application-update/analytics-for-installing-update.test.ts b/src/behaviours/application-update/analytics-for-installing-update.test.ts index 07eebccf1f..c33a3f92af 100644 --- a/src/behaviours/application-update/analytics-for-installing-update.test.ts +++ b/src/behaviours/application-update/analytics-for-installing-update.test.ts @@ -17,6 +17,7 @@ import type { DownloadPlatformUpdate } from "../../main/application-update/downl import downloadPlatformUpdateInjectable from "../../main/application-update/download-platform-update/download-platform-update.injectable"; import quitAndInstallUpdateInjectable from "../../main/application-update/quit-and-install-update.injectable"; import appVersionInjectable from "../../common/get-configuration-file-model/app-version/app-version.injectable"; +import periodicalCheckForUpdatesInjectable from "../../main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable"; describe("analytics for installing update", () => { let applicationBuilder: ApplicationBuilder; @@ -26,6 +27,8 @@ describe("analytics for installing update", () => { let mainDi: DiContainer; beforeEach(async () => { + jest.useFakeTimers(); + global.Date.now = () => new Date("2015-10-21T07:28:00Z").getTime(); applicationBuilder = getApplicationBuilder(); @@ -55,32 +58,70 @@ describe("analytics for installing update", () => { }); mainDi = applicationBuilder.dis.mainDi; - - await applicationBuilder.render(); }); - it("sends event to analytics about the current version", () => { - expect(analyticsListenerMock).toHaveBeenCalledWith({ - name: "app", - action: "current-version", + describe("given application is started and checking updates periodically", () => { + beforeEach(async () => { + mainDi.unoverride(periodicalCheckForUpdatesInjectable); + mainDi.permitSideEffects(periodicalCheckForUpdatesInjectable); + + await applicationBuilder.render(); - params: { - version: "42.0.0", - currentDateTime: "2015-10-21T07:28:00Z", - }, }); - }); - describe("when checking for updates", () => { - beforeEach(() => { + it("sends event to analytics for being checked periodically", () => { + expect(analyticsListenerMock).toHaveBeenCalledWith({ + name: "app", + action: "checking-for-updates", + + params: { + currentDateTime: "2015-10-21T07:28:00Z", + source: "periodic", + }, + }); + }); + + it("when enough time passes to check for updates again, sends event to analytics for being checked periodically", () => { analyticsListenerMock.mockClear(); - const processCheckingForUpdates = mainDi.inject(processCheckingForUpdatesInjectable); + jest.advanceTimersByTime(1000 * 60 * 60 * 2); - processCheckingForUpdates(); + expect(analyticsListenerMock).toHaveBeenCalledWith({ + name: "app", + action: "checking-for-updates", + + params: { + currentDateTime: "2015-10-21T07:28:00Z", + source: "periodic", + }, + }); + }); + }); + + describe("when application is started", () => { + beforeEach(async () => { + analyticsListenerMock.mockClear(); + + await applicationBuilder.render(); }); - it("sends event to analytics about checking for updates", () => { + it("sends event to analytics about the current version", () => { + expect(analyticsListenerMock).toHaveBeenCalledWith({ + name: "app", + action: "current-version", + + params: { + version: "42.0.0", + currentDateTime: "2015-10-21T07:28:00Z", + }, + }); + }); + + it("when checking for updates using tray, sends event to analytics for being checked from tray", async () => { + analyticsListenerMock.mockClear(); + + applicationBuilder.tray.click("check-for-updates"); + expect(analyticsListenerMock.mock.calls).toEqual([ [ { @@ -89,14 +130,40 @@ describe("analytics for installing update", () => { params: { currentDateTime: "2015-10-21T07:28:00Z", + source: "tray", + }, + }, + ], + ]); + + }); + + it("when checking for updates using application menu, sends event to analytics for being checked from application menu", async () => { + analyticsListenerMock.mockClear(); + + applicationBuilder.applicationMenu.click("root.check-for-updates"); + + expect(analyticsListenerMock.mock.calls).toEqual([ + [ + { + name: "app", + action: "checking-for-updates", + + params: { + currentDateTime: "2015-10-21T07:28:00Z", + source: "application-menu", }, }, ], ]); }); - describe("when check for updates resolves with new update being available", () => { + describe("given checking for updates, when check for updates resolves with new update being available", () => { beforeEach(async () => { + const processCheckingForUpdates = mainDi.inject(processCheckingForUpdatesInjectable); + + processCheckingForUpdates("irrelevant"); + analyticsListenerMock.mockClear(); await checkForPlatformUpdatesMock.resolve({ diff --git a/src/behaviours/application-update/downgrading-version-update.test.ts b/src/behaviours/application-update/downgrading-version-update.test.ts index e0b154bb21..2abefa1ac8 100644 --- a/src/behaviours/application-update/downgrading-version-update.test.ts +++ b/src/behaviours/application-update/downgrading-version-update.test.ts @@ -77,7 +77,7 @@ describe("downgrading version update", () => { const processCheckingForUpdates = mainDi.inject(processCheckingForUpdatesInjectable); - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); expect(checkForPlatformUpdatesMock).toHaveBeenCalledWith(expect.any(Object), { allowDowngrade: downgradeIsAllowed }); }); diff --git a/src/behaviours/application-update/installing-update.test.ts b/src/behaviours/application-update/installing-update.test.ts index 2d7a749b2d..14cd107598 100644 --- a/src/behaviours/application-update/installing-update.test.ts +++ b/src/behaviours/application-update/installing-update.test.ts @@ -67,7 +67,7 @@ describe("installing update", () => { describe("when started", () => { let rendered: RenderResult; - let processCheckingForUpdates: () => Promise; + let processCheckingForUpdates: (source: string) => Promise; beforeEach(async () => { rendered = await applicationBuilder.render(); @@ -83,7 +83,7 @@ describe("installing update", () => { let processCheckingForUpdatesPromise: Promise; beforeEach(async () => { - processCheckingForUpdatesPromise = processCheckingForUpdates(); + processCheckingForUpdatesPromise = processCheckingForUpdates("irrelevant"); }); it("checks for updates", () => { diff --git a/src/behaviours/application-update/selection-of-update-stability.test.ts b/src/behaviours/application-update/selection-of-update-stability.test.ts index 49eb68469c..1fd6e22dbd 100644 --- a/src/behaviours/application-update/selection-of-update-stability.test.ts +++ b/src/behaviours/application-update/selection-of-update-stability.test.ts @@ -72,7 +72,7 @@ describe("selection of update stability", () => { describe("when started", () => { let rendered: RenderResult; - let processCheckingForUpdates: () => Promise; + let processCheckingForUpdates: (source: string) => Promise; beforeEach(async () => { rendered = await applicationBuilder.render(); @@ -97,7 +97,7 @@ describe("selection of update stability", () => { selectedUpdateChannel.setValue(updateChannels.alpha.id); - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); }); it('checks updates from update channel "alpha"', () => { @@ -191,7 +191,7 @@ describe("selection of update stability", () => { describe("when checking for updates", () => { beforeEach(() => { - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); }); describe('when update from "beta" channel is discovered', () => { @@ -241,7 +241,7 @@ describe("selection of update stability", () => { const processCheckingForUpdates = applicationBuilder.dis.mainDi.inject(processCheckingForUpdatesInjectable); - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); expect(checkForPlatformUpdatesMock).toHaveBeenCalledWith(updateChannels.beta, expect.any(Object)); }); @@ -259,7 +259,7 @@ describe("selection of update stability", () => { const processCheckingForUpdates = applicationBuilder.dis.mainDi.inject(processCheckingForUpdatesInjectable); - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); expect(checkForPlatformUpdatesMock).toHaveBeenCalledWith(updateChannels.latest, expect.any(Object)); }); @@ -273,7 +273,7 @@ describe("selection of update stability", () => { const processCheckingForUpdates = applicationBuilder.dis.mainDi.inject(processCheckingForUpdatesInjectable); - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); expect(checkForPlatformUpdatesMock).toHaveBeenCalledWith( updateChannels.latest, @@ -290,7 +290,7 @@ describe("selection of update stability", () => { const processCheckingForUpdates = applicationBuilder.dis.mainDi.inject(processCheckingForUpdatesInjectable); - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); expect(checkForPlatformUpdatesMock).toHaveBeenCalledWith(updateChannels.alpha, expect.any(Object)); }); @@ -304,7 +304,7 @@ describe("selection of update stability", () => { const processCheckingForUpdates = applicationBuilder.dis.mainDi.inject(processCheckingForUpdatesInjectable); - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); expect(checkForPlatformUpdatesMock).toHaveBeenCalledWith(updateChannels.beta, expect.any(Object)); }); @@ -324,7 +324,7 @@ describe("selection of update stability", () => { const processCheckingForUpdates = applicationBuilder.dis.mainDi.inject(processCheckingForUpdatesInjectable); - processCheckingForUpdates(); + processCheckingForUpdates("irrelevant"); expect(checkForPlatformUpdatesMock).toHaveBeenCalledWith(updateChannels.beta, expect.any(Object)); }); diff --git a/src/main/application-update/check-for-updates-tray-item.injectable.ts b/src/main/application-update/check-for-updates-tray-item.injectable.ts index 5ff4be731a..29f39fa9d5 100644 --- a/src/main/application-update/check-for-updates-tray-item.injectable.ts +++ b/src/main/application-update/check-for-updates-tray-item.injectable.ts @@ -59,7 +59,7 @@ const checkForUpdatesTrayItemInjectable = getInjectable({ click: pipeline( async () => { - await processCheckingForUpdates(); + await processCheckingForUpdates("tray"); await showApplicationWindow(); }, diff --git a/src/main/application-update/check-for-updates/process-checking-for-updates.injectable.ts b/src/main/application-update/check-for-updates/process-checking-for-updates.injectable.ts index 840095789d..a7066468da 100644 --- a/src/main/application-update/check-for-updates/process-checking-for-updates.injectable.ts +++ b/src/main/application-update/check-for-updates/process-checking-for-updates.injectable.ts @@ -31,11 +31,11 @@ const processCheckingForUpdatesInjectable = getInjectable({ const withOrphanPromise = di.inject(withOrphanPromiseInjectable); const emitEvent = di.inject(emitEventInjectable); - return async () => { + return async (source: string) => { emitEvent({ name: "app", action: "checking-for-updates", - params: { currentDateTime: getCurrentDateTime() }, + params: { currentDateTime: getCurrentDateTime(), source }, }); broadcastChangeInUpdatingStatus({ eventId: "checking-for-updates" }); diff --git a/src/main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable.ts b/src/main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable.ts index 394383ee65..7315d2a09f 100644 --- a/src/main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable.ts +++ b/src/main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable.ts @@ -16,11 +16,11 @@ const periodicalCheckForUpdatesInjectable = getInjectable({ const TWO_HOURS = 1000 * 60 * 60 * 2; // Note: intentional orphan promise to make checking for updates happen in the background - processCheckingForUpdates(); + processCheckingForUpdates("periodic"); const intervalId = setInterval(() => { // Note: intentional orphan promise to make checking for updates happen in the background - processCheckingForUpdates(); + processCheckingForUpdates("periodic"); }, TWO_HOURS); return () => { diff --git a/src/main/menu/application-menu-items.injectable.ts b/src/main/menu/application-menu-items.injectable.ts index 4ea447f675..f223c539c6 100644 --- a/src/main/menu/application-menu-items.injectable.ts +++ b/src/main/menu/application-menu-items.injectable.ts @@ -71,9 +71,10 @@ const applicationMenuItemsInjectable = getInjectable({ }, ...ignoreIf(!updatingIsEnabled, [ { + id: "check-for-updates", label: "Check for updates", click() { - processCheckingForUpdates().then(() => showApplicationWindow()); + processCheckingForUpdates("application-menu").then(() => showApplicationWindow()); }, }, ]), @@ -285,7 +286,7 @@ const applicationMenuItemsInjectable = getInjectable({ { label: "Check for updates", click() { - processCheckingForUpdates().then(() => + processCheckingForUpdates("periodic").then(() => showApplicationWindow(), ); },