Skip to content

Commit

Permalink
add: test-case
Browse files Browse the repository at this point in the history
  • Loading branch information
weixiangmeng521 committed Dec 20, 2023
1 parent e82dcb0 commit dc466e9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 66 deletions.
27 changes: 27 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { TeleportSingleton } = require("../lib/index");


(function(){


// Arrange
const teleport = TeleportSingleton.getInstance();
const eventNames = ['event1', 'event2', 'event3'];
const expectedData = ['Data 1', 'Data 2', 'Data 3'];
let receivedData = [];

// Act
setTimeout(() => {
teleport.multiReceive(eventNames, (...data) => {
receivedData = data;
console.log(data);
});
}, 1000);

eventNames.forEach((eventName, index) => {
teleport.emit(eventName, expectedData[index]);
});


console.log(expectedData, receivedData);
}())
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mwx47/teleport",
"version": "1.1.5",
"version": "1.1.6",
"description": "A library for managing and communicating events in your application through a singleton pattern. This pattern ensures that there is a single instance of the event manager, making it easy to coordinate and handle events across different parts of your codebase.",
"main": "./src/index.js",
"keywords": [
Expand Down
39 changes: 17 additions & 22 deletions src/__tests__/teleportSingleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,21 @@ describe('TeleportSingleton', () => {


// Test case for handling multiple events with a common handler
// TODO: async
// it('should handle multiple events with a common handler by async', () => {
// // Arrange
// const teleport = TeleportSingleton.getInstance();
// const eventNames = ['event1', 'event2', 'event3'];
// const expectedData = ['Data 1', 'Data 2', 'Data 3'];
// let receivedData: any[] = [];

// // Act
// setTimeout(() => {
// teleport.multiReceive(eventNames, (...data) => {
// receivedData = data;
// });
// }, 1000);

// eventNames.forEach((eventName, index) => {
// teleport.emit(eventName, expectedData[index]);
// });

// // Assert
// expect(receivedData).toEqual(expectedData);
// });
it('should handle multiple events with a common handler by async', () => {
// Arrange
const teleport = TeleportSingleton.getInstance();
const eventNames = ['event1', 'event2', 'event3'];
const expectedData = ['Data 1', 'Data 2', 'Data 3'];

// Act
setTimeout(() => {
teleport.multiReceive(eventNames, (...data) => {
expect(data).toEqual(expectedData);
});
}, 10);

eventNames.forEach((eventName, index) => {
teleport.emit(eventName, expectedData[index]);
});
});
});
84 changes: 41 additions & 43 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "lib", // 输出目录
"sourceMap": false, // 是否生成sourceMap
"target": "ES2015", // 编译目标
"module": "CommonJS", // 模块类型
"moduleResolution": "node",
"allowJs": false, // 是否编辑js文件
"strict": true, // 严格模式
"noUnusedLocals": true, // 未使用变量报错
"experimentalDecorators": true, // 启动装饰器
"resolveJsonModule": true, // 加载json
"esModuleInterop": true,
// "removeComments": false, // 删除注释


"declaration": true, // 生成定义文件
"declarationMap": false, // 生成定义sourceMap
"compilerOptions": {
"baseUrl": ".",
"outDir": "lib", // 输出目录
"sourceMap": false, // 是否生成sourceMap
"target": "ES2015", // 编译目标
"module": "CommonJS", // 模块类型
"moduleResolution": "node",
"allowJs": false, // 是否编辑js文件
"strict": true, // 严格模式
"noUnusedLocals": true, // 未使用变量报错
"experimentalDecorators": true, // 启动装饰器
"resolveJsonModule": true, // 加载json
"esModuleInterop": true,
// "removeComments": false, // 删除注释
"declaration": true, // 生成定义文件
"declarationMap": false, // 生成定义sourceMap
// "declarationDir": "./dist/types", // 定义文件输出目录


"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
},
"include": [
"src/*", // 导入目录
"**/*.ts"
],
"exclude": [
"node_modules", "**/__tests__/*", "./lib/**/*"
]
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
},
"include": [
"src/*", // 导入目录
"**/*.ts"
],
"exclude": [
"node_modules",
"**/__tests__/*",
"./lib/**/*"
]
}

0 comments on commit dc466e9

Please sign in to comment.