From 47d8a54b202d989cdd1c8adf25bc844ae85a2ebb Mon Sep 17 00:00:00 2001 From: Shiran Pasternak Date: Wed, 3 Feb 2021 19:14:22 -0500 Subject: [PATCH] Fixes #2272: Task file download Electron's `dialog.showSaveDialog` has changed at some point to return a `Promise` rather than a string value. Adjusted to the new API. --- .../testing/electron-testing.module.ts | 6 ++++- .../file-viewer-header.component.spec.ts | 22 +++++++++++-------- .../file-viewer-header.component.ts | 11 ++++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/@batch-flask/electron/testing/electron-testing.module.ts b/src/@batch-flask/electron/testing/electron-testing.module.ts index 877da5839f..3ea765899d 100644 --- a/src/@batch-flask/electron/testing/electron-testing.module.ts +++ b/src/@batch-flask/electron/testing/electron-testing.module.ts @@ -7,7 +7,11 @@ export class MockElectronDialog { public showSaveDialog: jasmine.Spy; constructor() { - this.showSaveDialog = jasmine.createSpy("dialog.showSaveDialog").and.returnValue("/some/local/path/foo.ts"); + this.showSaveDialog = + jasmine.createSpy("dialog.showSaveDialog").and.callFake( + () => new Promise( + resolve => resolve({ filePath: "/some/local/path/foo.ts" }) + )); } } diff --git a/src/@batch-flask/ui/file/file-viewer/file-viewer-container/file-viewer-header/file-viewer-header.component.spec.ts b/src/@batch-flask/ui/file/file-viewer/file-viewer-container/file-viewer-header/file-viewer-header.component.spec.ts index 61f4d89884..1ddd28bff1 100644 --- a/src/@batch-flask/ui/file/file-viewer/file-viewer-container/file-viewer-header/file-viewer-header.component.spec.ts +++ b/src/@batch-flask/ui/file/file-viewer/file-viewer-container/file-viewer-header/file-viewer-header.component.spec.ts @@ -1,5 +1,5 @@ import { Component, DebugElement } from "@angular/core"; -import { ComponentFixture, TestBed } from "@angular/core/testing"; +import { ComponentFixture, fakeAsync, flush, TestBed } from "@angular/core/testing"; import { By } from "@angular/platform-browser"; import { ServerError } from "@batch-flask/core"; import { TimeZoneTestingModule } from "@batch-flask/core/testing"; @@ -93,12 +93,14 @@ describe("FileViewerHeaderComponent", () => { expect(de.query(By.css("bl-download-button bl-button"))).not.toBeFalsy(); }); - it("download the file if clicking download button", () => { + it("download the file if clicking download button", fakeAsync(() => { const downloadSpy = spyOn(testComponent.fileLoader, "download") .and.returnValue(of("/some/local/path/foo.ts")); click(de.query(By.css("bl-download-button bl-button"))); - expect(TestBed.get(ElectronRemote).dialog.showSaveDialog).toHaveBeenCalledOnce(); - expect(TestBed.get(ElectronRemote).dialog.showSaveDialog).toHaveBeenCalledWith({ + flush(); + + expect(TestBed.inject(ElectronRemote).dialog.showSaveDialog).toHaveBeenCalledOnce(); + expect(TestBed.inject(ElectronRemote).dialog.showSaveDialog).toHaveBeenCalledWith({ buttonLabel: "Download", defaultPath: "foo.ts", }); @@ -106,22 +108,24 @@ describe("FileViewerHeaderComponent", () => { expect(downloadSpy).toHaveBeenCalledWith("/some/local/path/foo.ts"); // Show the item - expect(TestBed.get(ElectronShell).showItemInFolder).toHaveBeenCalledOnce(); - expect(TestBed.get(ElectronShell).showItemInFolder).toHaveBeenCalledWith("/some/local/path/foo.ts"); - }); + expect(TestBed.inject(ElectronShell).showItemInFolder).toHaveBeenCalledOnce(); + expect(TestBed.inject(ElectronShell).showItemInFolder).toHaveBeenCalledWith("/some/local/path/foo.ts"); + })); - it("show a notification if the download failed", () => { + it("show a notification if the download failed", fakeAsync(() => { const downloadSpy = spyOn(testComponent.fileLoader, "download") .and.returnValue(throwError(new ServerError({ message: "Some fake error" } as any))); click(de.query(By.css("bl-download-button bl-button"))); + flush(); + expect(downloadSpy).toHaveBeenCalledOnce(); // Show the item expect(notificationSpy.error).toHaveBeenCalledOnce(); expect(notificationSpy.error) .toHaveBeenCalledWith("Download failed", "foo.ts failed to download. Some fake error"); - }); + })); }); it("open the file in the default editor", () => { diff --git a/src/@batch-flask/ui/file/file-viewer/file-viewer-container/file-viewer-header/file-viewer-header.component.ts b/src/@batch-flask/ui/file/file-viewer/file-viewer-container/file-viewer-header/file-viewer-header.component.ts index 5547a6e80a..ee7dad72a2 100644 --- a/src/@batch-flask/ui/file/file-viewer/file-viewer-container/file-viewer-header/file-viewer-header.component.ts +++ b/src/@batch-flask/ui/file/file-viewer/file-viewer-container/file-viewer-header/file-viewer-header.component.ts @@ -5,7 +5,8 @@ import { FileLoader } from "@batch-flask/ui/file/file-loader"; import { File } from "@batch-flask/ui/file/file.model"; import { NotificationService } from "@batch-flask/ui/notifications"; import { prettyBytes } from "@batch-flask/utils"; -import { Subscription } from "rxjs"; +import { SaveDialogReturnValue } from "electron"; +import { Observable, Subscription } from "rxjs"; import { FileViewer, FileViewerCommand, FileViewerConfig } from "../../file-viewer"; import "./file-viewer-header.scss"; @@ -66,13 +67,15 @@ export class FileViewerHeaderComponent implements OnChanges { } @autobind() - public downloadFile() { + public async downloadFile(): Promise> { const dialog = this.remote.dialog; - const localPath = dialog.showSaveDialog({ + const retValue: SaveDialogReturnValue = await dialog.showSaveDialog({ buttonLabel: "Download", defaultPath: this.fileLoader.filename, }); + const localPath: string = retValue.filePath; + if (localPath) { return this._saveFile(localPath); } @@ -102,7 +105,7 @@ export class FileViewerHeaderComponent implements OnChanges { } } - private _saveFile(pathToFile) { + private _saveFile(pathToFile: string): Observable { if (pathToFile === undefined) { return; }