Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ElectronDialog.test.ts

145 lines5.4 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Cause from "effect/Cause";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5import type { BrowserWindow } from "electron";
6import { beforeEach, vi } from "vite-plus/test";
7​
8import * as ElectronDialog from "./ElectronDialog.ts";
9​
10const { showMessageBoxMock, showOpenDialogMock, showErrorBoxMock } = vi.hoisted(() => ({
11 showMessageBoxMock: vi.fn(),
12 showOpenDialogMock: vi.fn(),
13 showErrorBoxMock: vi.fn(),
14}));
15​
16vi.mock("electron", () => ({
17 dialog: {
18 showMessageBox: showMessageBoxMock,
19 showOpenDialog: showOpenDialogMock,
20 showErrorBox: showErrorBoxMock,
21 },
22}));
23​
24describe("ElectronDialog", () => {
25 beforeEach(() => {
26 showMessageBoxMock.mockReset();
27 showOpenDialogMock.mockReset();
28 showErrorBoxMock.mockReset();
29 });
30​
31 it.effect("preserves folder picker request context and cause", () =>
32 Effect.gen(function* () {
33 const cause = new Error("folder picker failed");
34 const owner = { id: 7 } as BrowserWindow;
35 showOpenDialogMock.mockRejectedValue(cause);
36 const dialog = yield* ElectronDialog.ElectronDialog;
37​
38 const error = yield* Effect.flip(
39 dialog.pickFolder({
40 owner: Option.some(owner),
41 defaultPath: Option.some("/workspace"),
42 }),
43 );
44​
45 assert.instanceOf(error, ElectronDialog.ElectronDialogPickFolderError);
46 assert.isTrue(ElectronDialog.isElectronDialogError(error));
47 assert.strictEqual(error.ownerWindowId, 7);
48 assert.strictEqual(error.defaultPath, "/workspace");
49 assert.strictEqual(error.cause, cause);
50 assert.include(error.message, "window 7");
51 assert.include(error.message, "/workspace");
52 assert.notInclude(error.message, cause.message);
53 }).pipe(Effect.provide(ElectronDialog.layer)),
54 );
55​
56 it.effect("opens a single-file picker when multiple selections are disabled", () =>
57 Effect.gen(function* () {
58 showOpenDialogMock.mockResolvedValue({
59 canceled: false,
60 filePaths: ["/pictures/icon.png"],
61 });
62 const dialog = yield* ElectronDialog.ElectronDialog;
63​
64 const paths = yield* dialog.pickFiles({
65 owner: Option.none(),
66 defaultPath: Option.some("/project"),
67 filters: [{ name: "Images", extensions: ["png"] }],
68 multiple: false,
69 });
70​
71 assert.deepEqual(paths, ["/pictures/icon.png"]);
72 assert.deepEqual(showOpenDialogMock.mock.calls, [
73 [
74 {
75 defaultPath: "/project",
76 filters: [{ name: "Images", extensions: ["png"] }],
77 properties: ["openFile"],
78 },
79 ],
80 ]);
81 }).pipe(Effect.provide(ElectronDialog.layer)),
82 );
83​
84 it.effect("preserves message box request context and cause", () =>
85 Effect.gen(function* () {
86 const cause = new Error("message box failed");
87 showMessageBoxMock.mockRejectedValue(cause);
88 const dialog = yield* ElectronDialog.ElectronDialog;
89​
90 const error = yield* Effect.flip(
91 dialog.showMessageBox({
92 type: "warning",
93 title: "Unsaved changes",
94 message: "Discard changes?",
95 detail: "This cannot be undone.",
96 buttons: ["Cancel", "Discard"],
97 }),
98 );
99​
100 assert.instanceOf(error, ElectronDialog.ElectronDialogShowMessageBoxError);
101 assert.strictEqual(error.type, "warning");
102 assert.strictEqual(error.titleLength, "Unsaved changes".length);
103 assert.strictEqual(error.messageLength, "Discard changes?".length);
104 assert.strictEqual(error.detailLength, "This cannot be undone.".length);
105 assert.strictEqual(error.buttonCount, 2);
106 assert.notProperty(error, "title");
107 assert.notProperty(error, "dialogMessage");
108 assert.notProperty(error, "dialogDetail");
109 assert.notProperty(error, "buttons");
110 assert.strictEqual(error.cause, cause);
111 assert.include(error.message, "warning");
112 assert.notInclude(error.message, "Unsaved changes");
113 assert.notInclude(error.message, "Discard changes?");
114 assert.notInclude(error.message, "This cannot be undone.");
115 assert.notInclude(error.message, "Cancel");
116 assert.notInclude(error.message, "Discard");
117 assert.notInclude(error.message, cause.message);
118 }).pipe(Effect.provide(ElectronDialog.layer)),
119 );
120​
121 it.effect("preserves error box request context and cause in the defect", () =>
122 Effect.gen(function* () {
123 const cause = new Error("error box failed");
124 showErrorBoxMock.mockImplementation(() => {
125 throw cause;
126 });
127 const dialog = yield* ElectronDialog.ElectronDialog;
128​
129 const exit = yield* Effect.exit(dialog.showErrorBox("Startup failed", "Could not start."));
130​
131 assert.isTrue(exit._tag === "Failure");
132 if (exit._tag === "Success") return;
133 const error = Cause.squash(exit.cause);
134 assert.instanceOf(error, ElectronDialog.ElectronDialogShowErrorBoxError);
135 assert.strictEqual(error.titleLength, "Startup failed".length);
136 assert.strictEqual(error.contentLength, "Could not start.".length);
137 assert.notProperty(error, "title");
138 assert.notProperty(error, "content");
139 assert.strictEqual(error.cause, cause);
140 assert.notInclude(error.message, "Startup failed");
141 assert.notInclude(error.message, "Could not start.");
142 assert.notInclude(error.message, cause.message);
143 }).pipe(Effect.provide(ElectronDialog.layer)),
144 );
145});