@tencentcloud/web-push 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +63 -0
- package/README.md +100 -92
- package/{dist/index.esm.js → index.esm.js} +1 -1
- package/{dist/index.umd.js → index.umd.js} +1 -1
- package/package.json +9 -47
- package/src/__tests__/index.test.ts +0 -120
- package/src/__tests__/integration.test.ts +0 -285
- package/src/__tests__/setup.ts +0 -210
- package/src/__tests__/types.test.ts +0 -303
- package/src/__tests__/web-push-sdk.test.ts +0 -257
- package/src/components/message-popup.ts +0 -1007
- package/src/core/event-emitter.ts +0 -61
- package/src/core/service-worker-manager.ts +0 -614
- package/src/core/web-push-sdk.ts +0 -690
- package/src/debug/GenerateTestUserSig.js +0 -37
- package/src/debug/index.d.ts +0 -6
- package/src/debug/index.js +0 -1
- package/src/debug/lib-generate-test-usersig-es.min.js +0 -2
- package/src/index.ts +0 -9
- package/src/service-worker/sw.ts +0 -494
- package/src/types/index.ts +0 -2
- package/src/types/inner.ts +0 -44
- package/src/types/outer.ts +0 -142
- package/src/utils/browser-support.ts +0 -412
- package/src/utils/logger.ts +0 -66
- package/src/utils/storage.ts +0 -51
- package/src/utils/validator.ts +0 -267
- /package/{dist/index.d.ts → index.d.ts} +0 -0
- /package/{dist/src → src}/components/message-popup.d.ts +0 -0
- /package/{dist/src → src}/core/event-emitter.d.ts +0 -0
- /package/{dist/src → src}/core/service-worker-manager.d.ts +0 -0
- /package/{dist/src → src}/core/web-push-sdk.d.ts +0 -0
- /package/{dist/src → src}/index.d.ts +0 -0
- /package/{dist/src → src}/service-worker/sw.d.ts +0 -0
- /package/{dist/src → src}/types/index.d.ts +0 -0
- /package/{dist/src → src}/types/inner.d.ts +0 -0
- /package/{dist/src → src}/types/outer.d.ts +0 -0
- /package/{dist/src → src}/utils/browser-support.d.ts +0 -0
- /package/{dist/src → src}/utils/logger.d.ts +0 -0
- /package/{dist/src → src}/utils/storage.d.ts +0 -0
- /package/{dist/src → src}/utils/validator.d.ts +0 -0
- /package/{dist/sw.js → sw.js} +0 -0
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 类型定义测试
|
|
3
|
-
*/
|
|
4
|
-
import {
|
|
5
|
-
EVENT,
|
|
6
|
-
RegisterPushOptions,
|
|
7
|
-
NotificationMessage,
|
|
8
|
-
NotificationClickData,
|
|
9
|
-
StatisticsData,
|
|
10
|
-
Message,
|
|
11
|
-
MessageReceivedResult,
|
|
12
|
-
MessageRevokedResult,
|
|
13
|
-
MessageNotificationClickedResult,
|
|
14
|
-
EventResult,
|
|
15
|
-
WebPushSDK,
|
|
16
|
-
ServiceWorkerMessage,
|
|
17
|
-
SubscriptionInfo,
|
|
18
|
-
} from '../types';
|
|
19
|
-
|
|
20
|
-
describe('类型定义测试', () => {
|
|
21
|
-
describe('EVENT 枚举测试', () => {
|
|
22
|
-
it('应该包含所有必需的事件类型', () => {
|
|
23
|
-
expect(EVENT.MESSAGE_RECEIVED).toBe('message_received');
|
|
24
|
-
expect(EVENT.MESSAGE_REVOKED).toBe('message_revoked');
|
|
25
|
-
expect(EVENT.NOTIFICATION_CLICKED).toBe('notification_clicked');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('EVENT 应该是只读的', () => {
|
|
29
|
-
// TypeScript 编译时会检查这个,运行时我们验证值的正确性
|
|
30
|
-
expect(Object.keys(EVENT)).toHaveLength(3);
|
|
31
|
-
expect(Object.values(EVENT)).toEqual([
|
|
32
|
-
'message_received',
|
|
33
|
-
'message_revoked',
|
|
34
|
-
'notification_clicked',
|
|
35
|
-
]);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
describe('Event 类型测试', () => {
|
|
40
|
-
it('应该接受有效的事件类型', () => {
|
|
41
|
-
const validEvents: EVENT[] = [
|
|
42
|
-
EVENT.MESSAGE_RECEIVED,
|
|
43
|
-
EVENT.MESSAGE_REVOKED,
|
|
44
|
-
EVENT.NOTIFICATION_CLICKED,
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
validEvents.forEach((event) => {
|
|
48
|
-
expect(typeof event).toBe('string');
|
|
49
|
-
expect(Object.values(EVENT)).toContain(event);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('RegisterPushOptions 接口测试', () => {
|
|
55
|
-
it('应该包含必需的属性', () => {
|
|
56
|
-
const options: RegisterPushOptions = {
|
|
57
|
-
SDKAppID: 123456,
|
|
58
|
-
appKey: 'test-app-key',
|
|
59
|
-
userID: 'test-user-id',
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
expect(typeof options.SDKAppID).toBe('number');
|
|
63
|
-
expect(typeof options.appKey).toBe('string');
|
|
64
|
-
expect(typeof options.userID).toBe('string');
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('应该支持可选的 serviceWorkerPath', () => {
|
|
68
|
-
const optionsWithSW: RegisterPushOptions = {
|
|
69
|
-
SDKAppID: 123456,
|
|
70
|
-
appKey: 'test-app-key',
|
|
71
|
-
userID: 'test-user-id',
|
|
72
|
-
serviceWorkerPath: '/custom-sw.js',
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
expect(typeof optionsWithSW.serviceWorkerPath).toBe('string');
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
describe('NotificationMessage 接口测试', () => {
|
|
80
|
-
it('应该包含必需的属性', () => {
|
|
81
|
-
const message: NotificationMessage = {
|
|
82
|
-
messageID: 'msg-123',
|
|
83
|
-
title: 'Test Title',
|
|
84
|
-
body: 'Test Body',
|
|
85
|
-
timestamp: Date.now(),
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
expect(typeof message.messageID).toBe('string');
|
|
89
|
-
expect(typeof message.title).toBe('string');
|
|
90
|
-
expect(typeof message.body).toBe('string');
|
|
91
|
-
expect(typeof message.timestamp).toBe('number');
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('应该支持可选属性', () => {
|
|
95
|
-
const messageWithOptional: NotificationMessage = {
|
|
96
|
-
messageID: 'msg-123',
|
|
97
|
-
title: 'Test Title',
|
|
98
|
-
body: 'Test Body',
|
|
99
|
-
timestamp: Date.now(),
|
|
100
|
-
icon: '/icon.png',
|
|
101
|
-
tag: 'test-tag',
|
|
102
|
-
data: { custom: 'data' },
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
expect(typeof messageWithOptional.icon).toBe('string');
|
|
106
|
-
expect(typeof messageWithOptional.tag).toBe('string');
|
|
107
|
-
expect(typeof messageWithOptional.data).toBe('object');
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('NotificationClickData 接口测试', () => {
|
|
112
|
-
it('应该包含通知对象', () => {
|
|
113
|
-
const message: NotificationMessage = {
|
|
114
|
-
messageID: 'msg-123',
|
|
115
|
-
title: 'Test Title',
|
|
116
|
-
body: 'Test Body',
|
|
117
|
-
timestamp: Date.now(),
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const clickData: NotificationClickData = {
|
|
121
|
-
notification: message,
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
expect(clickData.notification).toBe(message);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('应该支持可选的 action', () => {
|
|
128
|
-
const message: NotificationMessage = {
|
|
129
|
-
messageID: 'msg-123',
|
|
130
|
-
title: 'Test Title',
|
|
131
|
-
body: 'Test Body',
|
|
132
|
-
timestamp: Date.now(),
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const clickDataWithAction: NotificationClickData = {
|
|
136
|
-
notification: message,
|
|
137
|
-
action: 'reply',
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
expect(typeof clickDataWithAction.action).toBe('string');
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
describe('StatisticsData 接口测试', () => {
|
|
145
|
-
it('应该包含统计数据的所有属性', () => {
|
|
146
|
-
const statsData: StatisticsData = {
|
|
147
|
-
messageID: 'msg-123',
|
|
148
|
-
type: 'reach',
|
|
149
|
-
timestamp: Date.now(),
|
|
150
|
-
SDKAppID: 123456,
|
|
151
|
-
registrationID: 'reg-123',
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
expect(typeof statsData.messageID).toBe('string');
|
|
155
|
-
expect(['reach', 'click']).toContain(statsData.type);
|
|
156
|
-
expect(typeof statsData.timestamp).toBe('number');
|
|
157
|
-
expect(typeof statsData.SDKAppID).toBe('number');
|
|
158
|
-
expect(typeof statsData.registrationID).toBe('string');
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
describe('Message 接口测试', () => {
|
|
163
|
-
it('应该包含消息的所有必需属性', () => {
|
|
164
|
-
const message: Message = {
|
|
165
|
-
ID: 'msg-123',
|
|
166
|
-
type: 'TIMTextElem',
|
|
167
|
-
payload: { text: 'Hello' },
|
|
168
|
-
conversationID: 'conv-123',
|
|
169
|
-
conversationType: 'C2C',
|
|
170
|
-
to: 'user-456',
|
|
171
|
-
from: 'user-123',
|
|
172
|
-
flow: 'in',
|
|
173
|
-
time: Date.now(),
|
|
174
|
-
status: 'success',
|
|
175
|
-
isRevoked: false,
|
|
176
|
-
nick: 'Test User',
|
|
177
|
-
avatar: '/avatar.jpg',
|
|
178
|
-
isPeerRead: false,
|
|
179
|
-
nameCard: 'Test Card',
|
|
180
|
-
atUserList: [],
|
|
181
|
-
cloudCustomData: '{}',
|
|
182
|
-
isDeleted: false,
|
|
183
|
-
isModified: false,
|
|
184
|
-
needReadReceipt: false,
|
|
185
|
-
readReceiptInfo: {},
|
|
186
|
-
isBroadcastMessage: false,
|
|
187
|
-
isSupportExtension: true,
|
|
188
|
-
revoker: '',
|
|
189
|
-
sequence: 1,
|
|
190
|
-
progress: 100,
|
|
191
|
-
revokerInfo: {
|
|
192
|
-
userID: '',
|
|
193
|
-
nick: '',
|
|
194
|
-
avatar: '',
|
|
195
|
-
},
|
|
196
|
-
revokeReason: '',
|
|
197
|
-
hasRiskContent: false,
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
expect(typeof message.ID).toBe('string');
|
|
201
|
-
expect([
|
|
202
|
-
'TIMTextElem',
|
|
203
|
-
'TIMImageElem',
|
|
204
|
-
'TIMSoundElem',
|
|
205
|
-
'TIMVideoFileElem',
|
|
206
|
-
'TIMFileElem',
|
|
207
|
-
'TIMCustomElem',
|
|
208
|
-
'TIMRelayElem',
|
|
209
|
-
'TIMLocationElem',
|
|
210
|
-
'TIMFaceElem',
|
|
211
|
-
]).toContain(message.type);
|
|
212
|
-
expect(['C2C', 'GROUP']).toContain(message.conversationType);
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
describe('WebPushSDK 接口测试', () => {
|
|
217
|
-
it('应该定义所有必需的方法', () => {
|
|
218
|
-
// 这个测试主要是类型检查,确保接口定义正确
|
|
219
|
-
const mockSDK: WebPushSDK = {
|
|
220
|
-
registerPush: jest.fn(),
|
|
221
|
-
unRegisterPush: jest.fn(),
|
|
222
|
-
addPushListener: jest.fn(),
|
|
223
|
-
removePushListener: jest.fn(),
|
|
224
|
-
EVENT: EVENT,
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
expect(typeof mockSDK.registerPush).toBe('function');
|
|
228
|
-
expect(typeof mockSDK.unRegisterPush).toBe('function');
|
|
229
|
-
expect(typeof mockSDK.addPushListener).toBe('function');
|
|
230
|
-
expect(typeof mockSDK.removePushListener).toBe('function');
|
|
231
|
-
expect(mockSDK.EVENT).toBe(EVENT);
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
describe('ServiceWorkerMessage 接口测试', () => {
|
|
236
|
-
it('应该支持所有消息类型', () => {
|
|
237
|
-
const messages: ServiceWorkerMessage[] = [
|
|
238
|
-
{ type: 'MESSAGE_RECEIVED', data: {} },
|
|
239
|
-
{ type: 'NOTIFICATION_CLICKED', data: {} },
|
|
240
|
-
{ type: 'MESSAGE_REVOKED', data: {} },
|
|
241
|
-
];
|
|
242
|
-
|
|
243
|
-
messages.forEach((msg) => {
|
|
244
|
-
expect([
|
|
245
|
-
'MESSAGE_RECEIVED',
|
|
246
|
-
'NOTIFICATION_CLICKED',
|
|
247
|
-
'MESSAGE_REVOKED',
|
|
248
|
-
]).toContain(msg.type);
|
|
249
|
-
expect(msg.data).toBeDefined();
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
describe('SubscriptionInfo 接口测试', () => {
|
|
255
|
-
it('应该包含推送订阅的所有信息', () => {
|
|
256
|
-
const mockSubscription = {
|
|
257
|
-
endpoint: 'https://example.com/push',
|
|
258
|
-
keys: {
|
|
259
|
-
auth: 'mock-auth',
|
|
260
|
-
p256dh: 'mock-p256dh',
|
|
261
|
-
},
|
|
262
|
-
} as PushSubscription;
|
|
263
|
-
|
|
264
|
-
const subscriptionInfo: SubscriptionInfo = {
|
|
265
|
-
subscription: mockSubscription,
|
|
266
|
-
token: 'mock-token',
|
|
267
|
-
auth: 'mock-auth',
|
|
268
|
-
p256dh: 'mock-p256dh',
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
expect(subscriptionInfo.subscription).toBe(mockSubscription);
|
|
272
|
-
expect(typeof subscriptionInfo.token).toBe('string');
|
|
273
|
-
expect(typeof subscriptionInfo.auth).toBe('string');
|
|
274
|
-
expect(typeof subscriptionInfo.p256dh).toBe('string');
|
|
275
|
-
});
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
describe('EventResult 联合类型测试', () => {
|
|
279
|
-
it('应该支持所有事件结果类型', () => {
|
|
280
|
-
const messageReceived: EventResult = {
|
|
281
|
-
data: {
|
|
282
|
-
message: {} as Message,
|
|
283
|
-
} as MessageReceivedResult,
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
const messageRevoked: EventResult = {
|
|
287
|
-
data: {
|
|
288
|
-
messageID: 'msg-123',
|
|
289
|
-
} as MessageRevokedResult,
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
const notificationClicked: EventResult = {
|
|
293
|
-
data: {
|
|
294
|
-
ext: 'extension-data',
|
|
295
|
-
} as MessageNotificationClickedResult,
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
expect(messageReceived.data).toBeDefined();
|
|
299
|
-
expect(messageRevoked.data).toBeDefined();
|
|
300
|
-
expect(notificationClicked.data).toBeDefined();
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
});
|
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WebPushSDK 简化单元测试
|
|
3
|
-
*/
|
|
4
|
-
import { WebPushSDK } from '../core/web-push-sdk';
|
|
5
|
-
import { EVENT, RegisterPushOptions } from '../types';
|
|
6
|
-
import { ValidationError } from '../utils/validator';
|
|
7
|
-
|
|
8
|
-
describe('WebPushSDK 对外接口测试', () => {
|
|
9
|
-
let webPushSDK: WebPushSDK;
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
// 重置单例实例
|
|
13
|
-
(WebPushSDK as unknown as { instance: undefined }).instance = undefined;
|
|
14
|
-
webPushSDK = WebPushSDK.getInstance();
|
|
15
|
-
|
|
16
|
-
// 清除所有 mock
|
|
17
|
-
jest.clearAllMocks();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('单例模式测试', () => {
|
|
21
|
-
it('应该返回同一个实例', () => {
|
|
22
|
-
const instance1 = WebPushSDK.getInstance();
|
|
23
|
-
const instance2 = WebPushSDK.getInstance();
|
|
24
|
-
|
|
25
|
-
expect(instance1).toBe(instance2);
|
|
26
|
-
expect(instance1).toBeInstanceOf(WebPushSDK);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('EVENT 常量测试', () => {
|
|
31
|
-
it('应该暴露正确的事件常量', () => {
|
|
32
|
-
expect(webPushSDK.EVENT).toEqual(EVENT);
|
|
33
|
-
expect(webPushSDK.EVENT.MESSAGE_RECEIVED).toBe('message_received');
|
|
34
|
-
expect(webPushSDK.EVENT.MESSAGE_REVOKED).toBe('message_revoked');
|
|
35
|
-
expect(webPushSDK.EVENT.NOTIFICATION_CLICKED).toBe(
|
|
36
|
-
'notification_clicked'
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
describe('registerPush() 接口测试', () => {
|
|
42
|
-
const validOptions: RegisterPushOptions = {
|
|
43
|
-
SDKAppID: 123456,
|
|
44
|
-
appKey: 'test-app-key',
|
|
45
|
-
userID: 'test-user-id',
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
it('应该成功注册推送服务', async () => {
|
|
49
|
-
const registrationID = await webPushSDK.registerPush(validOptions);
|
|
50
|
-
|
|
51
|
-
expect(registrationID).toBe(validOptions.userID);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('应该验证必需参数', async () => {
|
|
55
|
-
await expect(webPushSDK.registerPush()).rejects.toThrow(ValidationError);
|
|
56
|
-
|
|
57
|
-
await expect(
|
|
58
|
-
webPushSDK.registerPush({} as RegisterPushOptions)
|
|
59
|
-
).rejects.toThrow(ValidationError);
|
|
60
|
-
|
|
61
|
-
await expect(
|
|
62
|
-
webPushSDK.registerPush({
|
|
63
|
-
SDKAppID: 123456,
|
|
64
|
-
appKey: '',
|
|
65
|
-
userID: 'test-user',
|
|
66
|
-
})
|
|
67
|
-
).rejects.toThrow(ValidationError);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('应该处理重复注册的情况', async () => {
|
|
71
|
-
// 第一次注册
|
|
72
|
-
await webPushSDK.registerPush(validOptions);
|
|
73
|
-
|
|
74
|
-
// 第二次注册应该先注销再重新注册
|
|
75
|
-
const registrationID = await webPushSDK.registerPush({
|
|
76
|
-
...validOptions,
|
|
77
|
-
userID: 'new-user-id',
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
expect(registrationID).toBe('new-user-id');
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe('unRegisterPush() 接口测试', () => {
|
|
85
|
-
beforeEach(async () => {
|
|
86
|
-
// 先注册一个推送服务
|
|
87
|
-
await webPushSDK.registerPush({
|
|
88
|
-
SDKAppID: 123456,
|
|
89
|
-
appKey: 'test-app-key',
|
|
90
|
-
userID: 'test-user-id',
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('应该成功反注册推送服务', async () => {
|
|
95
|
-
const result = await webPushSDK.unRegisterPush();
|
|
96
|
-
|
|
97
|
-
expect(result).toBe(true);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('应该处理未注册状态的反注册', async () => {
|
|
101
|
-
// 先反注册一次
|
|
102
|
-
await webPushSDK.unRegisterPush();
|
|
103
|
-
|
|
104
|
-
// 再次反注册应该仍然返回 true
|
|
105
|
-
const result = await webPushSDK.unRegisterPush();
|
|
106
|
-
expect(result).toBe(true);
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
describe('addPushListener() 接口测试', () => {
|
|
111
|
-
const mockListener = jest.fn();
|
|
112
|
-
|
|
113
|
-
beforeEach(() => {
|
|
114
|
-
mockListener.mockClear();
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('应该成功添加推送监听器', () => {
|
|
118
|
-
const listenerId = webPushSDK.addPushListener(
|
|
119
|
-
EVENT.MESSAGE_RECEIVED,
|
|
120
|
-
mockListener
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
expect(typeof listenerId).toBe('string');
|
|
124
|
-
expect(listenerId.length).toBeGreaterThan(0);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('应该验证事件类型参数', () => {
|
|
128
|
-
expect(() => {
|
|
129
|
-
webPushSDK.addPushListener('invalid_event' as EVENT, mockListener);
|
|
130
|
-
}).toThrow(ValidationError);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('应该验证监听器参数', () => {
|
|
134
|
-
expect(() => {
|
|
135
|
-
webPushSDK.addPushListener(
|
|
136
|
-
EVENT.MESSAGE_RECEIVED,
|
|
137
|
-
null as unknown as () => void
|
|
138
|
-
);
|
|
139
|
-
}).toThrow(ValidationError);
|
|
140
|
-
|
|
141
|
-
expect(() => {
|
|
142
|
-
webPushSDK.addPushListener(
|
|
143
|
-
EVENT.MESSAGE_RECEIVED,
|
|
144
|
-
'not_a_function' as unknown as () => void
|
|
145
|
-
);
|
|
146
|
-
}).toThrow(ValidationError);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('应该支持所有事件类型', () => {
|
|
150
|
-
const listener1 = webPushSDK.addPushListener(
|
|
151
|
-
EVENT.MESSAGE_RECEIVED,
|
|
152
|
-
mockListener
|
|
153
|
-
);
|
|
154
|
-
const listener2 = webPushSDK.addPushListener(
|
|
155
|
-
EVENT.MESSAGE_REVOKED,
|
|
156
|
-
mockListener
|
|
157
|
-
);
|
|
158
|
-
const listener3 = webPushSDK.addPushListener(
|
|
159
|
-
EVENT.NOTIFICATION_CLICKED,
|
|
160
|
-
mockListener
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
expect(listener1).toBeTruthy();
|
|
164
|
-
expect(listener2).toBeTruthy();
|
|
165
|
-
expect(listener3).toBeTruthy();
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it('应该支持同一事件添加多个监听器', () => {
|
|
169
|
-
const listener1 = jest.fn();
|
|
170
|
-
const listener2 = jest.fn();
|
|
171
|
-
|
|
172
|
-
const id1 = webPushSDK.addPushListener(EVENT.MESSAGE_RECEIVED, listener1);
|
|
173
|
-
const id2 = webPushSDK.addPushListener(EVENT.MESSAGE_RECEIVED, listener2);
|
|
174
|
-
|
|
175
|
-
expect(id1).not.toBe(id2);
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
describe('removePushListener() 接口测试', () => {
|
|
180
|
-
const mockListener = jest.fn();
|
|
181
|
-
|
|
182
|
-
beforeEach(() => {
|
|
183
|
-
mockListener.mockClear();
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
it('应该成功移除推送监听器', () => {
|
|
187
|
-
webPushSDK.addPushListener(EVENT.MESSAGE_RECEIVED, mockListener);
|
|
188
|
-
|
|
189
|
-
const result = webPushSDK.removePushListener(
|
|
190
|
-
EVENT.MESSAGE_RECEIVED,
|
|
191
|
-
mockListener
|
|
192
|
-
);
|
|
193
|
-
expect(result).toBe(true);
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
it('应该验证事件类型参数', () => {
|
|
197
|
-
expect(() => {
|
|
198
|
-
webPushSDK.removePushListener('invalid_event' as EVENT, mockListener);
|
|
199
|
-
}).toThrow(ValidationError);
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it('应该验证监听器参数', () => {
|
|
203
|
-
expect(() => {
|
|
204
|
-
webPushSDK.removePushListener(
|
|
205
|
-
EVENT.MESSAGE_RECEIVED,
|
|
206
|
-
null as unknown as () => void
|
|
207
|
-
);
|
|
208
|
-
}).toThrow(ValidationError);
|
|
209
|
-
|
|
210
|
-
expect(() => {
|
|
211
|
-
webPushSDK.removePushListener(
|
|
212
|
-
EVENT.MESSAGE_RECEIVED,
|
|
213
|
-
'not_a_function' as unknown as () => void
|
|
214
|
-
);
|
|
215
|
-
}).toThrow(ValidationError);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it('应该处理移除不存在的监听器', () => {
|
|
219
|
-
const nonExistentListener = jest.fn();
|
|
220
|
-
|
|
221
|
-
const result = webPushSDK.removePushListener(
|
|
222
|
-
EVENT.MESSAGE_RECEIVED,
|
|
223
|
-
nonExistentListener
|
|
224
|
-
);
|
|
225
|
-
expect(result).toBe(false);
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
it('应该只移除指定的监听器', () => {
|
|
229
|
-
const listener1 = jest.fn();
|
|
230
|
-
const listener2 = jest.fn();
|
|
231
|
-
|
|
232
|
-
webPushSDK.addPushListener(EVENT.MESSAGE_RECEIVED, listener1);
|
|
233
|
-
webPushSDK.addPushListener(EVENT.MESSAGE_RECEIVED, listener2);
|
|
234
|
-
|
|
235
|
-
const result1 = webPushSDK.removePushListener(
|
|
236
|
-
EVENT.MESSAGE_RECEIVED,
|
|
237
|
-
listener1
|
|
238
|
-
);
|
|
239
|
-
expect(result1).toBe(true);
|
|
240
|
-
|
|
241
|
-
// listener2 应该仍然存在
|
|
242
|
-
const result2 = webPushSDK.removePushListener(
|
|
243
|
-
EVENT.MESSAGE_RECEIVED,
|
|
244
|
-
listener2
|
|
245
|
-
);
|
|
246
|
-
expect(result2).toBe(true);
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
describe('错误处理测试', () => {
|
|
251
|
-
it('应该正确处理 ValidationError', async () => {
|
|
252
|
-
await expect(
|
|
253
|
-
webPushSDK.registerPush({} as RegisterPushOptions)
|
|
254
|
-
).rejects.toBeInstanceOf(ValidationError);
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
});
|