Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KOGITO-1791: Saving new file should add automatically an extension #141

Merged
merged 2 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/desktop/__mocks__/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,38 @@
import * as electron from "electron";

const mockIpcRendererEvents = new Map<string, (event?: electron.IpcRendererEvent, ...args: any[]) => void>();
const mockIpcMainEvents = new Map<string, (event?: electron.IpcMainEvent, ...args: any[]) => void>();

export const ipcRenderer = {
on(channel: string, listener: (event?: electron.IpcRendererEvent, ...args: any[]) => void) {
mockIpcRendererEvents.set(channel, listener);
},
send(channel: string, ...args: any[]) {
mockIpcRendererEvents.get(channel)?.(undefined, ...args);
mockIpcMainEvents.get(channel)?.(undefined, ...args);
},
removeAllListeners(channel: string) {
mockIpcRendererEvents.delete(channel);
}
};

export const ipcMain = {
on(channel: string, listener: (event: electron.IpcMainEvent, ...args: any[]) => void) {
mockIpcMainEvents.set(channel, listener);
},
removeAllListeners(channel: string) {
mockIpcMainEvents.delete(channel);
}
};

export class BrowserWindow {
constructor(options?: electron.BrowserWindowConstructorOptions) { /**/ }
};

export const showSaveDialogMock = jest.fn((browserWindow: BrowserWindow, options: electron.SaveDialogOptions) => {
return Promise.resolve();
});

export const dialog = {
showSaveDialog: showSaveDialogMock
};
67 changes: 67 additions & 0 deletions packages/desktop/src/__tests__/backend/FileOperations.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { act } from "react-dom/test-utils";
import * as electron from "electron";
import { FileSaveActions } from "../../common/File";
import { DesktopUserData } from "../../backend/DesktopUserData";
import { Menu } from "../../backend/Menu";
import { FileOperations } from "../../backend/FileOperations";
import { showSaveDialogMock } from "../../../__mocks__/electron";

beforeEach(() => {
document.execCommand = () => true;
});

jest.mock("../../backend/DesktopUserData", () => {
return {
DesktopUserData: jest.fn().mockImplementation()
};
});

jest.mock("../../backend/Menu", () => {
return {
Menu: jest.fn().mockImplementation()
};
});

describe("saveFile ipc event", () => {
test("check dialog for save file as operation", () => {
const window = new electron.BrowserWindow();
const userData = new DesktopUserData();
const menu = new Menu(window, userData);
const fileOperations = new FileOperations(window, menu, userData);

act(() =>
electron.ipcRenderer.send("saveFile", {
action: FileSaveActions.SAVE_AS,
file: {
fileType: "dmn",
fileContent: "content"
}
})
);

expect(showSaveDialogMock).toHaveBeenCalledTimes(1);
expect(showSaveDialogMock.mock.calls[0][0]).toEqual(window);
expect(showSaveDialogMock.mock.calls[0][1].defaultPath).toEqual("model.dmn");
expect(showSaveDialogMock.mock.calls[0][1].title).toEqual("Save file");
expect(showSaveDialogMock.mock.calls[0][1].filters!).toHaveLength(1);
expect(showSaveDialogMock.mock.calls[0][1].filters![0].name).toEqual("DMN");
expect(showSaveDialogMock.mock.calls[0][1].filters![0].extensions).toHaveLength(1);
expect(showSaveDialogMock.mock.calls[0][1].filters![0].extensions[0]).toEqual("dmn");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class FileOperations {

dialog
.showSaveDialog(this.window, {
defaultPath: "model." + data.file.fileType,
title: "Save file",
filters: [{ name: data.file.fileType.toUpperCase(), extensions: [data.file.fileType] }]
})
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = [
...commonConfig,
target: "electron-main",
entry: {
index: "./src/electron/index.ts"
index: "./src/backend/index.ts"
},
plugins: [new CopyPlugin([{ from: "./build", to: "./build" }])],
node: {
Expand Down