Skip to content

Commit

Permalink
Fixes #2272: Task file download
Browse files Browse the repository at this point in the history
Electron's `dialog.showSaveDialog` has changed at some point to return a `Promise` rather than a string value. Adjusted to the new API.
  • Loading branch information
gingi committed Feb 4, 2021
1 parent 321d219 commit 47d8a54
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/@batch-flask/electron/testing/electron-testing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -93,35 +93,39 @@ 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",
});
expect(downloadSpy).toHaveBeenCalledOnce();
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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -66,13 +67,15 @@ export class FileViewerHeaderComponent implements OnChanges {
}

@autobind()
public downloadFile() {
public async downloadFile(): Promise<Observable<string>> {
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);
}
Expand Down Expand Up @@ -102,7 +105,7 @@ export class FileViewerHeaderComponent implements OnChanges {
}
}

private _saveFile(pathToFile) {
private _saveFile(pathToFile: string): Observable<string> {
if (pathToFile === undefined) {
return;
}
Expand Down

0 comments on commit 47d8a54

Please sign in to comment.