Skip to content
Merged

Sentry #1631

Show file tree
Hide file tree
Changes from 4 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
53 changes: 53 additions & 0 deletions __tests__/components/notifications/NotificationsContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import React from "react";
const push = jest.fn();
const mockUseRouter = jest.fn(() => ({ push }));

let mockIsActive = true;
jest.mock("@/hooks/useCapacitor", () => () => ({
isCapacitor: true,
isIos: true,
get isActive() {
return mockIsActive;
},
}));
jest.mock("next/navigation", () => ({
__esModule: true,
Expand Down Expand Up @@ -43,6 +47,9 @@ jest.mock("@capacitor/push-notifications", () => {
jest.mock("@capacitor/device", () => ({
Device: { getInfo: jest.fn().mockResolvedValue({ platform: "ios" }) },
}));
jest.mock("@/components/notifications/stable-device-id", () => ({
getStableDeviceId: jest.fn().mockResolvedValue("test-device-id"),
}));

const wrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<NotificationsProvider>{children}</NotificationsProvider>
Expand All @@ -68,6 +75,52 @@ describe("NotificationsContext", () => {
});
});

describe("NotificationsContext initialization", () => {
beforeEach(() => {
mockIsActive = true;
const { PushNotifications } = require("@capacitor/push-notifications");
jest.clearAllMocks();
PushNotifications.removeAllListeners.mockClear();
PushNotifications.addListener.mockClear();
PushNotifications.register.mockClear();
});

it("does not initialize when isActive is false", async () => {
mockIsActive = false;
const { PushNotifications } = require("@capacitor/push-notifications");

renderHook(() => useNotificationsContext(), { wrapper });

await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 100));
});

expect(PushNotifications.removeAllListeners).not.toHaveBeenCalled();
expect(PushNotifications.addListener).not.toHaveBeenCalled();
expect(PushNotifications.register).not.toHaveBeenCalled();
});

it("initializes when isActive is true", async () => {
mockIsActive = true;
const { PushNotifications } = require("@capacitor/push-notifications");
renderHook(() => useNotificationsContext(), { wrapper });

await waitFor(() => {
expect(PushNotifications.removeAllListeners).toHaveBeenCalled();
});

expect(PushNotifications.addListener).toHaveBeenCalled();
expect(PushNotifications.requestPermissions).toHaveBeenCalled();

await waitFor(
() => {
expect(PushNotifications.register).toHaveBeenCalled();
},
{ timeout: 2000 }
);
Comment thread
prxt6529 marked this conversation as resolved.
});
});

it("removes notifications when functions called", async () => {
const { PushNotifications } = require("@capacitor/push-notifications");
const { result } = renderHook(() => useNotificationsContext(), { wrapper });
Expand Down
Loading