Skip to content

Commit 90bf521

Browse files
authored
feat: add unittest (#2)
1 parent b3178c3 commit 90bf521

File tree

21 files changed

+454
-0
lines changed

21 files changed

+454
-0
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ examples/**/app/public
33
node_modules
44
logs
55
run
6+
coverage

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ $ npm start
1313
## List of examples
1414

1515
- [httpclient]
16+
- [unittest]
1617

1718
[egg]: https://github.com/eggjs/egg
1819
[httpclient]: https://github.com/eggjs/examples/tree/master/httpclient
20+
[unittest]: https://github.com/eggjs/examples/tree/master/unittest

unittest/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# httpclient
2+
3+
Example use on https://github.com/eggjs/egg/blob/master/docs/source/zh-cn/core/httpclient.md

unittest/app.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = function* init(app) {
4+
// 示例:启动的时候去读取 https://registry.npm.taobao.org/egg/latest 的版本信息
5+
const result = yield app.curl('https://registry.npm.taobao.org/egg/latest', {
6+
dataType: 'json',
7+
});
8+
app.logger.info('egg latest version: %s', result.data.version);
9+
};

unittest/app/controller/home.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
exports.index = function* () {
4+
this.body = 'hello world';
5+
};
6+
7+
exports.post = function* () {
8+
this.body = this.request.body;
9+
};
10+
11+
exports.session = function* () {
12+
this.body = {
13+
session: this.session,
14+
};
15+
};
16+
17+
exports.user = function* () {
18+
const user = yield this.service.user.get(this.query.name);
19+
this.body = {
20+
user,
21+
};
22+
};
23+
24+
exports.httpclient = function* () {
25+
const res = yield this.curl('https://eggjs.org');
26+
this.body = res.data.toString();
27+
};

unittest/app/extend/application.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const LRU = Symbol('Application#lru');
4+
const LRUCache = require('ylru');
5+
6+
module.exports = {
7+
get lru() {
8+
if (!this[LRU]) {
9+
this[LRU] = new LRUCache(1000);
10+
}
11+
return this[LRU];
12+
},
13+
};

unittest/app/extend/context.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
module.exports = {
4+
get isXHR() {
5+
return this.get('X-Requested-With') === 'XMLHttpRequest';
6+
},
7+
};

unittest/app/extend/helper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = {
4+
money(val) {
5+
const lang = this.ctx.get('Accept-Language');
6+
if (lang.includes('zh-CN')) {
7+
return `¥ ${val}`;
8+
}
9+
return `$ ${val}`;
10+
},
11+
};

unittest/app/extend/request.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const IS_CHROME = Symbol('Request#isChrome');
4+
5+
module.exports = {
6+
get isChrome() {
7+
if (!this[IS_CHROME]) {
8+
const ua = this.get('User-Agent').toLowerCase();
9+
this[IS_CHROME] = ua.includes('chrome/');
10+
}
11+
return this[IS_CHROME];
12+
},
13+
};

unittest/app/extend/response.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
module.exports = {
4+
get isSuccess() {
5+
return this.status === 200;
6+
},
7+
};

0 commit comments

Comments
 (0)