From 9ea452d399d7a7ffc1ddedfa46207467d4cd4e9a Mon Sep 17 00:00:00 2001 From: Iku-turso Date: Wed, 1 Jun 2022 15:02:58 +0300 Subject: [PATCH] Add behaviours for quitting the application using application menu Co-authored-by: Janne Savolainen Signed-off-by: Iku-turso --- ...ing-the-app-using-application-menu.test.ts | 90 +++++++++++++++++++ src/main/getDiForUnitTesting.ts | 2 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/behaviours/quitting-and-restarting-the-app/quitting-the-app-using-application-menu.test.ts diff --git a/src/behaviours/quitting-and-restarting-the-app/quitting-the-app-using-application-menu.test.ts b/src/behaviours/quitting-and-restarting-the-app/quitting-the-app-using-application-menu.test.ts new file mode 100644 index 0000000000..8341e40349 --- /dev/null +++ b/src/behaviours/quitting-and-restarting-the-app/quitting-the-app-using-application-menu.test.ts @@ -0,0 +1,90 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import type { ApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder"; +import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder"; +import type { ClusterManager } from "../../main/cluster-manager"; +import { lensWindowInjectionToken } from "../../main/start-main-application/lens-window/application-window/lens-window-injection-token"; +import exitAppInjectable from "../../main/electron-app/features/exit-app.injectable"; +import clusterManagerInjectable from "../../main/cluster-manager.injectable"; +import stopServicesAndExitAppInjectable from "../../main/stop-services-and-exit-app.injectable"; + +describe("quitting the app using application menu", () => { + describe("given application has started", () => { + let applicationBuilder: ApplicationBuilder; + let clusterManagerStub: ClusterManager; + let exitAppMock: jest.Mock; + + beforeEach(async () => { + jest.useFakeTimers(); + + applicationBuilder = getApplicationBuilder().beforeApplicationStart( + ({ mainDi }) => { + mainDi.unoverride(stopServicesAndExitAppInjectable); + + clusterManagerStub = { stop: jest.fn() } as unknown as ClusterManager; + mainDi.override(clusterManagerInjectable, () => clusterManagerStub); + + exitAppMock = jest.fn(); + mainDi.override(exitAppInjectable, () => exitAppMock); + }, + ); + + await applicationBuilder.render(); + }); + + it("only an application window is open", () => { + const windows = applicationBuilder.dis.mainDi.injectMany( + lensWindowInjectionToken, + ); + + expect( + windows.map((window) => ({ id: window.id, visible: window.visible })), + ).toEqual([ + { id: "only-application-window", visible: true }, + { id: "splash", visible: false }, + ]); + }); + + describe("when application is quit", () => { + beforeEach(async () => { + await applicationBuilder.applicationMenu.click("root.quit"); + }); + + it("closes all windows", () => { + const windows = applicationBuilder.dis.mainDi.injectMany( + lensWindowInjectionToken, + ); + + expect( + windows.map((window) => ({ id: window.id, visible: window.visible })), + ).toEqual([ + { id: "only-application-window", visible: false }, + { id: "splash", visible: false }, + ]); + }); + + it("disconnects all clusters", () => { + expect(clusterManagerStub.stop).toHaveBeenCalled(); + }); + + it("after insufficient time passes, does not terminate application yet", () => { + jest.advanceTimersByTime(999); + + expect(exitAppMock).not.toHaveBeenCalled(); + }); + + describe("after sufficient time passes", () => { + beforeEach(() => { + jest.advanceTimersByTime(1000); + }); + + it("terminates application", () => { + expect(exitAppMock).toHaveBeenCalled(); + }); + }); + }); + }); +}); diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index 43f61c22cc..5f2e968f14 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -242,7 +242,7 @@ const overrideElectronFeatures = (di: DiContainer) => { di.override(getCommandLineSwitchInjectable, () => () => "irrelevant"); di.override(requestSingleInstanceLockInjectable, () => () => true); di.override(disableHardwareAccelerationInjectable, () => () => {}); - di.override(shouldStartHiddenInjectable, () => true); + di.override(shouldStartHiddenInjectable, () => false); di.override(showMessagePopupInjectable, () => () => {}); di.override(waitForElectronToBeReadyInjectable, () => () => Promise.resolve()); di.override(ipcMainInjectable, () => ({}));