Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import type { BrowserWindow } from "electron";
5import { beforeEach, vi } from "vitest";
6
7import * as ElectronDialog from "./ElectronDialog.ts";
8
9const { showMessageBoxMock, showOpenDialogMock, showErrorBoxMock } = vi.hoisted(() => ({
10 showMessageBoxMock: vi.fn(),
11 showOpenDialogMock: vi.fn(),
12 showErrorBoxMock: vi.fn(),
13}));
14
15vi.mock("electron", () => ({
16 dialog: {
17 showMessageBox: showMessageBoxMock,
18 showOpenDialog: showOpenDialogMock,
19 showErrorBox: showErrorBoxMock,
20 },
21}));
22
23describe("ElectronDialog", () => {
24 beforeEach(() => {
25 showMessageBoxMock.mockReset();
26 showOpenDialogMock.mockReset();
27 showErrorBoxMock.mockReset();
28 });
29
30 it.effect("returns false without opening a confirm dialog for empty messages", () =>
31 Effect.gen(function* () {
32 const dialog = yield* ElectronDialog.ElectronDialog;
33
34 const result = yield* dialog.confirm({
35 message: " ",
36 owner: Option.none(),
37 });
38
39 assert.isFalse(result);
40 assert.equal(showMessageBoxMock.mock.calls.length, 0);
41 }).pipe(Effect.provide(ElectronDialog.layer)),
42 );
43
44 it.effect("opens a confirm dialog for the owner window", () =>
45 Effect.gen(function* () {
46 const owner = { id: 1 } as BrowserWindow;
47 showMessageBoxMock.mockResolvedValue({ response: 1 });
48 const dialog = yield* ElectronDialog.ElectronDialog;
49
50 const result = yield* dialog.confirm({
51 message: "Delete worktree?",
52 owner: Option.some(owner),
53 });
54
55 assert.isTrue(result);
56 assert.deepEqual(showMessageBoxMock.mock.calls[0], [
57 owner,
58 {
59 type: "question",
60 buttons: ["No", "Yes"],
61 defaultId: 0,
62 cancelId: 0,
63 noLink: true,
64 message: "Delete worktree?",
65 },
66 ]);
67 }).pipe(Effect.provide(ElectronDialog.layer)),
68 );
69
70 it.effect("opens an app-level confirm dialog when there is no owner window", () =>
71 Effect.gen(function* () {
72 showMessageBoxMock.mockResolvedValue({ response: 0 });
73 const dialog = yield* ElectronDialog.ElectronDialog;
74
75 const result = yield* dialog.confirm({
76 message: "Delete worktree?",
77 owner: Option.none(),
78 });
79
80 assert.isFalse(result);
81 assert.deepEqual(showMessageBoxMock.mock.calls[0], [
82 {
83 type: "question",
84 buttons: ["No", "Yes"],
85 defaultId: 0,
86 cancelId: 0,
87 noLink: true,
88 message: "Delete worktree?",
89 },
90 ]);
91 }).pipe(Effect.provide(ElectronDialog.layer)),
92 );
93});