forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-dialog-spec.ts
212 lines (178 loc) · 6.9 KB
/
api-dialog-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { expect } from 'chai';
import { dialog, BrowserWindow } from 'electron/main';
import { closeAllWindows } from './lib/window-helpers';
import { ifit } from './lib/spec-helpers';
import { setTimeout } from 'node:timers/promises';
describe('dialog module', () => {
describe('showOpenDialog', () => {
afterEach(closeAllWindows);
ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
expect(() => {
dialog.showOpenDialog({ title: 'i am title' });
}).to.not.throw();
expect(() => {
const w = new BrowserWindow();
dialog.showOpenDialog(w, { title: 'i am title' });
}).to.not.throw();
});
it('throws errors when the options are invalid', () => {
expect(() => {
dialog.showOpenDialog({ properties: false as any });
}).to.throw(/Properties must be an array/);
expect(() => {
dialog.showOpenDialog({ title: 300 as any });
}).to.throw(/Title must be a string/);
expect(() => {
dialog.showOpenDialog({ buttonLabel: [] as any });
}).to.throw(/Button label must be a string/);
expect(() => {
dialog.showOpenDialog({ defaultPath: {} as any });
}).to.throw(/Default path must be a string/);
expect(() => {
dialog.showOpenDialog({ message: {} as any });
}).to.throw(/Message must be a string/);
});
});
describe('showSaveDialog', () => {
afterEach(closeAllWindows);
ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
expect(() => {
dialog.showSaveDialog({ title: 'i am title' });
}).to.not.throw();
expect(() => {
const w = new BrowserWindow();
dialog.showSaveDialog(w, { title: 'i am title' });
}).to.not.throw();
});
it('throws errors when the options are invalid', () => {
expect(() => {
dialog.showSaveDialog({ title: 300 as any });
}).to.throw(/Title must be a string/);
expect(() => {
dialog.showSaveDialog({ buttonLabel: [] as any });
}).to.throw(/Button label must be a string/);
expect(() => {
dialog.showSaveDialog({ defaultPath: {} as any });
}).to.throw(/Default path must be a string/);
expect(() => {
dialog.showSaveDialog({ message: {} as any });
}).to.throw(/Message must be a string/);
expect(() => {
dialog.showSaveDialog({ nameFieldLabel: {} as any });
}).to.throw(/Name field label must be a string/);
});
});
describe('showMessageBox', () => {
afterEach(closeAllWindows);
// parentless message boxes are synchronous on macOS
// dangling message boxes on windows cause a DCHECK: https://source.chromium.org/chromium/chromium/src/+/main:base/win/message_window.cc;drc=7faa4bf236a866d007dc5672c9ce42660e67a6a6;l=68
ifit(process.platform !== 'darwin' && process.platform !== 'win32')('should not throw for a parentless message box', () => {
expect(() => {
dialog.showMessageBox({ message: 'i am message' });
}).to.not.throw();
});
ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
expect(() => {
const w = new BrowserWindow();
dialog.showMessageBox(w, { message: 'i am message' });
}).to.not.throw();
});
it('throws errors when the options are invalid', () => {
expect(() => {
dialog.showMessageBox(undefined as any, { type: 'not-a-valid-type' as any, message: '' });
}).to.throw(/Invalid message box type/);
expect(() => {
dialog.showMessageBox(null as any, { buttons: false as any, message: '' });
}).to.throw(/Buttons must be an array/);
expect(() => {
dialog.showMessageBox({ title: 300 as any, message: '' });
}).to.throw(/Title must be a string/);
expect(() => {
dialog.showMessageBox({ message: [] as any });
}).to.throw(/Message must be a string/);
expect(() => {
dialog.showMessageBox({ detail: 3.14 as any, message: '' });
}).to.throw(/Detail must be a string/);
expect(() => {
dialog.showMessageBox({ checkboxLabel: false as any, message: '' });
}).to.throw(/checkboxLabel must be a string/);
});
});
describe('showMessageBox with signal', () => {
afterEach(closeAllWindows);
it('closes message box immediately', async () => {
const controller = new AbortController();
const signal = controller.signal;
const w = new BrowserWindow();
const p = dialog.showMessageBox(w, { signal, message: 'i am message' });
controller.abort();
const result = await p;
expect(result.response).to.equal(0);
});
it('closes message box after a while', async () => {
const controller = new AbortController();
const signal = controller.signal;
const w = new BrowserWindow();
const p = dialog.showMessageBox(w, { signal, message: 'i am message' });
await setTimeout(500);
controller.abort();
const result = await p;
expect(result.response).to.equal(0);
});
it('cancels message box', async () => {
const controller = new AbortController();
const signal = controller.signal;
const w = new BrowserWindow();
const p = dialog.showMessageBox(w, {
signal,
message: 'i am message',
buttons: ['OK', 'Cancel'],
cancelId: 1
});
controller.abort();
const result = await p;
expect(result.response).to.equal(1);
});
it('cancels message box after a while', async () => {
const controller = new AbortController();
const signal = controller.signal;
const w = new BrowserWindow();
const p = dialog.showMessageBox(w, {
signal,
message: 'i am message',
buttons: ['OK', 'Cancel'],
cancelId: 1
});
await setTimeout(500);
controller.abort();
const result = await p;
expect(result.response).to.equal(1);
});
});
describe('showErrorBox', () => {
it('throws errors when the options are invalid', () => {
expect(() => {
(dialog.showErrorBox as any)();
}).to.throw(/Insufficient number of arguments/);
expect(() => {
dialog.showErrorBox(3 as any, 'four');
}).to.throw(/Error processing argument at index 0/);
expect(() => {
dialog.showErrorBox('three', 4 as any);
}).to.throw(/Error processing argument at index 1/);
});
});
describe('showCertificateTrustDialog', () => {
it('throws errors when the options are invalid', () => {
expect(() => {
(dialog.showCertificateTrustDialog as any)();
}).to.throw(/options must be an object/);
expect(() => {
dialog.showCertificateTrustDialog({} as any);
}).to.throw(/certificate must be an object/);
expect(() => {
dialog.showCertificateTrustDialog({ certificate: {} as any, message: false as any });
}).to.throw(/message must be a string/);
});
});
});