electron/spec/api-dialog-spec.ts

244 lines
7.8 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { expect } from 'chai';
import { dialog, BaseWindow, BrowserWindow } from 'electron/main';
import { closeAllWindows } from './lib/window-helpers';
import { ifit } from './lib/spec-helpers';
import { setTimeout } from 'node:timers/promises';
2016-12-01 22:15:05 +00:00
describe('dialog module', () => {
describe('showOpenDialog', () => {
2020-03-20 20:28:31 +00:00
afterEach(closeAllWindows);
ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showOpenDialog({ title: 'i am title' });
}).to.not.throw();
expect(() => {
2020-03-20 20:28:31 +00:00
const w = new BrowserWindow();
dialog.showOpenDialog(w, { title: 'i am title' });
}).to.not.throw();
expect(() => {
const w = new BaseWindow();
dialog.showOpenDialog(w, { title: 'i am title' });
}).to.not.throw();
2020-03-20 20:28:31 +00:00
});
2016-12-01 22:15:05 +00:00
it('throws errors when the options are invalid', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showOpenDialog({ properties: false as any });
}).to.throw(/Properties must be an array/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showOpenDialog({ title: 300 as any });
}).to.throw(/Title must be a string/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showOpenDialog({ buttonLabel: [] as any });
}).to.throw(/Button label must be a string/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showOpenDialog({ defaultPath: {} as any });
}).to.throw(/Default path must be a string/);
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showOpenDialog({ message: {} as any });
}).to.throw(/Message must be a string/);
});
});
2016-12-01 22:15:05 +00:00
describe('showSaveDialog', () => {
2020-03-20 20:28:31 +00:00
afterEach(closeAllWindows);
ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showSaveDialog({ title: 'i am title' });
}).to.not.throw();
expect(() => {
2020-03-20 20:28:31 +00:00
const w = new BrowserWindow();
dialog.showSaveDialog(w, { title: 'i am title' });
}).to.not.throw();
expect(() => {
const w = new BaseWindow();
dialog.showSaveDialog(w, { title: 'i am title' });
}).to.not.throw();
2020-03-20 20:28:31 +00:00
});
2016-12-01 22:15:05 +00:00
it('throws errors when the options are invalid', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showSaveDialog({ title: 300 as any });
}).to.throw(/Title must be a string/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showSaveDialog({ buttonLabel: [] as any });
}).to.throw(/Button label must be a string/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showSaveDialog({ defaultPath: {} as any });
}).to.throw(/Default path must be a string/);
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showSaveDialog({ message: {} as any });
}).to.throw(/Message must be a string/);
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showSaveDialog({ nameFieldLabel: {} as any });
}).to.throw(/Name field label must be a string/);
});
});
2016-12-01 22:15:05 +00:00
describe('showMessageBox', () => {
2020-03-20 20:28:31 +00:00
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(() => {
2020-03-20 20:28:31 +00:00
dialog.showMessageBox({ message: 'i am message' });
}).to.not.throw();
});
ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
const w = new BrowserWindow();
dialog.showMessageBox(w, { message: 'i am message' });
}).to.not.throw();
expect(() => {
const w = new BaseWindow();
dialog.showMessageBox(w, { message: 'i am message' });
}).to.not.throw();
2020-03-20 20:28:31 +00:00
});
2016-12-01 22:15:05 +00:00
it('throws errors when the options are invalid', () => {
expect(() => {
dialog.showMessageBox(undefined as any, { type: 'not-a-valid-type' as any, message: '' });
2020-03-20 20:28:31 +00:00
}).to.throw(/Invalid message box type/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showMessageBox(null as any, { buttons: false as any, message: '' });
}).to.throw(/Buttons must be an array/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showMessageBox({ title: 300 as any, message: '' });
}).to.throw(/Title must be a string/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showMessageBox({ message: [] as any });
}).to.throw(/Message must be a string/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showMessageBox({ detail: 3.14 as any, message: '' });
}).to.throw(/Detail must be a string/);
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showMessageBox({ checkboxLabel: false as any, message: '' });
}).to.throw(/checkboxLabel must be a string/);
});
});
2016-12-01 22:15:05 +00:00
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('does not crash when there is a defaultId but no buttons', async () => {
const controller = new AbortController();
const signal = controller.signal;
const w = new BrowserWindow();
const p = dialog.showMessageBox(w, {
signal,
message: 'i am message',
type: 'info',
defaultId: 0,
title: 'i am title'
});
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);
});
});
2016-12-01 22:15:05 +00:00
describe('showErrorBox', () => {
it('throws errors when the options are invalid', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
(dialog.showErrorBox as any)();
}).to.throw(/Insufficient number of arguments/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showErrorBox(3 as any, 'four');
}).to.throw(/Error processing argument at index 0/);
2016-12-01 22:15:05 +00:00
expect(() => {
2020-03-20 20:28:31 +00:00
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(() => {
2020-03-20 20:28:31 +00:00
(dialog.showCertificateTrustDialog as any)();
}).to.throw(/options must be an object/);
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showCertificateTrustDialog({} as any);
}).to.throw(/certificate must be an object/);
expect(() => {
2020-03-20 20:28:31 +00:00
dialog.showCertificateTrustDialog({ certificate: {} as any, message: false as any });
}).to.throw(/message must be a string/);
});
});
});