From a6138d7b345cc09d033a7e1977527870da66147f Mon Sep 17 00:00:00 2001 From: ToTheos-Dev Date: Fri, 22 May 2026 14:25:19 +1000 Subject: [PATCH] test: add src/native/autoLaunch.test.ts --- src/native/autoLaunch.test.ts | 147 ++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/native/autoLaunch.test.ts diff --git a/src/native/autoLaunch.test.ts b/src/native/autoLaunch.test.ts new file mode 100644 index 0000000..2e1e529 --- /dev/null +++ b/src/native/autoLaunch.test.ts @@ -0,0 +1,147 @@ +/// + +import AutoLaunch from "auto-launch"; +import { ipcMain } from "electron"; + +import { autoLaunch } from "./autoLaunch"; +import { mainWindow } from "./window"; + +// Mock auto-launch - define mock methods inside the factory +jest.mock("auto-launch", () => { + const mockAutoLaunch = { + isEnabled: jest.fn(), + enable: jest.fn(), + disable: jest.fn(), + }; + const AutoLaunchMock = jest.fn().mockImplementation(() => mockAutoLaunch); + (AutoLaunchMock as unknown as Record).mockMethods = + mockAutoLaunch; + return AutoLaunchMock; +}); + +// Mock electron - store handlers on global +jest.mock("electron", () => ({ + ipcMain: { + on: jest.fn((channel: string, handler: Function) => { + (global as Record).__autoLaunchIpcHandlers = + (global as Record).__autoLaunchIpcHandlers || {}; + ((global as Record).__autoLaunchIpcHandlers as Record< + string, + Function + >)[channel] = handler; + }), + }, +})); + +// Mock window +jest.mock("./window", () => ({ + mainWindow: { + webContents: { + send: jest.fn(), + }, + }, +})); + +// Access the mock methods from the mocked constructor +const mockMethods = (AutoLaunch as unknown as Record) + .mockMethods as Record; + +const getIpcHandlers = (): Record => + ((global as Record).__autoLaunchIpcHandlers as Record< + string, + Function + >) || {}; + +describe("autoLaunch", () => { + beforeEach(() => { + // Clear mock method calls but preserve AutoLaunch constructor call history + // since it's called at module load time before tests run + const mockMethods = (AutoLaunch as unknown as Record) + .mockMethods as Record; + Object.values(mockMethods).forEach((mock) => mock.mockClear()); + (mainWindow.webContents.send as jest.Mock).mockClear(); + }); + + describe("autoLaunch instance", () => { + it("should create an AutoLaunch instance with the correct config", () => { + expect(AutoLaunch).toHaveBeenCalledWith({ + name: "Revolt", + }); + }); + + it("should export the autoLaunch instance", () => { + expect(autoLaunch).toBeDefined(); + }); + }); + + describe("isAutostart? IPC handler", () => { + it("should register the isAutostart? IPC handler", () => { + expect(getIpcHandlers()["isAutostart?"]).toBeDefined(); + }); + + it("should check if autoLaunch is enabled when handler is called", () => { + mockMethods.isEnabled.mockResolvedValue(true); + getIpcHandlers()["isAutostart?"](); + + expect(mockMethods.isEnabled).toHaveBeenCalled(); + }); + + it("should send true to mainWindow when autoLaunch is enabled", async () => { + mockMethods.isEnabled.mockResolvedValue(true); + getIpcHandlers()["isAutostart?"](); + + await Promise.resolve(); + + expect(mainWindow.webContents.send).toHaveBeenCalledWith( + "isAutostart", + true, + ); + }); + + it("should send false to mainWindow when autoLaunch is disabled", async () => { + mockMethods.isEnabled.mockResolvedValue(false); + getIpcHandlers()["isAutostart?"](); + + await Promise.resolve(); + + expect(mainWindow.webContents.send).toHaveBeenCalledWith( + "isAutostart", + false, + ); + }); + }); + + describe("setAutostart IPC handler", () => { + it("should register the setAutostart IPC handler", () => { + expect(getIpcHandlers()["setAutostart"]).toBeDefined(); + }); + + it("should enable autoLaunch when state is true", () => { + getIpcHandlers()["setAutostart"](true); + + expect(mockMethods.enable).toHaveBeenCalled(); + expect(mockMethods.disable).not.toHaveBeenCalled(); + }); + + it("should disable autoLaunch when state is false", () => { + getIpcHandlers()["setAutostart"](false); + + expect(mockMethods.disable).toHaveBeenCalled(); + expect(mockMethods.enable).not.toHaveBeenCalled(); + }); + + it("should disable autoLaunch when state is falsy", () => { + getIpcHandlers()["setAutostart"](null); + + expect(mockMethods.disable).toHaveBeenCalled(); + expect(mockMethods.enable).not.toHaveBeenCalled(); + }); + + it("should disable autoLaunch when state is undefined", () => { + getIpcHandlers()["setAutostart"](undefined); + + expect(mockMethods.disable).toHaveBeenCalled(); + expect(mockMethods.enable).not.toHaveBeenCalled(); + }); + }); +});