-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathindex.test.tsx
More file actions
241 lines (211 loc) · 7.82 KB
/
index.test.tsx
File metadata and controls
241 lines (211 loc) · 7.82 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { TRIGGER_TYPES } from '@metamask/notification-services-controller/notification-services';
import {
enableNotifications,
disableNotifications,
fetchAccountNotificationSettings,
disableAccounts,
enableAccounts,
toggleFeatureAnnouncements,
fetchNotifications,
markNotificationsAsRead,
enablePushNotifications,
disablePushNotifications,
hasNotificationPreferences,
type setContentPreviewToken as setContentPreviewTokenFn,
type getContentPreviewToken as getContentPreviewTokenFn,
type subscribeToContentPreviewToken as subscribeToContentPreviewTokenFn,
} from '.';
import Engine from '../../../core/Engine';
jest.mock('../../../util/notifications', () => ({
isNotificationsFeatureEnabled: () => true,
}));
jest.mock('../../../core/Engine', () => ({
context: {
NotificationServicesController: {
enableMetamaskNotifications: jest.fn(),
disableNotificationServices: jest.fn(),
checkAccountsPresence: jest.fn(),
disableAccounts: jest.fn(),
enableAccounts: jest.fn(),
createOnChainTriggers: jest.fn(),
setFeatureAnnouncementsEnabled: jest.fn(),
setPerpsNotificationsEnabled: jest.fn(),
fetchAndUpdateMetamaskNotifications: jest.fn(),
markMetamaskNotificationsAsRead: jest.fn(),
enablePushNotifications: jest.fn(),
disablePushNotifications: jest.fn(),
},
},
controllerMessenger: {
call: jest.fn(),
},
}));
beforeEach(() => {
jest.clearAllMocks();
});
describe('helpers - enableNotificationServices()', () => {
it('invoke notification services method', async () => {
await enableNotifications();
expect(
Engine.context.NotificationServicesController.enableMetamaskNotifications,
).toHaveBeenCalled();
});
it('passes marketing consent to notification services method', async () => {
const options = { hasMarketingConsent: true };
await enableNotifications(options);
expect(
Engine.context.NotificationServicesController.enableMetamaskNotifications,
).toHaveBeenCalledWith(options);
});
});
describe('helpers - hasNotificationPreferences()', () => {
it('returns true when AUS preferences exist', async () => {
jest.mocked(Engine.controllerMessenger.call).mockResolvedValue({
walletActivity: {
inAppNotificationsEnabled: true,
pushNotificationsEnabled: true,
accounts: [],
},
});
await expect(hasNotificationPreferences()).resolves.toBe(true);
expect(Engine.controllerMessenger.call).toHaveBeenCalledWith(
'AuthenticatedUserStorageService:getNotificationPreferences',
);
});
it('returns false when AUS preferences are missing', async () => {
jest.mocked(Engine.controllerMessenger.call).mockResolvedValue(null);
await expect(hasNotificationPreferences()).resolves.toBe(false);
});
});
describe('helpers - disableNotificationServices()', () => {
it('invoke notification services method', async () => {
await disableNotifications();
expect(
Engine.context.NotificationServicesController.disableNotificationServices,
).toHaveBeenCalled();
});
});
describe('helpers - checkAccountsPresence()', () => {
it('invoke notification services method', async () => {
const accounts = ['0xAddr1', '0xAddr2', '0xAddr3'];
await fetchAccountNotificationSettings(accounts);
expect(
Engine.context.NotificationServicesController.checkAccountsPresence,
).toHaveBeenCalledWith(accounts);
});
});
describe('helpers - disableAccounts()', () => {
it('invoke notification services method', async () => {
const accounts = ['0xAddr1', '0xAddr2', '0xAddr3'];
await disableAccounts(accounts);
expect(
Engine.context.NotificationServicesController.disableAccounts,
).toHaveBeenCalledWith(accounts);
});
});
describe('helpers - enableAccounts()', () => {
it('invoke notification services method', async () => {
const accounts = ['0xAddr1', '0xAddr2', '0xAddr3'];
await enableAccounts(accounts);
expect(
Engine.context.NotificationServicesController.enableAccounts,
).toHaveBeenCalledWith(accounts);
});
});
describe('helpers - setFeatureAnnouncementsEnabled()', () => {
it('invoke notification services method', async () => {
await toggleFeatureAnnouncements(true);
expect(
Engine.context.NotificationServicesController
.setFeatureAnnouncementsEnabled,
).toHaveBeenCalled();
});
});
describe('helpers - fetchAndUpdateMetamaskNotifications()', () => {
it('invoke notification services method', async () => {
await fetchNotifications();
expect(
Engine.context.NotificationServicesController
.fetchAndUpdateMetamaskNotifications,
).toHaveBeenCalled();
});
});
describe('helpers - markMetamaskNotificationsAsRead()', () => {
it('invoke notification services method', async () => {
const readNotifications = [
{ id: '1', isRead: true, type: TRIGGER_TYPES.ETH_SENT },
];
await markNotificationsAsRead(readNotifications);
expect(
Engine.context.NotificationServicesController
.markMetamaskNotificationsAsRead,
).toHaveBeenCalledWith(readNotifications);
});
});
describe('helpers - enablePushNotifications()', () => {
it('invoke notification services method', async () => {
await enablePushNotifications();
expect(
Engine.context.NotificationServicesController.enablePushNotifications,
).toHaveBeenCalled();
});
});
describe('helpers - disablePushNotifications()', () => {
it('invoke notification services method', async () => {
await disablePushNotifications();
expect(
Engine.context.NotificationServicesController.disablePushNotifications,
).toHaveBeenCalled();
});
});
describe('Content Preview Token', () => {
let setContentPreviewToken: typeof setContentPreviewTokenFn;
let getContentPreviewToken: typeof getContentPreviewTokenFn;
let subscribeToContentPreviewToken: typeof subscribeToContentPreviewTokenFn;
beforeEach(() => {
// Reset module per test as we are modifying a local variable in this module
jest.isolateModules(() => {
const HelpersModule = jest.requireActual('./index');
setContentPreviewToken = HelpersModule.setContentPreviewToken;
getContentPreviewToken = HelpersModule.getContentPreviewToken;
subscribeToContentPreviewToken =
HelpersModule.subscribeToContentPreviewToken;
});
});
it('sets the preview token when given a valid string', () => {
const token = 'preview-token-123';
setContentPreviewToken(token);
expect(getContentPreviewToken()).toBe(token);
});
it('does not set the preview token when given null', () => {
setContentPreviewToken('initial-token');
setContentPreviewToken(null);
expect(getContentPreviewToken()).toBe('initial-token');
});
it('does not set the preview token when given undefined', () => {
setContentPreviewToken('initial-token');
setContentPreviewToken(undefined);
expect(getContentPreviewToken()).toBe('initial-token');
});
it('initial get token will return undefined', () => {
expect(getContentPreviewToken()).toBeUndefined();
});
it('emits event when token is updated, and event is not received when unsubscribing', () => {
const mockCallback = jest.fn();
const unsubscribe = subscribeToContentPreviewToken(mockCallback);
// Call 1 - assert token update
setContentPreviewToken('token1');
expect(mockCallback).toHaveBeenCalledWith('token1');
// Call 2 - assert no token update
mockCallback.mockClear();
setContentPreviewToken(null);
expect(getContentPreviewToken()).toBe('token1'); // previous token not removed
expect(mockCallback).not.toHaveBeenCalled();
// Call 2 - assert listener removed
mockCallback.mockClear();
unsubscribe();
setContentPreviewToken('token2');
expect(getContentPreviewToken()).toBe('token2'); // new token added
expect(mockCallback).not.toHaveBeenCalled();
});
});