Skip to content
This repository was archived by the owner on Nov 18, 2021. It is now read-only.

Commit 992250e

Browse files
committed
测试完成
1 parent a135d95 commit 992250e

File tree

6 files changed

+215
-17
lines changed

6 files changed

+215
-17
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/coverage/

chrome-call.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(function ( root , factory ) {
2+
/* istanbul ignore next */
23
if ( typeof module === 'object' && typeof module.exports === 'object' ) {
34
module.exports = factory();
45
} else if ( typeof define === 'function' && define.amd ) {
@@ -7,23 +8,21 @@
78
root[ 'chromeCall' ] = factory();
89
}
910
})( this , function () {
10-
var ap = Array.prototype ,
11-
slice = ap.slice ,
12-
push = ap.push ,
11+
var slice = Array.prototype.slice ,
1312
runtime = chrome.runtime;
1413

1514
/**
16-
* 根据 base 对象返回指定路径的属性
15+
* 根据 base 对象返回指定路径的栈
1716
* @param {String} path - 属性路径,可用点(.)分隔
1817
* @param {String|Object} base - 开始查找的那个对象
1918
* @returns {*[]}
2019
*
2120
* @example
22-
* findPaths('document.body',window) 应该返回 [window,window.document,document.body]
21+
* pathStack('document.body',window) 应该返回 [window,window.document,window.document.body]
2322
*/
24-
function findPaths( path , base ) {
23+
function pathStack( path , base ) {
2524
if ( typeof base === 'string' ) {
26-
base = findPaths( base , chrome );
25+
base = pathStack( base , chrome ).pop();
2726
}
2827

2928
var keys = path.split( '.' ) ,
@@ -48,29 +47,23 @@
4847
function scope( base ) {
4948
return function ( fnPath /*, ...args*/ ) {
5049
// Step 1: find the function which need to be call
51-
var paths = findPaths( fnPath , base );
50+
var paths = pathStack( fnPath , base );
5251

5352
var args = slice.call( arguments , 1 );
5453
return new Promise( function ( resolve , reject ) {
5554
// Step 2: inject callback
56-
push.call( args , function () {
55+
args.push( function () {
5756
var lastError = runtime.lastError;
5857
if ( lastError ) {
5958
reject( lastError );
6059
return;
6160
}
6261

63-
var length = arguments.length;
64-
if ( length ) {
65-
resolve( length === 1 ? arguments[ 0 ] : slice.call( arguments , 0 ) );
66-
} else {
67-
resolve();
68-
}
62+
resolve( arguments.length <= 1 ? arguments[ 0 ] : slice.call( arguments , 0 ) );
6963
} );
7064

7165
// Step 3: call function with it's original "this"
72-
var length = paths.length;
73-
paths[ length - 1 ].apply( paths[ length - 2 ] , args );
66+
paths.pop().apply( paths.pop() , args );
7467
} );
7568
};
7669
}

karma.conf.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = function ( config ) {
2+
config.set( {
3+
basePath : '' ,
4+
frameworks : [ 'jasmine' ] ,
5+
files : [
6+
'node_modules/es6-promise/dist/es6-promise.js' ,
7+
'test/helper.js' ,
8+
'chrome-call.js' ,
9+
'test/test.js'
10+
] ,
11+
preprocessors : {
12+
'chrome-call.js' : [ 'coverage' ]
13+
} ,
14+
reporters : [ 'progress' , 'coverage' ] ,
15+
coverageReporter : {
16+
dir : 'coverage' ,
17+
reporters : [
18+
{
19+
type : 'html' ,
20+
subdir : function ( browser ) {
21+
return 'html/' + browser.toLowerCase().split( /[ /-]/ )[ 0 ];
22+
}
23+
} ,
24+
{
25+
type : 'lcov' ,
26+
subdir : 'lcov'
27+
}
28+
]
29+
} ,
30+
port : 9876 ,
31+
colors : true ,
32+
logLevel : config.LOG_INFO ,
33+
autoWatch : false ,
34+
browsers : [ 'Firefox' , 'Chrome' , 'IE' , 'PhantomJS' ] ,
35+
singleRun : true
36+
} )
37+
};

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@
1010
"bugs": {
1111
"url": "https://github.com/lmk123/chrome-call/issues"
1212
},
13+
"scripts": {
14+
"test": "karma start"
15+
},
16+
"devDependencies": {
17+
"es6-promise": "^3.0.2",
18+
"phantomjs": "^1.9.18",
19+
"jasmine-core": "^2.4.1",
20+
"karma": "^0.13.14",
21+
"karma-chrome-launcher": "^0.2.1",
22+
"karma-firefox-launcher": "^0.1.6",
23+
"karma-ie-launcher": "^0.2.0",
24+
"karma-jasmine": "^0.3.6",
25+
"karma-phantomjs-launcher": "^0.2.1",
26+
"karma-safari-launcher": "^0.1.1",
27+
"karma-coverage": "^0.5.3",
28+
"karma-coveralls": "^1.1.2"
29+
},
1330
"keywords": [
1431
"chrome",
1532
"ext",

test/helper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ES6Promise.polyfill();
2+
window.chrome = {
3+
runtime : {}
4+
};

test/test.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
chrome.storage = {
2+
local : {
3+
remove : function () {}
4+
}
5+
};
6+
7+
describe( 'chromeCall' , function () {
8+
describe( '调用 chorme api 时' , function () {
9+
10+
beforeEach( function () {
11+
spyOn( chrome.storage.local , 'remove' ).and.callFake( function ( key , value , cb ) {
12+
expect( this ).toBe( chrome.storage.local );
13+
expect( key ).toBe( 'a' );
14+
expect( value ).toBe( 'b' );
15+
expect( typeof cb ).toBe( 'function' );
16+
setTimeout( function () {
17+
cb( 'c' );
18+
} , 100 );
19+
} );
20+
} );
21+
22+
it( '若正常执行则 resolve promise' , function ( done ) {
23+
chromeCall( 'storage.local.remove' , 'a' , 'b' )
24+
.then( function ( value ) {
25+
expect( value ).toBe( 'c' );
26+
done();
27+
} , function () {
28+
fail( '没有进入 resolve 分支' );
29+
done();
30+
} );
31+
} );
32+
33+
it( '若执行时出错则会 reject promise' , function ( done ) {
34+
chrome.runtime.lastError = 'hello error';
35+
chromeCall( 'storage.local.remove' , 'a' , 'b' )
36+
.then( function () {
37+
fail( '没有进入 reject 分支' );
38+
chrome.runtime.lastError = null;
39+
done();
40+
} , function ( error ) {
41+
expect( error ).toBe( 'hello error' );
42+
chrome.runtime.lastError = null;
43+
done();
44+
} );
45+
} );
46+
47+
} );
48+
49+
describe( '调用的 chrome api' , function () {
50+
beforeEach( function () {
51+
spyOn( chrome.storage.local , 'remove' );
52+
} );
53+
54+
it( '若没有返回参数则 resolve 函数的参数为零' , function ( done ) {
55+
chrome.storage.local.remove.and.callFake( function ( cb ) {
56+
cb();
57+
} );
58+
chromeCall( 'storage.local.remove' )
59+
.then( function ( x ) {
60+
expect( x ).toBeUndefined();
61+
done();
62+
} , function () {
63+
fail( '没有进入 resolve 分支' );
64+
done();
65+
} );
66+
} );
67+
68+
it( '若只返回了一个参数则 resolve 函数的参数为一且值为那个参数' , function ( done ) {
69+
chrome.storage.local.remove.and.callFake( function ( cb ) {
70+
cb( 'x' );
71+
} );
72+
chromeCall( 'storage.local.remove' )
73+
.then( function ( value ) {
74+
expect( value ).toBe( 'x' );
75+
done();
76+
} , function () {
77+
fail( '没有进入 resolve 分支' );
78+
done();
79+
} );
80+
} );
81+
82+
it( '若返回了一个两个或更多参数则 resolve 函数的参数为一个真实的数组,包含那些参数' , function ( done ) {
83+
chrome.storage.local.remove.and.callFake( function ( cb ) {
84+
cb( 'x' , 'y' , 'z' );
85+
} );
86+
chromeCall( 'storage.local.remove' )
87+
.then( function ( value ) {
88+
expect( value ).toEqual( [ 'x' , 'y' , 'z' ] );
89+
done();
90+
} , function () {
91+
fail( '没有进入 resolve 分支' );
92+
done();
93+
} );
94+
} );
95+
96+
} );
97+
98+
it( '若找不到指定的路径则会报错' , function () {
99+
expect( function () {
100+
chromeCall( 'x.y.z' );
101+
} ).toThrow();
102+
} );
103+
104+
describe( '作用域' , function () {
105+
106+
beforeEach( function () {
107+
spyOn( chrome.storage.local , 'remove' );
108+
} );
109+
110+
it( '可以用字符串声明' , function ( done ) {
111+
var local = chromeCall.scope( 'storage.local' );
112+
113+
chrome.storage.local.remove.and.callFake( function ( cb ) {
114+
cb( 'hi' );
115+
} );
116+
117+
local( 'remove' )
118+
.then( function ( x ) {
119+
expect( x ).toBe( 'hi' );
120+
done();
121+
} , function () {
122+
fail( '没有进入 resolve 分支' );
123+
done();
124+
} );
125+
} );
126+
127+
it( '可以直接用对象声明' , function ( done ) {
128+
var local = chromeCall.scope( chrome.storage.local );
129+
130+
chrome.storage.local.remove.and.callFake( function ( cb ) {
131+
cb( 'hi' );
132+
} );
133+
134+
local( 'remove' )
135+
.then( function ( x ) {
136+
expect( x ).toBe( 'hi' );
137+
done();
138+
} , function () {
139+
fail( '没有进入 resolve 分支' );
140+
done();
141+
} );
142+
} );
143+
144+
} );
145+
} );

0 commit comments

Comments
 (0)