mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { ElectronApplication } from '@playwright/test';
|
|
import { BrowserWindow, MessageBoxOptions } from 'electron';
|
|
|
|
const setMessageBoxResponse = (electronApp: ElectronApplication, responseMatch: RegExp) => {
|
|
return electronApp.evaluate(async ({ dialog }, responseMatch) => {
|
|
type DialogArgsType = [ BrowserWindow, MessageBoxOptions ]|[MessageBoxOptions];
|
|
|
|
const getMatchingButton = (dialogArgs: DialogArgsType) => {
|
|
const matchingButton = (options: MessageBoxOptions) => {
|
|
const buttons = options.buttons ?? ['OK'];
|
|
|
|
for (let i = 0; i < buttons.length; i++) {
|
|
if (buttons[i].match(responseMatch)) {
|
|
return i;
|
|
}
|
|
}
|
|
|
|
throw new Error('No matching button found');
|
|
};
|
|
|
|
if (dialogArgs.length === 1) {
|
|
return matchingButton(dialogArgs[0]);
|
|
} else {
|
|
return matchingButton(dialogArgs[1]);
|
|
}
|
|
};
|
|
dialog.showMessageBoxSync = (...args: DialogArgsType) => getMatchingButton(args);
|
|
dialog.showMessageBox = async (...args: DialogArgsType) => ({
|
|
response: getMatchingButton(args),
|
|
checkboxChecked: false,
|
|
// We're mocking, so include "as any" to prevent this from breaking when we upgrade
|
|
// Electron.
|
|
} as any);
|
|
}, responseMatch);
|
|
};
|
|
|
|
export default setMessageBoxResponse;
|