diff --git a/resources/js/electron-plugin/src/server/api.ts b/resources/js/electron-plugin/src/server/api.ts index dea608d2..0baa33ea 100644 --- a/resources/js/electron-plugin/src/server/api.ts +++ b/resources/js/electron-plugin/src/server/api.ts @@ -4,6 +4,7 @@ import getPort, {portNumbers} from "get-port"; import middleware from "./api/middleware.js"; import clipboardRoutes from "./api/clipboard.js"; +import alertRoutes from "./api/alert.js"; import appRoutes from "./api/app.js"; import screenRoutes from "./api/screen.js"; import dialogRoutes from "./api/dialog.js"; @@ -40,6 +41,7 @@ async function startAPIServer(randomSecret: string): Promise { httpServer.use(middleware(randomSecret)); httpServer.use(bodyParser.json()); httpServer.use("/api/clipboard", clipboardRoutes); + httpServer.use("/api/alert", alertRoutes); httpServer.use("/api/app", appRoutes); httpServer.use("/api/screen", screenRoutes); httpServer.use("/api/dialog", dialogRoutes); diff --git a/resources/js/electron-plugin/src/server/api/alert.ts b/resources/js/electron-plugin/src/server/api/alert.ts new file mode 100644 index 00000000..2b7ba560 --- /dev/null +++ b/resources/js/electron-plugin/src/server/api/alert.ts @@ -0,0 +1,31 @@ +import express from 'express' +import { dialog } from 'electron' +const router = express.Router(); + +router.post('/message', (req, res) => { + const { message, type, title, detail, buttons, defaultId, cancelId } = req.body; + const result = dialog.showMessageBoxSync({ + message, + type, + title, + detail, + buttons, + defaultId, + cancelId + }); + res.json({ + result + }); +}); + +router.post('/error', (req, res) => { + const { title, message } = req.body; + + dialog.showErrorBox(title, message); + + res.json({ + result: true + }); +}); + +export default router; diff --git a/resources/js/electron-plugin/src/server/api/dialog.ts b/resources/js/electron-plugin/src/server/api/dialog.ts index f91f0936..74cb2a55 100644 --- a/resources/js/electron-plugin/src/server/api/dialog.ts +++ b/resources/js/electron-plugin/src/server/api/dialog.ts @@ -60,29 +60,4 @@ router.post('/save', (req, res) => { }) }); -router.post('/message', (req, res) => { - const {title, message, type, buttons} = req.body - - const result = dialog.showMessageBoxSync({ - title, - message, - type, - buttons - }) - - res.json({ - result - }) -}); - -router.post('/error', (req, res) => { - const {title, message} = req.body - - dialog.showErrorBox(title, message) - - res.json({ - result: true - }) -}); - export default router; diff --git a/resources/js/electron-plugin/tests/endpoints/alert.test.ts b/resources/js/electron-plugin/tests/endpoints/alert.test.ts new file mode 100644 index 00000000..025df8ef --- /dev/null +++ b/resources/js/electron-plugin/tests/endpoints/alert.test.ts @@ -0,0 +1,56 @@ +import startAPIServer, {APIProcess} from "../../src/server/api"; +import axios from "axios"; +import electron from "electron"; + +let apiServer: APIProcess; + +jest.mock('electron', () => ({ + ...jest.requireActual('electron'), + + dialog: { + showMessageBoxSync: jest.fn(() => 1), + showErrorBox: jest.fn(), + } +})); + +describe('Alert test', () => { + beforeEach(async () => { + apiServer = await startAPIServer('randomSecret') + + axios.defaults.baseURL = `http://localhost:${apiServer.port}/api`; + axios.defaults.headers.common['x-nativephp-secret'] = 'randomSecret'; + }) + + afterEach(done => { + apiServer.server.close(done); + }); + + it('can open a alert', async () => { + const options = { + message: 'Do you really want to delete this?', + type: 'info', + title: 'Are you sure', + detail: 'This action cannot be undone', + buttons: ['Delete', 'Cancel'], + defaultId: 0, + cancelId: 1 + } + + const response = await axios.post('/alert/message', options); + + expect(electron.dialog.showMessageBoxSync).toHaveBeenCalledWith(options); + expect(response.data.result).toEqual(1); + }); + + it('can open an error alert', async () => { + const options = { + title: 'Error Dialog', + message: 'Uh oh!', + } + + const response = await axios.post('/alert/error', options); + + expect(electron.dialog.showErrorBox).toHaveBeenCalledWith(options.title, options.message); + expect(response.data.result).toEqual(true); + }); +}); diff --git a/resources/js/electron-plugin/tests/endpoints/dialog.test.ts b/resources/js/electron-plugin/tests/endpoints/dialog.test.ts index 239c4a6e..69a11df0 100644 --- a/resources/js/electron-plugin/tests/endpoints/dialog.test.ts +++ b/resources/js/electron-plugin/tests/endpoints/dialog.test.ts @@ -66,30 +66,4 @@ describe('Dialog test', () => { expect(response.data.result).toEqual(['save dialog result']); }); - it('can open a message dialog', async () => { - const options = { - title: 'Open Dialog', - message: 'Select an image', - type: 'info', - buttons: ['OK', 'Cancel'] - } - - const response = await axios.post('/dialog/message', options); - - expect(electron.dialog.showMessageBoxSync).toHaveBeenCalledWith(options); - expect(response.data.result).toEqual(1); - }); - - it('can open an error dialog', async () => { - const options = { - title: 'Error Dialog', - message: 'Uh oh!', - } - - const response = await axios.post('/dialog/error', options); - - expect(electron.dialog.showErrorBox).toHaveBeenCalledWith(options.title, options.message); - expect(response.data.result).toEqual(true); - }); - });