This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
notifications.test.jsx
114 lines (97 loc) · 4.19 KB
/
notifications.test.jsx
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable global-require */
describe('Notifications.showNotification', () => {
let Notifications;
beforeEach(() => {
jest.resetModules();
Notifications = require('utils/notifications.tsx');
});
it('should throw an exception if Notification is not defined on window', async () => {
await expect(Notifications.showNotification()).rejects.toThrow('Notification not supported');
});
it('should throw an exception if Notification.requestPermission is not defined', async () => {
window.Notification = {};
await expect(Notifications.showNotification()).rejects.toThrow('Notification.requestPermission not supported');
});
it('should throw an exception if Notification.requestPermission is not a function', async () => {
window.Notification = {
requestPermission: true,
};
await expect(Notifications.showNotification()).rejects.toThrow('Notification.requestPermission not supported');
});
it('should request permissions, promise style, if not previously requested, handling rejection', async () => {
window.Notification = {
requestPermission: () => Promise.resolve('denied'),
permission: 'denied',
};
await expect(Notifications.showNotification()).rejects.toThrow('Notifications not granted');
});
it('should request permissions, callback style, if not previously requested, handling rejection', async () => {
window.Notification = {
requestPermission: (callback) => {
if (callback) {
callback('denied');
}
},
permission: 'denied',
};
await expect(Notifications.showNotification()).rejects.toThrow('Notifications not granted');
});
it('should request permissions, promise style, if not previously requested, handling success', async () => {
window.Notification = jest.fn();
window.Notification.requestPermission = () => Promise.resolve('granted');
window.Notification.permission = 'denied';
const n = {};
window.Notification.mockReturnValueOnce(n);
await expect(Notifications.showNotification({
body: 'body',
requireInteraction: true,
silent: false,
})).resolves.toBeTruthy();
await expect(window.Notification.mock.calls.length).toBe(1);
const call = window.Notification.mock.calls[0];
expect(call[1]).toEqual({
body: 'body',
tag: 'body',
icon: {},
requireInteraction: true,
silent: false,
});
});
it('should request permissions, callback style, if not previously requested, handling success', async () => {
window.Notification = jest.fn();
window.Notification.requestPermission = (callback) => {
if (callback) {
callback('granted');
}
};
window.Notification.permission = 'denied';
const n = {};
window.Notification.mockReturnValueOnce(n);
await expect(Notifications.showNotification({
body: 'body',
requireInteraction: true,
silent: false,
})).resolves.toBeTruthy();
await expect(window.Notification.mock.calls.length).toBe(1);
const call = window.Notification.mock.calls[0];
expect(call[1]).toEqual({
body: 'body',
tag: 'body',
icon: {},
requireInteraction: true,
silent: false,
});
});
it('should do nothing if permissions previously requested but not granted', async () => {
window.Notification = {
requestPermission: () => Promise.resolve('denied'),
permission: 'denied',
};
// Call one to deny and mark as already requested
await expect(Notifications.showNotification()).rejects.toThrow('Notifications not granted');
// Try again
await expect(Notifications.showNotification()).rejects.toThrow('Notifications already requested but not granted');
});
});