Skip to content

Commit aa7f108

Browse files
authored
Add alert API routes and corresponding tests (#184)
* Add alert API routes and corresponding tests * Add space * cleanup dialog.test.ts
1 parent cb7a3c8 commit aa7f108

File tree

5 files changed

+89
-51
lines changed

5 files changed

+89
-51
lines changed

resources/js/electron-plugin/src/server/api.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import getPort, {portNumbers} from "get-port";
44
import middleware from "./api/middleware.js";
55

66
import clipboardRoutes from "./api/clipboard.js";
7+
import alertRoutes from "./api/alert.js";
78
import appRoutes from "./api/app.js";
89
import screenRoutes from "./api/screen.js";
910
import dialogRoutes from "./api/dialog.js";
@@ -40,6 +41,7 @@ async function startAPIServer(randomSecret: string): Promise<APIProcess> {
4041
httpServer.use(middleware(randomSecret));
4142
httpServer.use(bodyParser.json());
4243
httpServer.use("/api/clipboard", clipboardRoutes);
44+
httpServer.use("/api/alert", alertRoutes);
4345
httpServer.use("/api/app", appRoutes);
4446
httpServer.use("/api/screen", screenRoutes);
4547
httpServer.use("/api/dialog", dialogRoutes);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import express from 'express'
2+
import { dialog } from 'electron'
3+
const router = express.Router();
4+
5+
router.post('/message', (req, res) => {
6+
const { message, type, title, detail, buttons, defaultId, cancelId } = req.body;
7+
const result = dialog.showMessageBoxSync({
8+
message,
9+
type,
10+
title,
11+
detail,
12+
buttons,
13+
defaultId,
14+
cancelId
15+
});
16+
res.json({
17+
result
18+
});
19+
});
20+
21+
router.post('/error', (req, res) => {
22+
const { title, message } = req.body;
23+
24+
dialog.showErrorBox(title, message);
25+
26+
res.json({
27+
result: true
28+
});
29+
});
30+
31+
export default router;

resources/js/electron-plugin/src/server/api/dialog.ts

-25
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,4 @@ router.post('/save', (req, res) => {
6060
})
6161
});
6262

63-
router.post('/message', (req, res) => {
64-
const {title, message, type, buttons} = req.body
65-
66-
const result = dialog.showMessageBoxSync({
67-
title,
68-
message,
69-
type,
70-
buttons
71-
})
72-
73-
res.json({
74-
result
75-
})
76-
});
77-
78-
router.post('/error', (req, res) => {
79-
const {title, message} = req.body
80-
81-
dialog.showErrorBox(title, message)
82-
83-
res.json({
84-
result: true
85-
})
86-
});
87-
8863
export default router;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import startAPIServer, {APIProcess} from "../../src/server/api";
2+
import axios from "axios";
3+
import electron from "electron";
4+
5+
let apiServer: APIProcess;
6+
7+
jest.mock('electron', () => ({
8+
...jest.requireActual('electron'),
9+
10+
dialog: {
11+
showMessageBoxSync: jest.fn(() => 1),
12+
showErrorBox: jest.fn(),
13+
}
14+
}));
15+
16+
describe('Alert test', () => {
17+
beforeEach(async () => {
18+
apiServer = await startAPIServer('randomSecret')
19+
20+
axios.defaults.baseURL = `http://localhost:${apiServer.port}/api`;
21+
axios.defaults.headers.common['x-nativephp-secret'] = 'randomSecret';
22+
})
23+
24+
afterEach(done => {
25+
apiServer.server.close(done);
26+
});
27+
28+
it('can open a alert', async () => {
29+
const options = {
30+
message: 'Do you really want to delete this?',
31+
type: 'info',
32+
title: 'Are you sure',
33+
detail: 'This action cannot be undone',
34+
buttons: ['Delete', 'Cancel'],
35+
defaultId: 0,
36+
cancelId: 1
37+
}
38+
39+
const response = await axios.post('/alert/message', options);
40+
41+
expect(electron.dialog.showMessageBoxSync).toHaveBeenCalledWith(options);
42+
expect(response.data.result).toEqual(1);
43+
});
44+
45+
it('can open an error alert', async () => {
46+
const options = {
47+
title: 'Error Dialog',
48+
message: 'Uh oh!',
49+
}
50+
51+
const response = await axios.post('/alert/error', options);
52+
53+
expect(electron.dialog.showErrorBox).toHaveBeenCalledWith(options.title, options.message);
54+
expect(response.data.result).toEqual(true);
55+
});
56+
});

resources/js/electron-plugin/tests/endpoints/dialog.test.ts

-26
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,4 @@ describe('Dialog test', () => {
6666
expect(response.data.result).toEqual(['save dialog result']);
6767
});
6868

69-
it('can open a message dialog', async () => {
70-
const options = {
71-
title: 'Open Dialog',
72-
message: 'Select an image',
73-
type: 'info',
74-
buttons: ['OK', 'Cancel']
75-
}
76-
77-
const response = await axios.post('/dialog/message', options);
78-
79-
expect(electron.dialog.showMessageBoxSync).toHaveBeenCalledWith(options);
80-
expect(response.data.result).toEqual(1);
81-
});
82-
83-
it('can open an error dialog', async () => {
84-
const options = {
85-
title: 'Error Dialog',
86-
message: 'Uh oh!',
87-
}
88-
89-
const response = await axios.post('/dialog/error', options);
90-
91-
expect(electron.dialog.showErrorBox).toHaveBeenCalledWith(options.title, options.message);
92-
expect(response.data.result).toEqual(true);
93-
});
94-
9569
});

0 commit comments

Comments
 (0)