|
| 1 | +import { assert, describe, it } from "@effect/vitest"; |
| 2 | +import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import type * as Electron from "electron"; |
| 6 | +import { beforeEach, vi } from "vite-plus/test"; |
| 7 | + |
| 8 | +interface MockTrayInstance { |
| 9 | + icon: unknown; |
| 10 | + toolTip?: string; |
| 11 | + contextMenu?: unknown; |
| 12 | + listeners: Map<string, (...args: unknown[]) => void>; |
| 13 | + destroyed: boolean; |
| 14 | + destroy: () => void; |
| 15 | + isDestroyed: () => boolean; |
| 16 | + setToolTip: (tip: string) => void; |
| 17 | + setContextMenu: (menu: unknown) => void; |
| 18 | + on: (event: string, listener: (...args: unknown[]) => void) => void; |
| 19 | +} |
| 20 | + |
| 21 | +const { buildFromTemplateMock, createFromPathMock, trayInstances } = vi.hoisted(() => { |
| 22 | + const instances: MockTrayInstance[] = []; |
| 23 | + |
| 24 | + return { |
| 25 | + buildFromTemplateMock: vi.fn((template) => ({ template })), |
| 26 | + createFromPathMock: vi.fn((path: string) => ({ |
| 27 | + isEmpty: () => false, |
| 28 | + path, |
| 29 | + })), |
| 30 | + trayInstances: instances, |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +vi.mock("electron", () => { |
| 35 | + class MockTray { |
| 36 | + toolTip?: string; |
| 37 | + contextMenu?: unknown; |
| 38 | + listeners = new Map<string, (...args: unknown[]) => void>(); |
| 39 | + destroyed = false; |
| 40 | + icon: unknown; |
| 41 | + |
| 42 | + constructor(icon: unknown) { |
| 43 | + this.icon = icon; |
| 44 | + trayInstances.push(this); |
| 45 | + } |
| 46 | + |
| 47 | + setToolTip(tip: string) { |
| 48 | + this.toolTip = tip; |
| 49 | + } |
| 50 | + |
| 51 | + setContextMenu(menu: unknown) { |
| 52 | + this.contextMenu = menu; |
| 53 | + } |
| 54 | + |
| 55 | + on(event: string, listener: (...args: unknown[]) => void) { |
| 56 | + this.listeners.set(event, listener); |
| 57 | + } |
| 58 | + |
| 59 | + destroy() { |
| 60 | + this.destroyed = true; |
| 61 | + } |
| 62 | + |
| 63 | + isDestroyed() { |
| 64 | + return this.destroyed; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + Menu: { |
| 70 | + buildFromTemplate: buildFromTemplateMock, |
| 71 | + }, |
| 72 | + nativeImage: { |
| 73 | + createFromPath: createFromPathMock, |
| 74 | + }, |
| 75 | + Tray: MockTray, |
| 76 | + }; |
| 77 | +}); |
| 78 | + |
| 79 | +import * as ElectronTray from "./ElectronTray.ts"; |
| 80 | + |
| 81 | +const TestLayer = ElectronTray.layer.pipe( |
| 82 | + Layer.provide(Layer.succeed(HostProcessPlatform, "win32")), |
| 83 | +); |
| 84 | + |
| 85 | +describe("ElectronTray", () => { |
| 86 | + beforeEach(() => { |
| 87 | + buildFromTemplateMock.mockClear(); |
| 88 | + createFromPathMock.mockClear(); |
| 89 | + trayInstances.length = 0; |
| 90 | + }); |
| 91 | + |
| 92 | + it.effect("creates tray with tooltip and context menu and click listeners", () => |
| 93 | + Effect.gen(function* () { |
| 94 | + const electronTray = yield* ElectronTray.ElectronTray; |
| 95 | + let clicked = false; |
| 96 | + let doubleClicked = false; |
| 97 | + |
| 98 | + yield* electronTray.create({ |
| 99 | + iconPath: "C:/path/to/icon.ico", |
| 100 | + tooltip: "T3 Code", |
| 101 | + onClick: () => { |
| 102 | + clicked = true; |
| 103 | + }, |
| 104 | + onDoubleClick: () => { |
| 105 | + doubleClicked = true; |
| 106 | + }, |
| 107 | + menuItems: [ |
| 108 | + { label: "Open T3 Code", click: () => {} }, |
| 109 | + { type: "separator" }, |
| 110 | + { label: "Quit", click: () => {} }, |
| 111 | + ], |
| 112 | + }); |
| 113 | + |
| 114 | + assert.equal(trayInstances.length, 1); |
| 115 | + const instance = trayInstances[0]; |
| 116 | + assert.isDefined(instance); |
| 117 | + assert.equal(instance?.toolTip, "T3 Code"); |
| 118 | + assert.isTrue(instance?.listeners.has("click")); |
| 119 | + assert.isTrue(instance?.listeners.has("double-click")); |
| 120 | + assert.equal(buildFromTemplateMock.mock.calls.length, 1); |
| 121 | + |
| 122 | + instance?.listeners.get("click")?.(); |
| 123 | + assert.isTrue(clicked); |
| 124 | + instance?.listeners.get("double-click")?.(); |
| 125 | + assert.isTrue(doubleClicked); |
| 126 | + |
| 127 | + yield* electronTray.destroy; |
| 128 | + assert.isTrue(instance?.destroyed); |
| 129 | + }).pipe(Effect.provide(TestLayer)), |
| 130 | + ); |
| 131 | + |
| 132 | + it.effect("destroys old tray before creating a new one", () => |
| 133 | + Effect.gen(function* () { |
| 134 | + const electronTray = yield* ElectronTray.ElectronTray; |
| 135 | + |
| 136 | + yield* electronTray.create({ iconPath: "icon1.ico" }); |
| 137 | + assert.equal(trayInstances.length, 1); |
| 138 | + assert.isFalse(trayInstances[0]?.destroyed); |
| 139 | + |
| 140 | + yield* electronTray.create({ iconPath: "icon2.ico" }); |
| 141 | + assert.equal(trayInstances.length, 2); |
| 142 | + assert.isTrue(trayInstances[0]?.destroyed); |
| 143 | + assert.isFalse(trayInstances[1]?.destroyed); |
| 144 | + }).pipe(Effect.provide(TestLayer)), |
| 145 | + ); |
| 146 | +}); |
0 commit comments