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

Add alert API routes and corresponding tests #184

Merged
merged 3 commits into from
Mar 25, 2025
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
2 changes: 2 additions & 0 deletions resources/js/electron-plugin/src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -40,6 +41,7 @@ async function startAPIServer(randomSecret: string): Promise<APIProcess> {
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);
Expand Down
31 changes: 31 additions & 0 deletions resources/js/electron-plugin/src/server/api/alert.ts
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 0 additions & 25 deletions resources/js/electron-plugin/src/server/api/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
56 changes: 56 additions & 0 deletions resources/js/electron-plugin/tests/endpoints/alert.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
26 changes: 0 additions & 26 deletions resources/js/electron-plugin/tests/endpoints/dialog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
Loading