Skip to content

Commit

Permalink
fix: Add notification permission request and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Feb 10, 2025
1 parent 80bc9a7 commit 5e9835a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
80 changes: 80 additions & 0 deletions frontend/src/services/notification.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { sendNotification } from "./notification";

describe("sendNotification", () => {
beforeEach(() => {
// Clear localStorage
localStorage.clear();
// Reset mocks
vi.clearAllMocks();
// Reset Notification mock
vi.unstubAllGlobals();
});

it("should not send notification when notifications are disabled", async () => {
const NotificationMock = vi.fn();
NotificationMock.permission = "granted";
NotificationMock.requestPermission = vi.fn().mockResolvedValue("granted");
vi.stubGlobal("Notification", NotificationMock);

localStorage.setItem("notifications-enabled", "false");
await sendNotification("Test notification");

expect(NotificationMock).not.toHaveBeenCalled();
});

it("should send notification when notifications are enabled and permission is granted", async () => {
const NotificationMock = vi.fn();
NotificationMock.permission = "granted";
NotificationMock.requestPermission = vi.fn().mockResolvedValue("granted");
vi.stubGlobal("Notification", NotificationMock);

localStorage.setItem("notifications-enabled", "true");
await sendNotification("Test notification");

expect(NotificationMock).toHaveBeenCalledWith(
"Test notification",
undefined,
);
});

it("should request permission when notifications are enabled but permission is not granted", async () => {
const NotificationMock = vi.fn();
NotificationMock.permission = "default";
NotificationMock.requestPermission = vi.fn().mockResolvedValue("granted");
vi.stubGlobal("Notification", NotificationMock);

localStorage.setItem("notifications-enabled", "true");
await sendNotification("Test notification");

expect(NotificationMock.requestPermission).toHaveBeenCalled();
expect(NotificationMock).toHaveBeenCalledWith(
"Test notification",
undefined,
);
});

it("should not send notification when permission is denied", async () => {
const NotificationMock = vi.fn();
NotificationMock.permission = "denied";
NotificationMock.requestPermission = vi.fn().mockResolvedValue("denied");
vi.stubGlobal("Notification", NotificationMock);

localStorage.setItem("notifications-enabled", "true");
await sendNotification("Test notification");

expect(NotificationMock).not.toHaveBeenCalled();
});

it("should not request permission when permission is denied", async () => {
const NotificationMock = vi.fn();
NotificationMock.permission = "denied";
NotificationMock.requestPermission = vi.fn().mockResolvedValue("denied");
vi.stubGlobal("Notification", NotificationMock);

localStorage.setItem("notifications-enabled", "true");
await sendNotification("Test notification");

expect(NotificationMock.requestPermission).not.toHaveBeenCalled();
});
});
8 changes: 7 additions & 1 deletion frontend/src/services/notification.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const sendNotification = (
export const sendNotification = async (
title: string,
options?: NotificationOptions,
) => {
Expand All @@ -17,5 +17,11 @@ export const sendNotification = (
if (Notification.permission === "granted") {
// eslint-disable-next-line no-new
new Notification(title, options);
} else if (Notification.permission !== "denied") {
const permission = await Notification.requestPermission();
if (permission === "granted") {
// eslint-disable-next-line no-new
new Notification(title, options);
}
}
};

0 comments on commit 5e9835a

Please sign in to comment.