1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Prevent opening of application if no new updates were downloaded when checking for updates using tray when application window is closed

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-07-29 09:04:20 +03:00
parent a68ce2b2be
commit 326c8a950c
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
7 changed files with 88 additions and 49 deletions

View File

@ -73,15 +73,14 @@ describe("encourage user to update when sufficient time passed since update was
}); });
describe("given the update check", () => { describe("given the update check", () => {
let processCheckingForUpdates: (source: string) => Promise<void>; let processCheckingForUpdates: (source: string) => Promise<{ updateWasDiscoveredAndDownloaded: boolean }>;
let processCheckingForUpdatesPromise: Promise<void>;
beforeEach(async () => { beforeEach(async () => {
processCheckingForUpdates = applicationBuilder.dis.mainDi.inject( processCheckingForUpdates = applicationBuilder.dis.mainDi.inject(
processCheckingForUpdatesInjectable, processCheckingForUpdatesInjectable,
); );
processCheckingForUpdatesPromise = processCheckingForUpdates("irrelevant"); processCheckingForUpdates("irrelevant");
}); });
describe("when update downloaded", () => { describe("when update downloaded", () => {
@ -94,7 +93,6 @@ describe("encourage user to update when sufficient time passed since update was
}); });
await downloadPlatformUpdateMock.resolve({ downloadWasSuccessful: true }); await downloadPlatformUpdateMock.resolve({ downloadWasSuccessful: true });
await processCheckingForUpdatesPromise;
button = rendered.getByTestId("update-button"); button = rendered.getByTestId("update-button");
}); });

View File

@ -13,13 +13,14 @@ import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest"; import asyncFn from "@async-fn/jest";
import type { DownloadPlatformUpdate } from "../../main/application-update/download-platform-update/download-platform-update.injectable"; import type { DownloadPlatformUpdate } from "../../main/application-update/download-platform-update/download-platform-update.injectable";
import downloadPlatformUpdateInjectable from "../../main/application-update/download-platform-update/download-platform-update.injectable"; import downloadPlatformUpdateInjectable from "../../main/application-update/download-platform-update/download-platform-update.injectable";
import showApplicationWindowInjectable from "../../main/start-main-application/lens-window/show-application-window.injectable"; import closeAllWindowsInjectable from "../../main/start-main-application/lens-window/hide-all-windows/close-all-windows.injectable";
import applicationWindowInjectable from "../../main/start-main-application/lens-window/application-window/application-window.injectable";
import type { LensWindow } from "../../main/start-main-application/lens-window/application-window/lens-window-injection-token";
describe("installing update using tray", () => { describe("installing update using tray", () => {
let applicationBuilder: ApplicationBuilder; let applicationBuilder: ApplicationBuilder;
let checkForPlatformUpdatesMock: AsyncFnMock<CheckForPlatformUpdates>; let checkForPlatformUpdatesMock: AsyncFnMock<CheckForPlatformUpdates>;
let downloadPlatformUpdateMock: AsyncFnMock<DownloadPlatformUpdate>; let downloadPlatformUpdateMock: AsyncFnMock<DownloadPlatformUpdate>;
let showApplicationWindowMock: jest.Mock;
beforeEach(() => { beforeEach(() => {
applicationBuilder = getApplicationBuilder(); applicationBuilder = getApplicationBuilder();
@ -27,9 +28,6 @@ describe("installing update using tray", () => {
applicationBuilder.beforeApplicationStart(({ mainDi }) => { applicationBuilder.beforeApplicationStart(({ mainDi }) => {
checkForPlatformUpdatesMock = asyncFn(); checkForPlatformUpdatesMock = asyncFn();
downloadPlatformUpdateMock = asyncFn(); downloadPlatformUpdateMock = asyncFn();
showApplicationWindowMock = jest.fn();
mainDi.override(showApplicationWindowInjectable, () => showApplicationWindowMock);
mainDi.override( mainDi.override(
checkForPlatformUpdatesInjectable, checkForPlatformUpdatesInjectable,
@ -61,15 +59,77 @@ describe("installing update using tray", () => {
expect(applicationBuilder.tray.get("install-update")).toBeNull(); expect(applicationBuilder.tray.get("install-update")).toBeNull();
}); });
describe("when user checks for updates using tray", () => { describe("given all application windows are closed, when checking for updates", () => {
let processCheckingForUpdatesPromise: Promise<void>; let applicationWindow: LensWindow;
let closeAllWindows: () => void;
beforeEach(async () => { beforeEach(() => {
processCheckingForUpdatesPromise = applicationBuilder.tray.click("check-for-updates"); const mainDi = applicationBuilder.dis.mainDi;
closeAllWindows = mainDi.inject(closeAllWindowsInjectable);
applicationWindow = mainDi.inject(applicationWindowInjectable);
closeAllWindows();
applicationBuilder.tray.click("check-for-updates");
}); });
it("does not show application window yet", () => { describe("when check for update resolves with new update", () => {
expect(showApplicationWindowMock).not.toHaveBeenCalled(); beforeEach(async () => {
await checkForPlatformUpdatesMock.resolve({
updateWasDiscovered: true,
version: "some-version",
});
});
it("does not show application window yet", () => {
expect(applicationWindow.isVisible).toBe(false);
});
describe("when download of update resolves with success", () => {
beforeEach(async () => {
await downloadPlatformUpdateMock.resolve({ downloadWasSuccessful: true });
});
it("shows the application window", () => {
expect(applicationWindow.isVisible).toBe(true);
});
it("given closing application window again and checking for updates again using tray, when check resolves with same version that was earlier downloaded, shows the application window", async () => {
closeAllWindows();
applicationBuilder.tray.click("check-for-updates");
await checkForPlatformUpdatesMock.resolve({
updateWasDiscovered: true,
version: "some-version",
});
expect(applicationWindow.isVisible).toBe(true);
});
});
it("when download of update resolves with failure, does not show the application window", async () => {
await downloadPlatformUpdateMock.resolve({ downloadWasSuccessful: false });
expect(applicationWindow.isVisible).toBe(false);
});
});
it("when process resolves without new update, does not show the application window", async () => {
await checkForPlatformUpdatesMock.resolve({
updateWasDiscovered: false,
});
expect(applicationWindow.isVisible).toBe(false);
});
});
describe("when user checks for updates using tray", () => {
beforeEach(() => {
applicationBuilder.tray.click("check-for-updates");
}); });
it("user cannot check for updates again", () => { it("user cannot check for updates again", () => {
@ -97,12 +157,6 @@ describe("installing update using tray", () => {
await checkForPlatformUpdatesMock.resolve({ await checkForPlatformUpdatesMock.resolve({
updateWasDiscovered: false, updateWasDiscovered: false,
}); });
await processCheckingForUpdatesPromise;
});
it("shows application window", () => {
expect(showApplicationWindowMock).toHaveBeenCalled();
}); });
it("user cannot install update", () => { it("user cannot install update", () => {
@ -132,12 +186,6 @@ describe("installing update using tray", () => {
updateWasDiscovered: true, updateWasDiscovered: true,
version: "some-version", version: "some-version",
}); });
await processCheckingForUpdatesPromise;
});
it("shows application window", () => {
expect(showApplicationWindowMock).toHaveBeenCalled();
}); });
it("user cannot check for updates again yet", () => { it("user cannot check for updates again yet", () => {

View File

@ -63,7 +63,7 @@ describe("installing update", () => {
describe("when started", () => { describe("when started", () => {
let rendered: RenderResult; let rendered: RenderResult;
let processCheckingForUpdates: (source: string) => Promise<void>; let processCheckingForUpdates: (source: string) => Promise<{ updateWasDiscoveredAndDownloaded: boolean }>;
beforeEach(async () => { beforeEach(async () => {
rendered = await applicationBuilder.render(); rendered = await applicationBuilder.render();
@ -84,10 +84,8 @@ describe("installing update", () => {
}); });
describe("when user checks for updates", () => { describe("when user checks for updates", () => {
let processCheckingForUpdatesPromise: Promise<void>; beforeEach(() => {
processCheckingForUpdates("irrelevant");
beforeEach(async () => {
processCheckingForUpdatesPromise = processCheckingForUpdates("irrelevant");
}); });
it("checks for updates", () => { it("checks for updates", () => {
@ -112,8 +110,6 @@ describe("installing update", () => {
await checkForPlatformUpdatesMock.resolve({ await checkForPlatformUpdatesMock.resolve({
updateWasDiscovered: false, updateWasDiscovered: false,
}); });
await processCheckingForUpdatesPromise;
}); });
it("shows tray icon for normal", () => { it("shows tray icon for normal", () => {
@ -137,8 +133,6 @@ describe("installing update", () => {
updateWasDiscovered: true, updateWasDiscovered: true,
version: "some-version", version: "some-version",
}); });
await processCheckingForUpdatesPromise;
}); });
it("starts downloading the update", () => { it("starts downloading the update", () => {
@ -243,7 +237,6 @@ describe("installing update", () => {
"/some-static-files-directory/icons/trayIconCheckingForUpdatesTemplate.png", "/some-static-files-directory/icons/trayIconCheckingForUpdatesTemplate.png",
); );
}); });
}); });
}); });
}); });

View File

@ -7,8 +7,6 @@ import { getApplicationBuilder } from "../../renderer/components/test-utils/get-
import type { RenderResult } from "@testing-library/react"; import type { RenderResult } from "@testing-library/react";
import electronUpdaterIsActiveInjectable from "../../main/electron-app/features/electron-updater-is-active.injectable"; import electronUpdaterIsActiveInjectable from "../../main/electron-app/features/electron-updater-is-active.injectable";
import publishIsConfiguredInjectable from "../../main/application-update/publish-is-configured.injectable"; import publishIsConfiguredInjectable from "../../main/application-update/publish-is-configured.injectable";
import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
import processCheckingForUpdatesInjectable from "../../main/application-update/check-for-updates/process-checking-for-updates.injectable"; import processCheckingForUpdatesInjectable from "../../main/application-update/check-for-updates/process-checking-for-updates.injectable";
import periodicalCheckForUpdatesInjectable from "../../main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable"; import periodicalCheckForUpdatesInjectable from "../../main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time"; import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
@ -17,7 +15,7 @@ const ENOUGH_TIME = 1000 * 60 * 60 * 2;
describe("periodical checking of updates", () => { describe("periodical checking of updates", () => {
let applicationBuilder: ApplicationBuilder; let applicationBuilder: ApplicationBuilder;
let processCheckingForUpdatesMock: AsyncFnMock<() => Promise<void>>; let processCheckingForUpdatesMock: jest.Mock;
beforeEach(() => { beforeEach(() => {
useFakeTime("2015-10-21T07:28:00Z"); useFakeTime("2015-10-21T07:28:00Z");
@ -28,7 +26,7 @@ describe("periodical checking of updates", () => {
mainDi.unoverride(periodicalCheckForUpdatesInjectable); mainDi.unoverride(periodicalCheckForUpdatesInjectable);
mainDi.permitSideEffects(periodicalCheckForUpdatesInjectable); mainDi.permitSideEffects(periodicalCheckForUpdatesInjectable);
processCheckingForUpdatesMock = asyncFn(); processCheckingForUpdatesMock = jest.fn();
mainDi.override( mainDi.override(
processCheckingForUpdatesInjectable, processCheckingForUpdatesInjectable,

View File

@ -67,7 +67,7 @@ describe("selection of update stability", () => {
describe("when started", () => { describe("when started", () => {
let rendered: RenderResult; let rendered: RenderResult;
let processCheckingForUpdates: (source: string) => Promise<void>; let processCheckingForUpdates: (source: string) => Promise<{ updateWasDiscoveredAndDownloaded: boolean }>;
beforeEach(async () => { beforeEach(async () => {
rendered = await applicationBuilder.render(); rendered = await applicationBuilder.render();

View File

@ -59,9 +59,11 @@ const checkForUpdatesTrayItemInjectable = getInjectable({
click: pipeline( click: pipeline(
async () => { async () => {
await processCheckingForUpdates("tray"); const { updateWasDiscoveredAndDownloaded } = await processCheckingForUpdates("tray");
await showApplicationWindow(); if (updateWasDiscoveredAndDownloaded) {
await showApplicationWindow();
}
}, },
withErrorLoggingFor(() => "[TRAY]: Checking for updates failed."), withErrorLoggingFor(() => "[TRAY]: Checking for updates failed."),

View File

@ -9,7 +9,6 @@ import discoveredUpdateVersionInjectable from "../../../common/application-updat
import { runInAction } from "mobx"; import { runInAction } from "mobx";
import downloadUpdateInjectable from "../download-update/download-update.injectable"; import downloadUpdateInjectable from "../download-update/download-update.injectable";
import checkForUpdatesStartingFromChannelInjectable from "./check-for-updates-starting-from-channel.injectable"; import checkForUpdatesStartingFromChannelInjectable from "./check-for-updates-starting-from-channel.injectable";
import withOrphanPromiseInjectable from "../../../common/utils/with-orphan-promise/with-orphan-promise.injectable";
import emitEventInjectable from "../../../common/app-event-bus/emit-event.injectable"; import emitEventInjectable from "../../../common/app-event-bus/emit-event.injectable";
import { getCurrentDateTime } from "../../../common/utils/date/get-current-date-time"; import { getCurrentDateTime } from "../../../common/utils/date/get-current-date-time";
@ -22,7 +21,6 @@ const processCheckingForUpdatesInjectable = getInjectable({
const checkingForUpdatesState = di.inject(updatesAreBeingDiscoveredInjectable); const checkingForUpdatesState = di.inject(updatesAreBeingDiscoveredInjectable);
const discoveredVersionState = di.inject(discoveredUpdateVersionInjectable); const discoveredVersionState = di.inject(discoveredUpdateVersionInjectable);
const checkForUpdatesStartingFromChannel = di.inject(checkForUpdatesStartingFromChannelInjectable); const checkForUpdatesStartingFromChannel = di.inject(checkForUpdatesStartingFromChannelInjectable);
const withOrphanPromise = di.inject(withOrphanPromiseInjectable);
const emitEvent = di.inject(emitEventInjectable); const emitEvent = di.inject(emitEventInjectable);
return async (source: string) => { return async (source: string) => {
@ -44,7 +42,7 @@ const processCheckingForUpdatesInjectable = getInjectable({
checkingForUpdatesState.set(false); checkingForUpdatesState.set(false);
}); });
return; return { updateWasDiscoveredAndDownloaded: false };
} }
const { version, actualUpdateChannel } = result; const { version, actualUpdateChannel } = result;
@ -56,7 +54,7 @@ const processCheckingForUpdatesInjectable = getInjectable({
checkingForUpdatesState.set(false); checkingForUpdatesState.set(false);
}); });
return; return { updateWasDiscoveredAndDownloaded: true };
} }
emitEvent({ emitEvent({
@ -74,7 +72,9 @@ const processCheckingForUpdatesInjectable = getInjectable({
checkingForUpdatesState.set(false); checkingForUpdatesState.set(false);
}); });
withOrphanPromise(async () => await downloadUpdate())(); const { downloadWasSuccessful } = await downloadUpdate();
return { updateWasDiscoveredAndDownloaded: downloadWasSuccessful };
}; };
}, },
}); });