2025-04-24 00:50:35 -07:00
|
|
|
import { MessageBoxType } from '@joplin/lib/shim';
|
2025-04-23 01:02:31 -07:00
|
|
|
import { DialogControl } from '../components/DialogManager';
|
|
|
|
import { PromptButtonSpec } from '../components/DialogManager/types';
|
|
|
|
import makeShowMessageBox from './makeShowMessageBox';
|
|
|
|
|
2025-04-24 00:50:35 -07:00
|
|
|
type OnPrompt = (buttons: PromptButtonSpec[], onDismiss: ()=> void)=> void;
|
2025-04-23 01:02:31 -07:00
|
|
|
const makeMockDialogControl = (onPrompt: OnPrompt): DialogControl => {
|
|
|
|
return {
|
|
|
|
info: jest.fn(),
|
|
|
|
error: jest.fn(),
|
2025-04-24 00:50:35 -07:00
|
|
|
prompt: jest.fn((_title, _message, buttons, options) => {
|
|
|
|
onPrompt(buttons, options.onDismiss);
|
2025-04-23 01:02:31 -07:00
|
|
|
}),
|
|
|
|
showMenu: jest.fn(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('makeShowMessageBox', () => {
|
|
|
|
test('should resolve with the index of the selected button', async () => {
|
|
|
|
const dialogControl = makeMockDialogControl(buttons => {
|
|
|
|
buttons.find(button => button.text === 'OK').onPress();
|
|
|
|
});
|
|
|
|
const showMessageBox = makeShowMessageBox({ current: dialogControl });
|
|
|
|
|
|
|
|
const okButtonIndex = 0;
|
|
|
|
expect(await showMessageBox('test')).toBe(okButtonIndex);
|
|
|
|
});
|
2025-04-24 00:50:35 -07:00
|
|
|
|
|
|
|
test('should resolve to the index of the cancel button when cancelled', async () => {
|
|
|
|
const dialogControl = makeMockDialogControl((_buttons, onDismiss) => {
|
|
|
|
// Cancel
|
|
|
|
onDismiss();
|
|
|
|
});
|
|
|
|
|
|
|
|
const showMessageBox = makeShowMessageBox({ current: dialogControl });
|
|
|
|
expect(await showMessageBox('test')).toBe(1); // Cancel button index
|
|
|
|
// Should resolve to -1 when there is no cancel button
|
|
|
|
expect(await showMessageBox('test', { type: MessageBoxType.Error })).toBe(-1);
|
|
|
|
});
|
2025-04-23 01:02:31 -07:00
|
|
|
});
|