@tencentcloud/web-push 1.0.3 → 1.0.5
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 +69 -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/sw.js +1 -0
- package/dist/sw.js +0 -1
- 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
|
@@ -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
|
-
});
|