-
-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathservices.test.ts
More file actions
201 lines (173 loc) · 5.71 KB
/
services.test.ts
File metadata and controls
201 lines (173 loc) · 5.71 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
import log from 'loglevel';
import type nock from 'nock';
import {
activatePushNotifications,
deactivatePushNotifications,
updateLinksAPI,
} from './services';
import { mockEndpointUpdatePushNotificationLinks } from '../__fixtures__/mockServices';
import type { PushNotificationEnv } from '../types/firebase';
// Testing util to clean up verbose logs when testing errors
const mockErrorLog = (): jest.SpyInstance =>
jest.spyOn(log, 'error').mockImplementation(jest.fn());
const MOCK_REG_TOKEN = 'REG_TOKEN';
const MOCK_NEW_REG_TOKEN = 'NEW_REG_TOKEN';
const MOCK_ADDRESSES = ['0x123', '0x456', '0x789'];
const MOCK_JWT = 'MOCK_JWT';
describe('NotificationServicesPushController Services', () => {
describe('updateLinksAPI', () => {
const act = async (): Promise<boolean> =>
await updateLinksAPI({
bearerToken: MOCK_JWT,
addresses: MOCK_ADDRESSES,
regToken: {
token: MOCK_NEW_REG_TOKEN,
platform: 'extension',
locale: 'en',
},
});
it('should return true if links are successfully updated', async () => {
const mockAPI = mockEndpointUpdatePushNotificationLinks();
const result = await act();
expect(mockAPI.isDone()).toBe(true);
expect(result).toBe(true);
});
it('should return false if the links API update fails', async () => {
const mockAPI = mockEndpointUpdatePushNotificationLinks({ status: 500 });
const result = await act();
expect(mockAPI.isDone()).toBe(true);
expect(result).toBe(false);
});
it('should return false if an error is thrown', async () => {
jest
.spyOn(global, 'fetch')
.mockRejectedValue(new Error('MOCK FAIL FETCH'));
const result = await act();
expect(result).toBe(false);
});
});
describe('activatePushNotifications', () => {
const arrangeMocks = (override?: {
mockPut?: { status: number };
}): {
params: {
bearerToken: string;
addresses: string[];
createRegToken: jest.Mock;
regToken: {
platform: 'extension';
locale: string;
};
env: PushNotificationEnv;
};
mobileParams: {
bearerToken: string;
addresses: string[];
createRegToken: jest.Mock;
regToken: {
platform: 'mobile';
locale: string;
};
env: PushNotificationEnv;
};
apis: {
mockPut: nock.Scope;
};
} => {
const params = {
bearerToken: MOCK_JWT,
addresses: MOCK_ADDRESSES,
createRegToken: jest.fn().mockResolvedValue(MOCK_NEW_REG_TOKEN),
regToken: {
platform: 'extension' as const,
locale: 'en',
},
env: {} as PushNotificationEnv,
};
const mobileParams = {
...params,
regToken: {
...params.regToken,
platform: 'mobile' as const,
},
};
return {
params,
mobileParams,
apis: {
mockPut: mockEndpointUpdatePushNotificationLinks(override?.mockPut),
},
};
};
it('should successfully call APIs and add new registration token', async () => {
const { params, apis } = arrangeMocks();
const result = await activatePushNotifications(params);
expect(params.createRegToken).toHaveBeenCalled();
expect(apis.mockPut.isDone()).toBe(true);
expect(result).toBe(MOCK_NEW_REG_TOKEN);
});
it('should return null if unable to create new registration token', async () => {
const { params, apis } = arrangeMocks();
params.createRegToken.mockRejectedValue(new Error('MOCK ERROR'));
const result = await activatePushNotifications(params);
expect(params.createRegToken).toHaveBeenCalled();
expect(apis.mockPut.isDone()).toBe(false);
expect(result).toBeNull();
});
it('should handle oldToken parameter when provided', async () => {
const { params, apis } = arrangeMocks();
const paramsWithOldToken = {
...params,
regToken: {
...params.regToken,
oldToken: 'OLD_TOKEN',
},
};
const result = await activatePushNotifications(paramsWithOldToken);
expect(params.createRegToken).toHaveBeenCalled();
expect(apis.mockPut.isDone()).toBe(true);
expect(result).toBe(MOCK_NEW_REG_TOKEN);
});
});
describe('deactivatePushNotifications', () => {
const arrangeMocks = (): {
params: {
regToken: string;
deleteRegToken: jest.Mock;
env: PushNotificationEnv;
};
} => {
const params = {
regToken: MOCK_REG_TOKEN,
deleteRegToken: jest.fn().mockResolvedValue(true),
env: {} as PushNotificationEnv,
};
return {
params,
};
};
it('should successfully delete the registration token', async () => {
const { params } = arrangeMocks();
const result = await deactivatePushNotifications(params);
expect(params.deleteRegToken).toHaveBeenCalled();
expect(result).toBe(true);
});
it('should return early when there is no registration token to delete', async () => {
const { params } = arrangeMocks();
mockErrorLog();
const result = await deactivatePushNotifications({
...params,
regToken: '',
});
expect(params.deleteRegToken).not.toHaveBeenCalled();
expect(result).toBe(true);
});
it('should return false when unable to delete the existing reg token', async () => {
const { params } = arrangeMocks();
params.deleteRegToken.mockResolvedValue(false);
const result = await deactivatePushNotifications(params);
expect(params.deleteRegToken).toHaveBeenCalled();
expect(result).toBe(false);
});
});
});