-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add notification permission request and tests
- Loading branch information
1 parent
80bc9a7
commit 5e9835a
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters