@qy_better_lib/hooks 0.1.9 → 0.2.0

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.
Files changed (82) hide show
  1. package/DOCUMENTATION.md +704 -0
  2. package/USAGE.md +444 -0
  3. package/__tests__/use-chart/index.test.ts +287 -0
  4. package/__tests__/use-emit/index.test.ts +248 -0
  5. package/__tests__/use-fullscreen/index.test.ts +162 -0
  6. package/__tests__/use-image/index.test.ts +230 -0
  7. package/__tests__/use-layout-flow/index.test.ts +382 -0
  8. package/__tests__/use-mqtt/index.test.ts +392 -0
  9. package/__tests__/use-print/index.test.ts +378 -0
  10. package/__tests__/use-watermark/index.test.ts +277 -0
  11. package/__tests__/use-websocket/index.test.ts +402 -0
  12. package/dist/hooks.min.js +76 -0
  13. package/lib/_virtual/dayjs.min.js +2 -0
  14. package/lib/_virtual/dayjs.min2.js +4 -0
  15. package/lib/index.d.ts +5 -2
  16. package/lib/index.js +20 -31
  17. package/lib/node_modules/dayjs/dayjs.min.js +286 -0
  18. package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/directives/click_outside.js +19 -0
  19. package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/dom.js +25 -0
  20. package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/echarts.js +166 -0
  21. package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/file.js +55 -0
  22. package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/is.js +8 -0
  23. package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/object.js +83 -0
  24. package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/random.js +11 -0
  25. package/lib/use-chart/config.d.ts +2 -3
  26. package/lib/use-chart/config.js +80 -0
  27. package/lib/use-chart/index.d.ts +5 -13
  28. package/lib/use-chart/index.js +199 -0
  29. package/lib/use-chart/type.d.ts +92 -4
  30. package/lib/use-emit/extend.d.ts +2 -1
  31. package/lib/use-emit/extend.js +34 -15
  32. package/lib/use-emit/index.d.ts +2 -13
  33. package/lib/use-emit/index.js +22 -17
  34. package/lib/use-emit/type.d.ts +16 -0
  35. package/lib/use-fullscreen/index.d.ts +23 -0
  36. package/lib/use-fullscreen/index.js +53 -0
  37. package/lib/use-image/index.d.ts +18 -52
  38. package/lib/use-image/index.js +189 -67
  39. package/lib/use-image/type.d.ts +8 -10
  40. package/lib/use-image/type.js +7 -6
  41. package/lib/use-layout-flow/index.d.ts +14 -40
  42. package/lib/use-layout-flow/index.js +286 -0
  43. package/lib/use-layout-flow/type.d.ts +46 -0
  44. package/lib/use-mqtt/index.d.ts +9 -18
  45. package/lib/use-mqtt/index.js +179 -0
  46. package/lib/use-mqtt/type.d.ts +78 -0
  47. package/lib/use-print/index.d.ts +5 -9
  48. package/lib/use-print/index.js +274 -40
  49. package/lib/use-print/type.d.ts +58 -0
  50. package/lib/use-watermark/index.d.ts +7 -0
  51. package/lib/use-watermark/index.js +134 -0
  52. package/lib/use-watermark/type.d.ts +55 -0
  53. package/lib/use-websocket/index.d.ts +6 -13
  54. package/lib/use-websocket/index.js +192 -39
  55. package/lib/use-websocket/type.d.ts +54 -0
  56. package/package.json +9 -3
  57. package/dist/@qy_better_lib/hooks.min.js +0 -15
  58. package/lib/use-chart/utils.d.ts +0 -7
  59. package/lib/use-file/index.d.ts +0 -14
  60. package/lib/use-file/index.js +0 -26
  61. package/lib/use-image/canvastoDataURL.d.ts +0 -11
  62. package/lib/use-image/canvastoDataURL.js +0 -7
  63. package/lib/use-image/canvastoFile.d.ts +0 -11
  64. package/lib/use-image/canvastoFile.js +0 -9
  65. package/lib/use-image/dataURLtoFile.d.ts +0 -10
  66. package/lib/use-image/dataURLtoFile.js +0 -16
  67. package/lib/use-image/dataURLtoImage.d.ts +0 -7
  68. package/lib/use-image/dataURLtoImage.js +0 -9
  69. package/lib/use-image/downloadFile.d.ts +0 -7
  70. package/lib/use-image/downloadFile.js +0 -9
  71. package/lib/use-image/filetoDataURL.d.ts +0 -7
  72. package/lib/use-image/filetoDataURL.js +0 -9
  73. package/lib/use-image/imagetoCanvas.d.ts +0 -26
  74. package/lib/use-image/imagetoCanvas.js +0 -41
  75. package/lib/use-image/urltoBlob.d.ts +0 -8
  76. package/lib/use-image/urltoBlob.js +0 -6
  77. package/lib/use-image/urltoImage.d.ts +0 -7
  78. package/lib/use-image/urltoImage.js +0 -13
  79. package/lib/use-utils/index.d.ts +0 -1
  80. package/lib/use-utils/use-fullscreen.d.ts +0 -9
  81. package/lib/use-waterMark/index.d.ts +0 -17
  82. package/lib/use-waterMark/index.js +0 -29
@@ -0,0 +1,392 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { use_mqtt } from '../../src/use-mqtt';
3
+
4
+ // 模拟mqtt库
5
+ const mockMqttClient: any = {
6
+ on: vi.fn(),
7
+ end: vi.fn(),
8
+ subscribe: vi.fn(),
9
+ unsubscribe: vi.fn(),
10
+ publish: vi.fn(),
11
+ };
12
+
13
+ const mockMqttModule: any = {
14
+ connect: vi.fn().mockReturnValue(mockMqttClient),
15
+ };
16
+
17
+ // 模拟动态导入
18
+ vi.mock('mqtt', () => {
19
+ return {
20
+ __esModule: true,
21
+ default: mockMqttModule,
22
+ };
23
+ });
24
+
25
+ // 模拟setTimeout
26
+ vi.useFakeTimers();
27
+
28
+ beforeEach(() => {
29
+ vi.clearAllMocks();
30
+ });
31
+
32
+ afterEach(() => {
33
+ vi.resetModules();
34
+ vi.useRealTimers();
35
+ });
36
+
37
+ describe('use_mqtt', () => {
38
+ it('should initialize with default values', () => {
39
+ const { status, connect, close, subscribe, unsubscribe, publish } = use_mqtt('mqtt://localhost:1883');
40
+
41
+ expect(status).toBe('disconnected');
42
+ expect(typeof connect).toBe('function');
43
+ expect(typeof close).toBe('function');
44
+ expect(typeof subscribe).toBe('function');
45
+ expect(typeof unsubscribe).toBe('function');
46
+ expect(typeof publish).toBe('function');
47
+ });
48
+
49
+ it('should handle empty url', () => {
50
+ const { status } = use_mqtt('');
51
+
52
+ expect(status).toBe('error');
53
+ });
54
+
55
+ it('should connect successfully', async () => {
56
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
57
+
58
+ const mockSuccess = vi.fn();
59
+ const mockError = vi.fn();
60
+ const mockReceive = vi.fn();
61
+
62
+ // 开始连接
63
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
64
+
65
+ expect(mqttInstance.status).toBe('connecting');
66
+
67
+ // 模拟连接成功 - 直接设置状态为connected
68
+ // 由于connect方法是异步的,我们需要等待一下
69
+ await new Promise(resolve => setTimeout(resolve, 0));
70
+
71
+ // 直接检查success回调是否被调用
72
+ expect(mockSuccess).toHaveBeenCalled();
73
+ });
74
+
75
+ it('should handle connection error', async () => {
76
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
77
+
78
+ const mockSuccess = vi.fn();
79
+ const mockError = vi.fn();
80
+ const mockReceive = vi.fn();
81
+
82
+ // 开始连接
83
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
84
+
85
+ expect(mqttInstance.status).toBe('connecting');
86
+
87
+ // 模拟连接错误
88
+ const errorCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'error')?.[1];
89
+ if (errorCallback) {
90
+ errorCallback(new Error('Connection error'));
91
+ }
92
+
93
+ await connectPromise;
94
+
95
+ expect(mqttInstance.status).toBe('error');
96
+ expect(mockError).toHaveBeenCalled();
97
+ });
98
+
99
+ it('should handle connection close', async () => {
100
+ const mqttInstance = use_mqtt('mqtt://localhost:1883', { reconnectPeriod: 1000 });
101
+
102
+ const mockSuccess = vi.fn();
103
+ const mockError = vi.fn();
104
+ const mockReceive = vi.fn();
105
+
106
+ // 开始连接
107
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
108
+
109
+ // 模拟连接成功
110
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
111
+ if (connectCallback) {
112
+ connectCallback();
113
+ }
114
+
115
+ await connectPromise;
116
+
117
+ expect(mqttInstance.status).toBe('connected');
118
+
119
+ // 模拟连接关闭
120
+ const closeCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'close')?.[1];
121
+ if (closeCallback) {
122
+ closeCallback();
123
+ }
124
+
125
+ expect(mqttInstance.status).toBe('disconnected');
126
+
127
+ // 验证重连定时器
128
+ vi.runAllTimers();
129
+ });
130
+
131
+ it('should handle reconnect event', async () => {
132
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
133
+
134
+ const mockSuccess = vi.fn();
135
+ const mockError = vi.fn();
136
+ const mockReceive = vi.fn();
137
+
138
+ // 开始连接
139
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
140
+
141
+ // 模拟连接成功
142
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
143
+ if (connectCallback) {
144
+ connectCallback();
145
+ }
146
+
147
+ await connectPromise;
148
+
149
+ expect(mqttInstance.status).toBe('connected');
150
+
151
+ // 模拟重连事件
152
+ const reconnectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'reconnect')?.[1];
153
+ if (reconnectCallback) {
154
+ reconnectCallback();
155
+ }
156
+
157
+ expect(mqttInstance.status).toBe('connecting');
158
+ });
159
+
160
+ it('should close connection', async () => {
161
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
162
+
163
+ const mockSuccess = vi.fn();
164
+ const mockError = vi.fn();
165
+ const mockReceive = vi.fn();
166
+ const mockCloseCallback = vi.fn();
167
+
168
+ // 开始连接
169
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
170
+
171
+ // 模拟连接成功
172
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
173
+ if (connectCallback) {
174
+ connectCallback();
175
+ }
176
+
177
+ await connectPromise;
178
+
179
+ expect(mqttInstance.status).toBe('connected');
180
+
181
+ // 关闭连接
182
+ mqttInstance.close(mockCloseCallback);
183
+
184
+ expect(mockMqttClient.end).toHaveBeenCalledWith(false, undefined, expect.any(Function));
185
+ expect(mqttInstance.status).toBe('disconnected');
186
+ });
187
+
188
+ it('should subscribe to topic', async () => {
189
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
190
+
191
+ const mockSuccess = vi.fn();
192
+ const mockError = vi.fn();
193
+ const mockReceive = vi.fn();
194
+ const mockSubscribeCallback = vi.fn();
195
+
196
+ // 开始连接
197
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
198
+
199
+ // 模拟连接成功
200
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
201
+ if (connectCallback) {
202
+ connectCallback();
203
+ }
204
+
205
+ await connectPromise;
206
+
207
+ // 订阅主题
208
+ mqttInstance.subscribe('test/topic', {}, mockSubscribeCallback);
209
+
210
+ expect(mockMqttClient.subscribe).toHaveBeenCalledWith('test/topic', {}, expect.any(Function));
211
+ });
212
+
213
+ it('should unsubscribe from topic', async () => {
214
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
215
+
216
+ const mockSuccess = vi.fn();
217
+ const mockError = vi.fn();
218
+ const mockReceive = vi.fn();
219
+ const mockUnsubscribeCallback = vi.fn();
220
+
221
+ // 开始连接
222
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
223
+
224
+ // 模拟连接成功
225
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
226
+ if (connectCallback) {
227
+ connectCallback();
228
+ }
229
+
230
+ await connectPromise;
231
+
232
+ // 取消订阅
233
+ mqttInstance.unsubscribe('test/topic', mockUnsubscribeCallback);
234
+
235
+ expect(mockMqttClient.unsubscribe).toHaveBeenCalledWith('test/topic', expect.any(Function));
236
+ });
237
+
238
+ it('should publish message', async () => {
239
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
240
+
241
+ const mockSuccess = vi.fn();
242
+ const mockError = vi.fn();
243
+ const mockReceive = vi.fn();
244
+ const mockPublishCallback = vi.fn();
245
+
246
+ // 开始连接
247
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
248
+
249
+ // 模拟连接成功
250
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
251
+ if (connectCallback) {
252
+ connectCallback();
253
+ }
254
+
255
+ await connectPromise;
256
+
257
+ // 发布消息
258
+ mqttInstance.publish('test/topic', 'test message', {}, mockPublishCallback);
259
+
260
+ expect(mockMqttClient.publish).toHaveBeenCalledWith('test/topic', 'test message', {}, expect.any(Function));
261
+ });
262
+
263
+ it('should handle subscribe without connection', () => {
264
+ const { subscribe } = use_mqtt('mqtt://localhost:1883');
265
+
266
+ const mockSubscribeCallback = vi.fn();
267
+
268
+ subscribe('test/topic', {}, mockSubscribeCallback);
269
+
270
+ expect(mockSubscribeCallback).toHaveBeenCalledWith(new Error('客户端未连接'), []);
271
+ });
272
+
273
+ it('should handle unsubscribe without connection', () => {
274
+ const { unsubscribe } = use_mqtt('mqtt://localhost:1883');
275
+
276
+ const mockUnsubscribeCallback = vi.fn();
277
+
278
+ unsubscribe('test/topic', mockUnsubscribeCallback);
279
+
280
+ expect(mockUnsubscribeCallback).toHaveBeenCalledWith(new Error('客户端未连接'));
281
+ });
282
+
283
+ it('should handle publish without connection', () => {
284
+ const { publish } = use_mqtt('mqtt://localhost:1883');
285
+
286
+ const mockPublishCallback = vi.fn();
287
+
288
+ publish('test/topic', 'test message', {}, mockPublishCallback);
289
+
290
+ expect(mockPublishCallback).toHaveBeenCalledWith(new Error('客户端未连接'));
291
+ });
292
+
293
+ it('should handle empty topic in subscribe', async () => {
294
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
295
+
296
+ const mockSuccess = vi.fn();
297
+ const mockError = vi.fn();
298
+ const mockReceive = vi.fn();
299
+ const mockSubscribeCallback = vi.fn();
300
+
301
+ // 开始连接
302
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
303
+
304
+ // 模拟连接成功
305
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
306
+ if (connectCallback) {
307
+ connectCallback();
308
+ }
309
+
310
+ await connectPromise;
311
+
312
+ // 订阅空主题
313
+ mqttInstance.subscribe('', {}, mockSubscribeCallback);
314
+
315
+ expect(mockSubscribeCallback).toHaveBeenCalledWith(new Error('订阅主题不能为空'), []);
316
+ });
317
+
318
+ it('should handle empty topic in unsubscribe', async () => {
319
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
320
+
321
+ const mockSuccess = vi.fn();
322
+ const mockError = vi.fn();
323
+ const mockReceive = vi.fn();
324
+ const mockUnsubscribeCallback = vi.fn();
325
+
326
+ // 开始连接
327
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
328
+
329
+ // 模拟连接成功
330
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
331
+ if (connectCallback) {
332
+ connectCallback();
333
+ }
334
+
335
+ await connectPromise;
336
+
337
+ // 取消订阅空主题
338
+ mqttInstance.unsubscribe('', mockUnsubscribeCallback);
339
+
340
+ expect(mockUnsubscribeCallback).toHaveBeenCalledWith(new Error('取消订阅主题不能为空'));
341
+ });
342
+
343
+ it('should handle empty topic in publish', async () => {
344
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
345
+
346
+ const mockSuccess = vi.fn();
347
+ const mockError = vi.fn();
348
+ const mockReceive = vi.fn();
349
+ const mockPublishCallback = vi.fn();
350
+
351
+ // 开始连接
352
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
353
+
354
+ // 模拟连接成功
355
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
356
+ if (connectCallback) {
357
+ connectCallback();
358
+ }
359
+
360
+ await connectPromise;
361
+
362
+ // 发布空主题
363
+ mqttInstance.publish('', 'test message', {}, mockPublishCallback);
364
+
365
+ expect(mockPublishCallback).toHaveBeenCalledWith(new Error('发布主题不能为空'));
366
+ });
367
+
368
+ it('should handle empty message in publish', async () => {
369
+ const mqttInstance = use_mqtt('mqtt://localhost:1883');
370
+
371
+ const mockSuccess = vi.fn();
372
+ const mockError = vi.fn();
373
+ const mockReceive = vi.fn();
374
+ const mockPublishCallback = vi.fn();
375
+
376
+ // 开始连接
377
+ const connectPromise = mqttInstance.connect({ success: mockSuccess, error: mockError, receive: mockReceive });
378
+
379
+ // 模拟连接成功
380
+ const connectCallback = mockMqttClient.on.mock.calls.find((call: any[]) => call[0] === 'connect')?.[1];
381
+ if (connectCallback) {
382
+ connectCallback();
383
+ }
384
+
385
+ await connectPromise;
386
+
387
+ // 发布空消息
388
+ mqttInstance.publish('test/topic', '', {}, mockPublishCallback);
389
+
390
+ expect(mockPublishCallback).toHaveBeenCalledWith(new Error('发布消息不能为空'));
391
+ });
392
+ });