8000 More tests · NativeLaravel/electron-plugin@9160101 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9160101

Browse files
committed
More tests
1 parent 1664128 commit 9160101

File tree

5 files changed

+121
-16
lines changed

5 files changed

+121
-16
lines changed

src/server/api/dialog.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import express from 'express'
22
import {dialog} from 'electron'
33
import state from '../state'
4+
import {trimOptions} from '../utils'
45
const router = express.Router();
56

6-
function trimOptions(options) {
7-
Object.keys(options).forEach(key => options[key] == null && delete options[key]);
8-
9-
return options;
10-
}
11-
12-
function findWindow(id) {
13-
return state.windows[id] || null
14-
}
15-
167
router.post('/open', (req, res) => {
178
const {title, buttonLabel, filters, properties, defaultPath, message, windowReference} = req.body
189

@@ -27,8 +18,9 @@ router.post('/open', (req, res) => {
2718

2819
options = trimOptions(options);
2920

30-
let browserWindow = findWindow(windowReference);
31-
let result = null;
21+
let result;
22+
let browserWindow = state.findWindow(windowReference);
23+
3224
if (browserWindow) {
3325
result = dialog.showOpenDialogSync(browserWindow, options)
3426
} else {

src/server/state.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ interface State {
77
caCert: string | null;
88
icon: string | null
99
windows: Record<string, BrowserWindow>
10-
randomSecret: string
10+
randomSecret: string,
11+
findWindow: (id: string) => BrowserWindow | null
1112
}
1213

1314
function generateRandomString(length: number) {
@@ -30,5 +31,8 @@ export default {
3031
caCert: null,
3132
icon: null,
3233
randomSecret: generateRandomString(32),
33-
windows: {}
34+
windows: {},
35+
findWindow(id: string) {
36+
return this.windows[id] || null
37+
}
3438
} as State;

src/server/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ export async function notifyLaravel(endpoint: string, payload = {}) {
2626
//
2727
}
2828
}
29+
30+
/**
31+
* Remove null and undefined values from an object
32+
*/
33+
export function trimOptions(options: any): any {
34+
Object.keys(options).forEach(key => options[key] == null && delete options[key]);
35+
36+
return options;
37+
}

tests/endpoints/clipboard.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ describe('Clipboard test', () => {
6868
});
6969

7070
it('returns an error when trying to write an invalid data URL as image', async () => {
71-
const electron = require('electron');
72-
7371
try {
7472
const response = await axios.post('/clipboard/image', {
7573
image: 'new image data URL',

tests/endpoints/dialog.test.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import startAPIServer, { APIProcess } from "../../src/server/api";
2+
import axios, { AxiosError } from "axios";
3+
import electron from "electron";
4+
import exp = require("constants");
5+
6+
let apiServer: APIProcess;
7+
8+
const mockReadImage = {
9+
isEmpty: jest.fn(() => true),
10+
toDataURL: jest.fn(() => 'clipboard image'),
11+
};
12+
13+
jest.mock('electron', () => ({
14+
dialog: {
15+
showOpenDialogSync: jest.fn(() => ['open dialog result']),
16+
showSaveDialogSync: jest.fn(() => ['save dialog result']),
17+
showMessageBoxSync: jest.fn(() => 1),
18+
showErrorBox: jest.fn(),
19+
}
20+
}));
21+
22+
describe('Dialog test', () => {
23+
beforeEach(async () => {
24+
apiServer = await startAPIServer('randomSecret')
25+
26+
axios.defaults.baseURL = `http://localhost:${apiServer.port}/api`;
27+
axios.defaults.headers.common['x-nativephp-secret'] = 'randomSecret';
28+
})
29+
30+
afterEach(done => {
31+
apiServer.server.close(done);
32+
});
33+
34+
it('can open a dialog', async () => {
35+
const electron = require('electron');
36+
const options = {
37+
title: 'Open Dialog',
38+
defaultPath: '/home/user/Desktop',
39+
buttonLabel: 'Open',
40+
filters: [
41+
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
42+
{ name: 'All Files', extensions: ['*'] }
43+
],
44+
message: 'Select an image',
45+
properties: ['openFile', 'multiSelections']
46+
}
47+
48+
const response = await axios.post('/dialog/open', options);
49+
50+
expect(electron.dialog.showOpenDialogSync).toHaveBeenCalledWith(options);
51+
expect(response.data.result).toEqual(['open dialog result']);
52+
});
53+
54+
it('can open a save dialog', async () => {
55+
const electron = require('electron');
56+
const options = {
57+
title: 'Open Dialog',
58+
defaultPath: '/home/user/Desktop',
59+
buttonLabel: 'Open',
60+
filters: [
61+
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
62+
{ name: 'All Files', extensions: ['*'] }
63+
],
64+
message: 'Select an image',
65+
properties: ['openFile', 'multiSelections']
66+
}
67+
68+
const response = await axios.post('/dialog/save', options);
69+
70+
expect(electron.dialog.showSaveDialogSync).toHaveBeenCalledWith(options);
71+
expect(response.data.result).toEqual(['save dialog result']);
72+
});
73+
74+
it('can open a message dialog', async () => {
75+
const electron = require('electron');
76+
const options = {
77+
title: 'Open Dialog',
78+
message: 'Select an image',
79+
type: 'info',
80+
buttons: ['OK', 'Cancel']
81+
}
82+
83+
const response = await axios.post('/dialog/message', options);
84+
85+
expect(electron.dialog.showMessageBoxSync).toHaveBeenCalledWith(options);
86+
expect(response.data.result).toEqual(1);
87+
});
88+
89+
it('can open an error dialog', async () => {
90+
const electron = require('electron');
91+
const options = {
92+
title: 'Error Dialog',
93+
message: 'Uh oh!',
94+
}
95+
96+
const response = await axios.post('/dialog/error', options);
97+
98+
expect(electron.dialog.showErrorBox).toHaveBeenCalledWith(options.title, options.message);
99+
expect(response.data.result).toEqual(true);
100+
});
101+
102+
});

0 commit comments

Comments
 (0)
0