重构network库(mvp版本)搭建基础设施和核心接口
定义ITransport/ISerializer/INetworkMessage接口 NetworkIdentity组件 基础事件定义
This commit is contained in:
@@ -1,27 +1,65 @@
|
||||
/**
|
||||
* Jest测试环境设置 - 客户端
|
||||
*/
|
||||
|
||||
// 导入reflect-metadata以支持装饰器
|
||||
import 'reflect-metadata';
|
||||
|
||||
// Mock WebSocket for testing
|
||||
(global as any).WebSocket = class MockWebSocket {
|
||||
onopen: ((event: Event) => void) | null = null;
|
||||
onmessage: ((event: MessageEvent) => void) | null = null;
|
||||
onclose: ((event: CloseEvent) => void) | null = null;
|
||||
onerror: ((event: Event) => void) | null = null;
|
||||
|
||||
constructor(public url: string) {}
|
||||
|
||||
send(data: string | ArrayBuffer | Blob) {
|
||||
// Mock implementation
|
||||
}
|
||||
|
||||
close() {
|
||||
// Mock implementation
|
||||
}
|
||||
};
|
||||
// 模拟浏览器环境的WebSocket
|
||||
Object.defineProperty(global, 'WebSocket', {
|
||||
value: class MockWebSocket {
|
||||
static CONNECTING = 0;
|
||||
static OPEN = 1;
|
||||
static CLOSING = 2;
|
||||
static CLOSED = 3;
|
||||
|
||||
global.beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
readyState = MockWebSocket.CONNECTING;
|
||||
url: string;
|
||||
onopen: ((event: Event) => void) | null = null;
|
||||
onclose: ((event: CloseEvent) => void) | null = null;
|
||||
onmessage: ((event: MessageEvent) => void) | null = null;
|
||||
onerror: ((event: Event) => void) | null = null;
|
||||
|
||||
constructor(url: string) {
|
||||
this.url = url;
|
||||
// 模拟异步连接
|
||||
setTimeout(() => {
|
||||
this.readyState = MockWebSocket.OPEN;
|
||||
if (this.onopen) {
|
||||
this.onopen(new Event('open'));
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
send(data: string | ArrayBuffer) {
|
||||
// 模拟发送
|
||||
}
|
||||
|
||||
close() {
|
||||
this.readyState = MockWebSocket.CLOSED;
|
||||
if (this.onclose) {
|
||||
this.onclose(new CloseEvent('close'));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
global.afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
// 全局测试配置
|
||||
beforeAll(() => {
|
||||
// 设置测试环境
|
||||
process.env.NODE_ENV = 'test';
|
||||
process.env.NETWORK_ENV = 'client';
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
// 清理测试环境
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// 每个测试前的准备工作
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// 每个测试后的清理工作
|
||||
// 清理可能的网络连接、定时器等
|
||||
});
|
||||
Reference in New Issue
Block a user