Skip to content

Commit

Permalink
test: 增加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Saviio committed Jun 11, 2018
1 parent 126af7a commit 97dd909
Show file tree
Hide file tree
Showing 10 changed files with 2,521 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
*.log
lib
coverage
.nyc_output
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ mockEnv.openDetail()

##### `Class: AppSDK`

- ```Static Method: AppSDK.fork(service, onPush, timeout)```
- ```Static Method: AppSDK.fork<T, K>(service, onPush, requestTimeout, connectTimeout)```

| 属性 | 说明 | 类型 | 默认值 |
| - | - | - | - |
| service | 指定装载的宿主 API 配置容器 | (sdk: AppSDK) => any | - |
| onPush | 指定宿主环境主动推送时的回调 | (data: any) => void | - |
| timeout | 指定远端调用最大超时时间 | number | 10000 |

| service | 指定装载的宿主 API 配置容器 | (sdk: AppSDK) => T | - |
| onPush | 指定宿主环境主动推送时的回调 | (data: K) => void | - |
| requestTimeout | 指定远端调用最大超时时间 | number | 10000 |
| connectTimeout | 指定远端连接最大超时时间 | number | 60000 |

## License
MIT
Expand Down
33 changes: 30 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
"description": "A sdk for communications between teambiton web and other envs.",
"main": "./index.js",
"scripts": {
"precommit": "lint-staged",
"build": "rm -rf ./lib && tsc -p ./tsconfig.json",
"publish_sdk": "npm run build && cp README.md package.json ./lib && npm publish ./lib",
"lint": "tslint -c tslint.json src/*.ts --project ./tsconfig.json \"src/**/*.ts\" \"./test/**/*.ts\" -e \"./test/e2e/*.ts\""
"cover": "rm -rf ./coverage ./.nyc_output && nyc --reporter=html --reporter=lcov --exclude=node_modules --exclude=test --exclude=lib/api/* --exclude=lib/call-service.js mocha && nyc report",
"lint": "tslint -c tslint.json src/*.ts --project ./tsconfig.json \"src/**/*.ts\" \"./test/**/*.ts\" -e \"./test/e2e/*.ts\"",
"test": "mocha"
},
"typings": "index.d.ts",
"repository": "https://github.com/teambition/tb-apps-sdk.git",
Expand All @@ -24,9 +27,33 @@
"tslib": "^1.6.0"
},
"devDependencies": {
"typescript": "^2.8.3",
"@types/chai": "^4.1.3",
"@types/mocha": "^5.2.1",
"chai": "^4.1.2",
"lint-staged": "^7.2.0",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"prettier": "^1.13.5",
"sinon": "^6.0.0",
"sinon-chai": "^3.1.0",
"tslib": "^1.6.0",
"tslint": "^5.9.1",
"tslint-eslint-rules": "^5.1.0"
"tslint-eslint-rules": "^5.1.0",
"typescript": "^2.8.3"
},
"prettier": {
"printWidth": 120,
"semi": false,
"trailingComma": "all",
"singleQuote": true,
"arrowParens": "always",
"parser": "typescript"
},
"lint-staged": {
"*.ts": [
"prettier --write",
"tslint -c tslint.json -p tsconfig.json --fix -e \"**/test/expect/**\"",
"git add"
]
}
}
6 changes: 6 additions & 0 deletions src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export function factory<T>(sdk: AppSDK, ctor: ICtor<T>): T & IAPIBase {
return new ctor(sdk) as T & IAPIBase
}

export function method(name: string) {
return function(...params: any[]) {
return this.call(name, ...params)
}
}

export class APIBase {

// version: number = 0
Expand Down
1 change: 1 addition & 0 deletions src/exception/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const ProcedureTimeout = 'Procedure call time out.'
export const CrossOrigin = 'Execute code error since cross origin.'
export const SourceNotFound = 'Target source is not found.'
export const IncorrectVersion = 'API version doest not matched.'
export const Uninitlized = 'SDK should be initlized.'
18 changes: 13 additions & 5 deletions src/sdk/AppSDK.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Connector, ConnStatus, Procedures, MessageType, RemoteCall } from '../interface'
import { ProcedureTimeout, CrossOrigin, SourceNotFound } from '../exception'
import { ProcedureTimeout, CrossOrigin, SourceNotFound, Uninitlized } from '../exception'

export class AppSDK {

static fork<T, K = any>(
service: (sdk: AppSDK) => T,
onPush?: (data: K) => void,
timeout = 10 * 1000
requestTimeout?: number,
connectTimeout?: number
) {
const sdk = new AppSDK(onPush, timeout)
const sdk = new AppSDK(onPush, requestTimeout, connectTimeout)
return service(sdk) as T
}

Expand All @@ -32,11 +33,12 @@ export class AppSDK {
window.addEventListener('message', this.onMessage)
this.connection = new Promise((resolve, reject) => {
this.connector.status = window.setTimeout(() => {
reject(new Error(SourceNotFound))
this.connector.status = ConnStatus.Failed
reject(new Error(SourceNotFound))
}, this.connectTimeout)

this.connector.resolve = () => {
/* istanbul ignore else */
if (this.connector.status !== ConnStatus.Success &&
this.connector.status != undefined
) {
Expand All @@ -52,7 +54,10 @@ export class AppSDK {
window.removeEventListener('message', this.onMessage)
}

send<T>(req: RemoteCall, isIsolated: boolean = false): Promise<T> {
send<T>(req: RemoteCall, isIsolated: boolean): Promise<T> {
if (!this.connection) {
return Promise.reject(new Error(Uninitlized))
}
if (this.connector.status === ConnStatus.Failed) {
return Promise.reject(new Error(SourceNotFound))
}
Expand Down Expand Up @@ -94,6 +99,7 @@ export class AppSDK {
switch (resp.data.type) {

case MessageType.SYN:
/* istanbul ignore else */
if (!this.initialize) {
this.targetId = resp.data.targetId
this.targetOrigin = resp.data.origin
Expand All @@ -106,12 +112,14 @@ export class AppSDK {
return

case MessageType.PSH:
/* istanbul ignore else */
if (typeof this.onPush === 'function' && typeof resp.data.payload != 'undefined') {
this.onPush(resp.data.payload)
}
return

case MessageType.MSG:
/* istanbul ignore else */
if (resp.data.responseId) {
const connector = this.procedures[resp.data.responseId]
if (!connector) {
Expand Down
Loading

0 comments on commit 97dd909

Please sign in to comment.