forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationToggle.test.tsx
More file actions
63 lines (54 loc) · 1.81 KB
/
Copy pathPushNotificationToggle.test.tsx
File metadata and controls
63 lines (54 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import PushNotificationToggle from '../PushNotificationToggle';
describe('PushNotificationToggle', () => {
const requestPermission = jest.fn();
const subscribe = jest.fn();
const getSubscription = jest.fn();
beforeEach(() => {
process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY = 'dGVzdA==';
requestPermission.mockReset();
requestPermission.mockResolvedValue('granted');
subscribe.mockReset();
subscribe.mockResolvedValue({});
getSubscription.mockReset();
getSubscription.mockResolvedValue(null);
Object.defineProperty(window, 'Notification', {
configurable: true,
value: {
permission: 'default',
requestPermission,
},
});
Object.defineProperty(navigator, 'serviceWorker', {
configurable: true,
value: {
register: jest.fn().mockResolvedValue({
active: {},
ready: Promise.resolve({
pushManager: {
subscribe,
getSubscription,
},
showNotification: jest.fn(),
}),
}),
},
});
global.fetch = jest.fn().mockResolvedValue({ ok: true }) as jest.Mock;
});
test('requests permission and saves the push subscription when enabled', async () => {
render(<PushNotificationToggle />);
fireEvent.click(screen.getByRole('button', { name: /enable alerts/i }));
await waitFor(() => expect(requestPermission).toHaveBeenCalled());
await waitFor(() => expect(subscribe).toHaveBeenCalled());
await waitFor(() =>
expect(global.fetch).toHaveBeenCalledWith(
'/api/push',
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({ 'Content-Type': 'application/json' }),
}),
),
);
});
});