qy-better-lib 0.0.1
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/.vscode/extensions.json +3 -0
- package/LICENSE +24 -0
- package/README.md +2 -0
- package/index.html +13 -0
- package/package.json +37 -0
- package/packages/core/DOCUMENTATION.md +2681 -0
- package/packages/core/LICENSE +24 -0
- package/packages/core/package.json +67 -0
- package/packages/core/types/index.d.ts +39 -0
- package/packages/core/types/json.d.ts +4 -0
- package/packages/core/types/vue.shim.d.ts +5 -0
- package/packages/core/vitest.config.ts +15 -0
- package/packages/core/yarn.lock +1233 -0
- package/packages/hooks/DOCUMENTATION.md +634 -0
- package/packages/hooks/LICENSE +24 -0
- package/packages/hooks/__tests__/use-chart/index.test.ts +287 -0
- package/packages/hooks/__tests__/use-emit/index.test.ts +248 -0
- package/packages/hooks/__tests__/use-fullscreen/index.test.ts +162 -0
- package/packages/hooks/__tests__/use-image/index.test.ts +230 -0
- package/packages/hooks/__tests__/use-layout-flow/index.test.ts +382 -0
- package/packages/hooks/__tests__/use-mqtt/index.test.ts +392 -0
- package/packages/hooks/__tests__/use-print/index.test.ts +378 -0
- package/packages/hooks/__tests__/use-watermark/index.test.ts +277 -0
- package/packages/hooks/__tests__/use-websocket/index.test.ts +402 -0
- package/packages/hooks/package.json +104 -0
- package/packages/hooks/types/index.d.ts +1 -0
- package/packages/hooks/types/json.d.ts +4 -0
- package/packages/hooks/types/vue.shim.d.ts +5 -0
- package/packages/hooks/yarn.lock +1832 -0
- package/packages/lowcode/core/package.json +60 -0
- package/packages/lowcode/core/types/index.d.ts +1 -0
- package/packages/lowcode/core/types/json.d.ts +4 -0
- package/packages/lowcode/core/types/vue.shim.d.ts +5 -0
- package/packages/lowcode/core/vite.dist.min.config.js +40 -0
- package/packages/lowcode/core/vite.dist.min.config.js.map +1 -0
- package/packages/lowcode/core/vite.lib.config.js +74 -0
- package/packages/lowcode/core/vite.lib.config.js.map +1 -0
- package/packages/lowcode/core/yarn.lock +1132 -0
- package/packages/lowcode/mobile/package.json +63 -0
- package/packages/lowcode/mobile/types/index.d.ts +1 -0
- package/packages/lowcode/mobile/types/json.d.ts +4 -0
- package/packages/lowcode/mobile/types/vue.shim.d.ts +77 -0
- package/packages/lowcode/mobile/vite.config.js +11 -0
- package/packages/lowcode/mobile/vite.config.js.map +1 -0
- package/packages/lowcode/mobile/vite.dist.min.config.js +71 -0
- package/packages/lowcode/mobile/vite.dist.min.config.js.map +1 -0
- package/packages/lowcode/mobile/vite.lib.config.js +137 -0
- package/packages/lowcode/mobile/vite.lib.config.js.map +1 -0
- package/packages/lowcode/mobile/yarn.lock +3888 -0
- package/packages/lowcode/pc/package.json +64 -0
- package/packages/lowcode/pc/vite.config.js +11 -0
- package/packages/lowcode/pc/vite.config.js.map +1 -0
- package/packages/lowcode/pc/vite.dist.min.config.js +71 -0
- package/packages/lowcode/pc/vite.dist.min.config.js.map +1 -0
- package/packages/lowcode/pc/vite.lib.config.js +137 -0
- package/packages/lowcode/pc/vite.lib.config.js.map +1 -0
- package/packages/lowcode/pc/yarn.lock +4026 -0
- package/packages/pc_components/DOCUMENTATION.md +566 -0
- package/packages/pc_components/LICENSE +24 -0
- package/packages/pc_components/lib/commDialog/index.d.ts +3 -0
- package/packages/pc_components/lib/commDialog/index.js +8 -0
- package/packages/pc_components/lib/commDialog/src/index.vue.d.ts +102 -0
- package/packages/pc_components/lib/commDialog/src/index.vue.js +43 -0
- package/packages/pc_components/lib/commDialog/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/commDialog/src/props.d.ts +49 -0
- package/packages/pc_components/lib/commDialog/src/props.js +48 -0
- package/packages/pc_components/lib/commDialog/src/type.d.ts +39 -0
- package/packages/pc_components/lib/commForm/index.d.ts +3 -0
- package/packages/pc_components/lib/commForm/index.js +8 -0
- package/packages/pc_components/lib/commForm/src/index.vue.d.ts +95 -0
- package/packages/pc_components/lib/commForm/src/index.vue.js +50 -0
- package/packages/pc_components/lib/commForm/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/commForm/src/props.d.ts +49 -0
- package/packages/pc_components/lib/commForm/src/props.js +47 -0
- package/packages/pc_components/lib/commForm/src/type.d.ts +8 -0
- package/packages/pc_components/lib/contextMenu/index.d.ts +5 -0
- package/packages/pc_components/lib/contextMenu/index.js +8 -0
- package/packages/pc_components/lib/contextMenu/src/index.vue.d.ts +91 -0
- package/packages/pc_components/lib/contextMenu/src/index.vue.js +153 -0
- package/packages/pc_components/lib/contextMenu/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/contextMenu/src/props.d.ts +32 -0
- package/packages/pc_components/lib/contextMenu/src/props.js +27 -0
- package/packages/pc_components/lib/contextMenu/src/type.d.ts +11 -0
- package/packages/pc_components/lib/describeView/index.d.ts +2 -0
- package/packages/pc_components/lib/describeView/index.js +7 -0
- package/packages/pc_components/lib/describeView/src/index.vue.d.ts +61 -0
- package/packages/pc_components/lib/describeView/src/index.vue.js +84 -0
- package/packages/pc_components/lib/describeView/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/describeView/src/props.d.ts +35 -0
- package/packages/pc_components/lib/describeView/src/props.js +33 -0
- package/packages/pc_components/lib/describeView/src/type.d.ts +10 -0
- package/packages/pc_components/lib/index.d.ts +18 -0
- package/packages/pc_components/lib/index.js +40 -0
- package/packages/pc_components/lib/simpleVirtualList/index.d.ts +2 -0
- package/packages/pc_components/lib/simpleVirtualList/index.js +7 -0
- package/packages/pc_components/lib/simpleVirtualList/src/index.vue.d.ts +28 -0
- package/packages/pc_components/lib/simpleVirtualList/src/index.vue.js +101 -0
- package/packages/pc_components/lib/simpleVirtualList/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/simpleVirtualList/src/type.d.ts +12 -0
- package/packages/pc_components/lib/styles/comm-form.scss +4 -0
- package/packages/pc_components/lib/styles/context-menu.scss +47 -0
- package/packages/pc_components/lib/styles/describe-view.scss +25 -0
- package/packages/pc_components/lib/styles/index.scss +6 -0
- package/packages/pc_components/lib/styles/simple-virtual-list.scss +25 -0
- package/packages/pc_components/lib/styles/table-extend.scss +100 -0
- package/packages/pc_components/lib/styles/text-button.scss +71 -0
- package/packages/pc_components/lib/tableExtend/index.d.ts +3 -0
- package/packages/pc_components/lib/tableExtend/index.js +7 -0
- package/packages/pc_components/lib/tableExtend/props.d.ts +130 -0
- package/packages/pc_components/lib/tableExtend/props.js +128 -0
- package/packages/pc_components/lib/tableExtend/src/colVisible.vue.d.ts +3 -0
- package/packages/pc_components/lib/tableExtend/src/colVisible.vue.js +128 -0
- package/packages/pc_components/lib/tableExtend/src/colVisible.vue2.js +4 -0
- package/packages/pc_components/lib/tableExtend/src/index.vue.d.ts +186 -0
- package/packages/pc_components/lib/tableExtend/src/index.vue.js +234 -0
- package/packages/pc_components/lib/tableExtend/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/tableExtend/src/valueCol.vue.d.ts +24 -0
- package/packages/pc_components/lib/tableExtend/src/valueCol.vue.js +29 -0
- package/packages/pc_components/lib/tableExtend/src/valueCol.vue2.js +4 -0
- package/packages/pc_components/lib/tableExtend/type.d.ts +165 -0
- package/packages/pc_components/lib/tableExtend/type.js +9 -0
- package/packages/pc_components/lib/tableExtend/useTable.d.ts +27 -0
- package/packages/pc_components/lib/tableExtend/useTable.js +115 -0
- package/packages/pc_components/lib/textButton/index.d.ts +2 -0
- package/packages/pc_components/lib/textButton/index.js +7 -0
- package/packages/pc_components/lib/textButton/src/index.vue.d.ts +42 -0
- package/packages/pc_components/lib/textButton/src/index.vue.js +44 -0
- package/packages/pc_components/lib/textButton/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/textButton/src/props.d.ts +23 -0
- package/packages/pc_components/lib/textButton/src/props.js +30 -0
- package/packages/pc_components/lib/textButton/src/type.d.ts +30 -0
- package/packages/pc_components/lib/utils/index.d.ts +77 -0
- package/packages/pc_components/lib/utils/index.js +124 -0
- package/packages/pc_components/package.json +73 -0
- package/packages/pc_components/types/index.d.ts +9 -0
- package/packages/pc_components/types/json.d.ts +4 -0
- package/packages/pc_components/types/vue.shim.d.ts +5 -0
- package/packages/pc_components/yarn.lock +1641 -0
- package/postcss.config.js +19 -0
- package/public/vite.svg +1 -0
- package/src/App.vue +37 -0
- package/src/api/index.ts +42 -0
- package/src/assets/vue.svg +1 -0
- package/src/document/core.md +2680 -0
- package/src/document/hooks.md +632 -0
- package/src/document/pc_components.md +566 -0
- package/src/locales/en.json +1 -0
- package/src/locales/zh-cn.json +1 -0
- package/src/main.ts +13 -0
- package/src/plugins/element-plus.ts +16 -0
- package/src/plugins/i18n.ts +17 -0
- package/src/plugins/index.ts +13 -0
- package/src/plugins/store.ts +12 -0
- package/src/router/index.ts +20 -0
- package/src/shared/chartType.ts +279 -0
- package/src/shared/const.ts +160 -0
- package/src/shared/lib.ts +129 -0
- package/src/shared/request.ts +255 -0
- package/src/shared/type.ts +48 -0
- package/src/shared/utils/date.ts +83 -0
- package/src/shared/utils/utils.ts +85 -0
- package/src/store/index.ts +22 -0
- package/src/store/modules/app.ts +17 -0
- package/src/store/modules/shared.ts +21 -0
- package/src/styles/index.scss +169 -0
- package/src/types/app.d.ts +23 -0
- package/src/types/echarts.d.ts +5 -0
- package/src/types/index.d.ts +1 -0
- package/src/types/json.d.ts +4 -0
- package/src/types/scss.d.ts +9 -0
- package/src/types/vue.shim.d.ts +5 -0
- package/src/views/docs/docs-content.vue +379 -0
- package/src/views/docs/docs-header.vue +130 -0
- package/src/views/docs/docs-sidebar.vue +76 -0
- package/src/views/docs/hooks.ts +600 -0
- package/src/views/docs/index.vue +30 -0
- package/src/views/docs/styles/docs-content.scss +106 -0
- package/src/views/docs/styles/docs-header.scss +20 -0
- package/src/views/docs/styles/docs-sidebar.scss +182 -0
- package/src/views/docs/styles/index.scss +14 -0
- package/src/views/docs/type.ts +101 -0
- package/src/views/index.vue +34 -0
- package/tsconfig.json +37 -0
- package/tsconfig.node.json +8 -0
- package/vite.config.ts +54 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { use_web_socket } from '../../src/use-websocket';
|
|
3
|
+
|
|
4
|
+
// 模拟WebSocket对象
|
|
5
|
+
class MockWebSocket {
|
|
6
|
+
readyState: number;
|
|
7
|
+
url: string;
|
|
8
|
+
onopen: ((event: Event) => void) | null;
|
|
9
|
+
onerror: ((event: Event) => void) | null;
|
|
10
|
+
onclose: ((event: CloseEvent) => void) | null;
|
|
11
|
+
onmessage: ((event: MessageEvent) => void) | null;
|
|
12
|
+
|
|
13
|
+
constructor(url: string) {
|
|
14
|
+
this.url = url;
|
|
15
|
+
this.readyState = WebSocket.CONNECTING;
|
|
16
|
+
this.onopen = null;
|
|
17
|
+
this.onerror = null;
|
|
18
|
+
this.onclose = null;
|
|
19
|
+
this.onmessage = null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
send(data: string) {
|
|
23
|
+
// 模拟发送消息
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
close() {
|
|
27
|
+
this.readyState = WebSocket.CLOSED;
|
|
28
|
+
if (this.onclose) {
|
|
29
|
+
const closeEvent = new CloseEvent('close', { code: 1000, reason: 'Normal closure' });
|
|
30
|
+
this.onclose(closeEvent);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 模拟连接成功
|
|
35
|
+
simulateOpen() {
|
|
36
|
+
this.readyState = WebSocket.OPEN;
|
|
37
|
+
if (this.onopen) {
|
|
38
|
+
this.onopen(new Event('open'));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 模拟连接错误
|
|
43
|
+
simulateError() {
|
|
44
|
+
if (this.onerror) {
|
|
45
|
+
this.onerror(new Event('error'));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 模拟接收消息
|
|
50
|
+
simulateMessage(data: any) {
|
|
51
|
+
if (this.onmessage) {
|
|
52
|
+
const messageEvent = new MessageEvent('message', { data });
|
|
53
|
+
this.onmessage(messageEvent);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 模拟WebSocket构造函数
|
|
59
|
+
const mockWebSocketClass = vi.fn().mockImplementation((url: string) => new MockWebSocket(url));
|
|
60
|
+
|
|
61
|
+
// 模拟全局对象
|
|
62
|
+
beforeEach(() => {
|
|
63
|
+
vi.clearAllMocks();
|
|
64
|
+
global.WebSocket = mockWebSocketClass as any;
|
|
65
|
+
// 模拟setTimeout和clearTimeout
|
|
66
|
+
vi.useFakeTimers();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
afterEach(() => {
|
|
70
|
+
vi.resetModules();
|
|
71
|
+
vi.useRealTimers();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('use_web_socket', () => {
|
|
75
|
+
it('should initialize with default values', () => {
|
|
76
|
+
const mockReceive = vi.fn();
|
|
77
|
+
const { ws, create, close, send_message, get_status, reconnect, start_heartbeat, stop_heartbeat } = use_web_socket({
|
|
78
|
+
server: 'ws://localhost:8080',
|
|
79
|
+
receive: mockReceive,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
expect(ws).toBeUndefined();
|
|
83
|
+
expect(typeof create).toBe('function');
|
|
84
|
+
expect(typeof close).toBe('function');
|
|
85
|
+
expect(typeof send_message).toBe('function');
|
|
86
|
+
expect(typeof get_status).toBe('function');
|
|
87
|
+
expect(typeof reconnect).toBe('function');
|
|
88
|
+
expect(typeof start_heartbeat).toBe('function');
|
|
89
|
+
expect(typeof stop_heartbeat).toBe('function');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should create and connect WebSocket successfully', () => {
|
|
93
|
+
const mockReceive = vi.fn();
|
|
94
|
+
const mockOnOpen = vi.fn();
|
|
95
|
+
|
|
96
|
+
const { create, get_status } = use_web_socket({
|
|
97
|
+
server: 'ws://localhost:8080',
|
|
98
|
+
receive: mockReceive,
|
|
99
|
+
on_open: mockOnOpen,
|
|
100
|
+
auto_connect: false,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// 创建连接
|
|
104
|
+
create();
|
|
105
|
+
|
|
106
|
+
expect(mockWebSocketClass).toHaveBeenCalledWith('ws://localhost:8080');
|
|
107
|
+
|
|
108
|
+
// 模拟连接成功
|
|
109
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
110
|
+
if (wsInstance) {
|
|
111
|
+
wsInstance.simulateOpen();
|
|
112
|
+
expect(mockOnOpen).toHaveBeenCalled();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should handle WebSocket error', () => {
|
|
117
|
+
const mockReceive = vi.fn();
|
|
118
|
+
const mockOnError = vi.fn();
|
|
119
|
+
|
|
120
|
+
const { create } = use_web_socket({
|
|
121
|
+
server: 'ws://localhost:8080',
|
|
122
|
+
receive: mockReceive,
|
|
123
|
+
on_error: mockOnError,
|
|
124
|
+
auto_connect: false,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// 创建连接
|
|
128
|
+
create();
|
|
129
|
+
|
|
130
|
+
// 模拟连接错误
|
|
131
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
132
|
+
if (wsInstance) {
|
|
133
|
+
wsInstance.simulateError();
|
|
134
|
+
expect(mockOnError).toHaveBeenCalled();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should handle WebSocket close', () => {
|
|
139
|
+
const mockReceive = vi.fn();
|
|
140
|
+
const mockOnClose = vi.fn();
|
|
141
|
+
|
|
142
|
+
const { create } = use_web_socket({
|
|
143
|
+
server: 'ws://localhost:8080',
|
|
144
|
+
receive: mockReceive,
|
|
145
|
+
on_close: mockOnClose,
|
|
146
|
+
auto_connect: false,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// 创建连接
|
|
150
|
+
create();
|
|
151
|
+
|
|
152
|
+
// 模拟连接成功
|
|
153
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
154
|
+
if (wsInstance) {
|
|
155
|
+
wsInstance.simulateOpen();
|
|
156
|
+
|
|
157
|
+
// 关闭连接
|
|
158
|
+
wsInstance.close();
|
|
159
|
+
|
|
160
|
+
expect(mockOnClose).toHaveBeenCalled();
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should send message successfully', () => {
|
|
165
|
+
const mockReceive = vi.fn();
|
|
166
|
+
|
|
167
|
+
const { create, send_message } = use_web_socket({
|
|
168
|
+
server: 'ws://localhost:8080',
|
|
169
|
+
receive: mockReceive,
|
|
170
|
+
auto_connect: false,
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// 创建连接
|
|
174
|
+
create();
|
|
175
|
+
|
|
176
|
+
// 模拟连接成功
|
|
177
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
178
|
+
if (wsInstance) {
|
|
179
|
+
wsInstance.simulateOpen();
|
|
180
|
+
|
|
181
|
+
// 发送消息
|
|
182
|
+
const sendSpy = vi.spyOn(wsInstance, 'send');
|
|
183
|
+
const result = send_message('test message');
|
|
184
|
+
|
|
185
|
+
expect(result).toBe(true);
|
|
186
|
+
expect(sendSpy).toHaveBeenCalledWith('test message');
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('should handle send message when not connected', () => {
|
|
191
|
+
const mockReceive = vi.fn();
|
|
192
|
+
|
|
193
|
+
const { send_message } = use_web_socket({
|
|
194
|
+
server: 'ws://localhost:8080',
|
|
195
|
+
receive: mockReceive,
|
|
196
|
+
auto_connect: false,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// 未连接时发送消息
|
|
200
|
+
const result = send_message('test message');
|
|
201
|
+
|
|
202
|
+
expect(result).toBe(false);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('should receive message', () => {
|
|
206
|
+
const mockReceive = vi.fn();
|
|
207
|
+
|
|
208
|
+
const { create } = use_web_socket({
|
|
209
|
+
server: 'ws://localhost:8080',
|
|
210
|
+
receive: mockReceive,
|
|
211
|
+
auto_connect: false,
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// 创建连接
|
|
215
|
+
create();
|
|
216
|
+
|
|
217
|
+
// 模拟连接成功
|
|
218
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
219
|
+
if (wsInstance) {
|
|
220
|
+
wsInstance.simulateOpen();
|
|
221
|
+
|
|
222
|
+
// 模拟接收消息
|
|
223
|
+
wsInstance.simulateMessage('test message');
|
|
224
|
+
|
|
225
|
+
expect(mockReceive).toHaveBeenCalled();
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should reconnect automatically', () => {
|
|
230
|
+
const mockReceive = vi.fn();
|
|
231
|
+
|
|
232
|
+
const { create } = use_web_socket({
|
|
233
|
+
server: 'ws://localhost:8080',
|
|
234
|
+
receive: mockReceive,
|
|
235
|
+
auto_reconnect: true,
|
|
236
|
+
reconnect_interval: 1000,
|
|
237
|
+
auto_connect: false,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// 创建连接
|
|
241
|
+
create();
|
|
242
|
+
expect(mockWebSocketClass).toHaveBeenCalledTimes(1);
|
|
243
|
+
|
|
244
|
+
// 模拟连接成功
|
|
245
|
+
const wsInstance1 = (global.WebSocket as any).mock.instances[0];
|
|
246
|
+
if (wsInstance1) {
|
|
247
|
+
wsInstance1.simulateOpen();
|
|
248
|
+
|
|
249
|
+
// 关闭连接(非干净关闭)
|
|
250
|
+
wsInstance1.close();
|
|
251
|
+
|
|
252
|
+
// 快进时间,触发重连
|
|
253
|
+
vi.advanceTimersByTime(1000);
|
|
254
|
+
|
|
255
|
+
expect(mockWebSocketClass).toHaveBeenCalledTimes(2);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('should start and stop heartbeat', () => {
|
|
260
|
+
const mockReceive = vi.fn();
|
|
261
|
+
|
|
262
|
+
const { create, start_heartbeat, stop_heartbeat } = use_web_socket({
|
|
263
|
+
server: 'ws://localhost:8080',
|
|
264
|
+
receive: mockReceive,
|
|
265
|
+
heartbeat_interval: 5000,
|
|
266
|
+
auto_connect: false,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
// 创建连接
|
|
270
|
+
create();
|
|
271
|
+
|
|
272
|
+
// 模拟连接成功
|
|
273
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
274
|
+
if (wsInstance) {
|
|
275
|
+
wsInstance.simulateOpen();
|
|
276
|
+
|
|
277
|
+
// 启动心跳
|
|
278
|
+
start_heartbeat();
|
|
279
|
+
|
|
280
|
+
// 快进时间,触发心跳
|
|
281
|
+
const sendSpy = vi.spyOn(wsInstance, 'send');
|
|
282
|
+
vi.advanceTimersByTime(5000);
|
|
283
|
+
|
|
284
|
+
expect(sendSpy).toHaveBeenCalledWith('ping');
|
|
285
|
+
|
|
286
|
+
// 停止心跳
|
|
287
|
+
stop_heartbeat();
|
|
288
|
+
|
|
289
|
+
// 快进时间,不应再触发心跳
|
|
290
|
+
sendSpy.mockClear();
|
|
291
|
+
vi.advanceTimersByTime(5000);
|
|
292
|
+
|
|
293
|
+
expect(sendSpy).not.toHaveBeenCalled();
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('should get correct connection status', () => {
|
|
298
|
+
const mockReceive = vi.fn();
|
|
299
|
+
|
|
300
|
+
const { create, get_status, close } = use_web_socket({
|
|
301
|
+
server: 'ws://localhost:8080',
|
|
302
|
+
receive: mockReceive,
|
|
303
|
+
auto_connect: false,
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
// 初始状态
|
|
307
|
+
expect(get_status()).toBe('CLOSED');
|
|
308
|
+
|
|
309
|
+
// 创建连接
|
|
310
|
+
create();
|
|
311
|
+
|
|
312
|
+
// 模拟连接成功
|
|
313
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
314
|
+
if (wsInstance) {
|
|
315
|
+
wsInstance.simulateOpen();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// 关闭连接
|
|
319
|
+
close();
|
|
320
|
+
expect(get_status()).toBe('CLOSED');
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('should handle connection with auto_connect', () => {
|
|
324
|
+
const mockReceive = vi.fn();
|
|
325
|
+
|
|
326
|
+
use_web_socket({
|
|
327
|
+
server: 'ws://localhost:8080',
|
|
328
|
+
receive: mockReceive,
|
|
329
|
+
auto_connect: true,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
expect(mockWebSocketClass).toHaveBeenCalledWith('ws://localhost:8080');
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('should handle connection error with auto_reconnect', () => {
|
|
336
|
+
const mockReceive = vi.fn();
|
|
337
|
+
|
|
338
|
+
use_web_socket({
|
|
339
|
+
server: 'ws://localhost:8080',
|
|
340
|
+
receive: mockReceive,
|
|
341
|
+
auto_reconnect: true,
|
|
342
|
+
reconnect_interval: 1000,
|
|
343
|
+
auto_connect: true,
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
expect(mockWebSocketClass).toHaveBeenCalledTimes(1);
|
|
347
|
+
|
|
348
|
+
// 模拟连接错误
|
|
349
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
350
|
+
if (wsInstance) {
|
|
351
|
+
wsInstance.simulateError();
|
|
352
|
+
|
|
353
|
+
// 快进时间,触发重连
|
|
354
|
+
vi.advanceTimersByTime(1000);
|
|
355
|
+
|
|
356
|
+
expect(mockWebSocketClass).toHaveBeenCalledTimes(2);
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it('should handle message queue when not connected', () => {
|
|
361
|
+
const mockReceive = vi.fn();
|
|
362
|
+
|
|
363
|
+
const { send_message, create } = use_web_socket({
|
|
364
|
+
server: 'ws://localhost:8080',
|
|
365
|
+
receive: mockReceive,
|
|
366
|
+
auto_connect: false,
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
// 未连接时发送消息
|
|
370
|
+
send_message('message 1');
|
|
371
|
+
send_message('message 2');
|
|
372
|
+
|
|
373
|
+
// 创建连接
|
|
374
|
+
create();
|
|
375
|
+
|
|
376
|
+
// 模拟连接成功
|
|
377
|
+
const wsInstance = (global.WebSocket as any).mock.instances[0];
|
|
378
|
+
if (wsInstance) {
|
|
379
|
+
const sendSpy = vi.spyOn(wsInstance, 'send');
|
|
380
|
+
wsInstance.simulateOpen();
|
|
381
|
+
|
|
382
|
+
// 验证队列中的消息是否被发送
|
|
383
|
+
expect(sendSpy).toHaveBeenCalledTimes(2);
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it('should limit message queue size', () => {
|
|
388
|
+
const mockReceive = vi.fn();
|
|
389
|
+
|
|
390
|
+
const { send_message } = use_web_socket({
|
|
391
|
+
server: 'ws://localhost:8080',
|
|
392
|
+
receive: mockReceive,
|
|
393
|
+
auto_connect: false,
|
|
394
|
+
max_message_queue_size: 2,
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
// 未连接时发送多条消息
|
|
398
|
+
send_message('message 1');
|
|
399
|
+
send_message('message 2');
|
|
400
|
+
send_message('message 3'); // 这应该替换最旧的消息
|
|
401
|
+
});
|
|
402
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@qy_better_lib/hooks",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.2.9",
|
|
5
|
+
"description": "qy better lib hooks",
|
|
6
|
+
"author": "luhuiming",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build:lib": "vite build --config vite.lib.config.ts",
|
|
10
|
+
"build:dist-min": "vite build --config vite.dist.min.config.ts",
|
|
11
|
+
"build": "yarn build:lib && yarn build:dist-min",
|
|
12
|
+
"test": "vitest run",
|
|
13
|
+
"test:watch": "vitest"
|
|
14
|
+
},
|
|
15
|
+
"main": "lib/index.mjs",
|
|
16
|
+
"module": "lib/index.mjs",
|
|
17
|
+
"types": "lib/index.d.ts",
|
|
18
|
+
"typesVersions": {
|
|
19
|
+
"*": {
|
|
20
|
+
"lib/index.mjs": [
|
|
21
|
+
"lib/index.d.ts"
|
|
22
|
+
],
|
|
23
|
+
"@qy-better-lib/hooks": [
|
|
24
|
+
"lib/index.d.ts"
|
|
25
|
+
],
|
|
26
|
+
"*": [
|
|
27
|
+
"*",
|
|
28
|
+
"dist/*",
|
|
29
|
+
"lib/*",
|
|
30
|
+
"lib/*.d.ts",
|
|
31
|
+
"lib/*/index.d.ts"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": "./lib/index.mjs",
|
|
38
|
+
"types": "./lib/index.d.ts"
|
|
39
|
+
},
|
|
40
|
+
"./*": {
|
|
41
|
+
"import": "./lib/*/index.mjs",
|
|
42
|
+
"types": "./lib/*/index.d.ts"
|
|
43
|
+
},
|
|
44
|
+
"./use-chart": {
|
|
45
|
+
"import": "./lib/use-chart/index.mjs",
|
|
46
|
+
"types": "./lib/use-chart/index.d.ts"
|
|
47
|
+
},
|
|
48
|
+
"./use-layout-flow": {
|
|
49
|
+
"import": "./lib/use-layout-flow/index.mjs",
|
|
50
|
+
"types": "./lib/use-layout-flow/index.d.ts"
|
|
51
|
+
},
|
|
52
|
+
"./use-mqtt": {
|
|
53
|
+
"import": "./lib/use-mqtt/index.mjs",
|
|
54
|
+
"types": "./lib/use-mqtt/index.d.ts"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@antv/x6": "^2.18.1",
|
|
59
|
+
"@qy_better_lib/core": "^0.2.8",
|
|
60
|
+
"@types/dagre": "^0.7.53",
|
|
61
|
+
"@types/node": "^24.5.2",
|
|
62
|
+
"@vitejs/plugin-vue": "^6.0.1",
|
|
63
|
+
"dagre": "^0.8.5",
|
|
64
|
+
"echarts": "5.6.0",
|
|
65
|
+
"mitt": "^3.0.1",
|
|
66
|
+
"mqtt": "^5.14.1",
|
|
67
|
+
"vite": "^7.1.7",
|
|
68
|
+
"vite-plugin-dts": "^4.5.4",
|
|
69
|
+
"vitest": "^4.0.18",
|
|
70
|
+
"vue": "^3.5.22"
|
|
71
|
+
},
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"@antv/x6": "^2.18.1",
|
|
74
|
+
"@qy_better_lib/core": "^0.2.0",
|
|
75
|
+
"dagre": "^0.8.5",
|
|
76
|
+
"echarts": "5.6.0",
|
|
77
|
+
"mitt": "^3.0.1",
|
|
78
|
+
"mqtt": "^5.14.1",
|
|
79
|
+
"vue": "^3.5.22"
|
|
80
|
+
},
|
|
81
|
+
"peerDependenciesMeta": {
|
|
82
|
+
"@antv/x6": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"dagre": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"echarts": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"mitt": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"mqtt": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
97
|
+
"@qy_better_lib/core": {
|
|
98
|
+
"optional": false
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
"dependencies": {
|
|
102
|
+
"@qy_better_lib/hooks": "^0.2.6"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare type $TODO = any
|