Skip to content

Commit

Permalink
Fixed opening files via electron's dialog API (#2237)
Browse files Browse the repository at this point in the history
* Fixed opening files via electron's dialog API

* Added a changelog entry.
  • Loading branch information
tonyanziano committed Mar 4, 2021
1 parent 2687d30 commit a4685d6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v4.11.0 - 2021 - 3 - 03
## v4.12.0 - 2021 - 3 - 03
- [client/main] Added UI to open bot dialog that allows the user to set transcript testing properties `randomSeed` and `randomValue` in PR [2209](https://github.com/microsoft/BotFramework-Emulator/pull/2209)
- [client] Added a dialog to the "Conversation -> Send System Activity" menu that allows the user to send custom activity payloads to their bot in PR [2213](https://github.com/microsoft/BotFramework-Emulator/pull/2213)
- [main] Bumped `electron` from `4.1.1` to `11.0.1` in PR [2226](https://github.com/microsoft/BotFramework-Emulator/pull/2226)
- [main] Bumped `electron-builder` to `22.9.1` and `electron-updater` to `4.3.5` to fix the Mac build in PR [2230](https://github.com/microsoft/BotFramework-Emulator/pull/2230)
- [client] Re-enabled the `<webview>` tag in the client so that the inspectors show again in PR [2233](https://github.com/microsoft/BotFramework-Emulator/pull/2233)
- [client] Bumped `botframework-webchat` to v4.12.0 and fixed various Web Chat-related bugs in PR [2236](https://github.com/microsoft/BotFramework-Emulator/pull/2236)
- [main] Fixed a bug where we were expecting the incorrect shape from Electron's updated `dialog.showOpenDialog()` API in PR [2237](https://github.com/microsoft/BotFramework-Emulator/pull/2237)

## v4.11.0 - 2020 - 11 - 05
- [client] Moved from master to main as the default branch. [2194](https://github.com/microsoft/BotFramework-Emulator/pull/2194)
Expand Down
2 changes: 1 addition & 1 deletion packages/app/main/src/commands/electronCommands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jest.mock('electron', () => ({
dialog: {
showMessageBox: (mainBrowserWindow: any, p: { buttons: string[]; type: string; title: string; message: string }) =>
void 0,
showOpenDialog: () => void 0,
showOpenDialog: () => ({ filePaths: [] }),
showSaveDialogSync: () => void 0,
},
shell: {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/main/src/commands/electronCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class ElectronCommands {
// ---------------------------------------------------------------------------
// Shows an open dialog and returns a path
@Command(Commands.ShowOpenDialog)
protected showOpenDialog(dialogOptions: Electron.OpenDialogOptions = {}): false | string {
protected showOpenDialog(dialogOptions: Electron.OpenDialogOptions = {}): Promise<string> {
return showOpenDialog(emulatorApplication.mainWindow.browserWindow, dialogOptions);
}

Expand Down
56 changes: 56 additions & 0 deletions packages/app/main/src/utils/showOpenDialog.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Framework Emulator Github:
// https://github.com/Microsoft/BotFramwork-Emulator
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

import { showOpenDialog } from './showOpenDialog';

const mockShowOpenDialog = jest.fn().mockResolvedValue({ filePaths: [] });
jest.mock('electron', () => ({
dialog: {
showOpenDialog: async (...args) => mockShowOpenDialog(...args),
},
}));

describe('showOpenDialog', () => {
it('should return an empty string if nothing was selected', async () => {
const filePath = await showOpenDialog(undefined, undefined);

expect(filePath).toBe('');
});

it('should return the selected file path', async () => {
mockShowOpenDialog.mockResolvedValueOnce({ filePaths: ['someFile'] });
const filePath = await showOpenDialog(undefined, undefined);

expect(filePath).toBe('someFile');
});
});
6 changes: 3 additions & 3 deletions packages/app/main/src/utils/showOpenDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import { BrowserWindow, dialog, OpenDialogOptions } from 'electron';

/** Shows a native open file / directory dialog */
export const showOpenDialog = (window: BrowserWindow, options: OpenDialogOptions): false | string => {
const filePaths = dialog.showOpenDialog(window, options);
return filePaths && filePaths[0];
export const showOpenDialog = async (window: BrowserWindow, options: OpenDialogOptions): Promise<string> => {
const { filePaths = [] } = await dialog.showOpenDialog(window, options);
return filePaths[0] || '';
};

0 comments on commit a4685d6

Please sign in to comment.