From 649c3a58a2fbe33ac3a8222299c19a867d0d78ea Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 16 Jan 2017 10:09:11 +0800 Subject: [PATCH 1/2] feat: add framework example --- framework/README.md | 39 ++++++++++++++++++++++ framework/app/app/controller/home.js | 7 ++++ framework/app/app/router.js | 5 +++ framework/app/app/view/home.tpl | 1 + framework/app/package.json | 13 ++++++++ framework/yadan/app/extend/application.js | 5 +++ framework/yadan/app/extend/context.js | 5 +++ framework/yadan/app/service/test.js | 17 ++++++++++ framework/yadan/config/config.default.js | 16 +++++++++ framework/yadan/config/plugin.js | 8 +++++ framework/yadan/index.js | 12 +++++++ framework/yadan/lib/agent.js | 13 ++++++++ framework/yadan/lib/application.js | 13 ++++++++ framework/yadan/package.json | 19 +++++++++++ framework/yadan/test/lib/framework.test.js | 39 ++++++++++++++++++++++ hackernews/package.json | 2 +- 16 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 framework/README.md create mode 100644 framework/app/app/controller/home.js create mode 100644 framework/app/app/router.js create mode 100644 framework/app/app/view/home.tpl create mode 100644 framework/app/package.json create mode 100644 framework/yadan/app/extend/application.js create mode 100644 framework/yadan/app/extend/context.js create mode 100644 framework/yadan/app/service/test.js create mode 100644 framework/yadan/config/config.default.js create mode 100644 framework/yadan/config/plugin.js create mode 100644 framework/yadan/index.js create mode 100644 framework/yadan/lib/agent.js create mode 100644 framework/yadan/lib/application.js create mode 100644 framework/yadan/package.json create mode 100644 framework/yadan/test/lib/framework.test.js diff --git a/framework/README.md b/framework/README.md new file mode 100644 index 0000000..587c31e --- /dev/null +++ b/framework/README.md @@ -0,0 +1,39 @@ +# Framework Example + +This example contains [app] and [framework] + +## Quick Start + +Start an application using custom framework called yadan + +```bash +$ cd app +$ npm install +$ npm link ../yadan +$ npm run dev +``` + +Yadan is a framework, it should be published to npm normally. With this example, you just `npm link` it. + +## Run Test + +Application + +```bash +$ cd app +$ npm test +``` + +Framework + +```bash +$ cd yadan +$ npm test +``` + +## Questions & Suggestions + +Please open an issue [here](https://github.com/eggjs/egg/issues). + +[app]: https://github.com/eggjs/examples/tree/master/framework/app +[framework]: https://github.com/eggjs/examples/tree/master/framework/yadan diff --git a/framework/app/app/controller/home.js b/framework/app/app/controller/home.js new file mode 100644 index 0000000..47eb266 --- /dev/null +++ b/framework/app/app/controller/home.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = function* home() { + // use service defined in framework + const data = yield this.service.test.get(123); + yield this.render('home.tpl', data); +}; diff --git a/framework/app/app/router.js b/framework/app/app/router.js new file mode 100644 index 0000000..e28728a --- /dev/null +++ b/framework/app/app/router.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = app => { + app.get('/', 'home'); +}; diff --git a/framework/app/app/view/home.tpl b/framework/app/app/view/home.tpl new file mode 100644 index 0000000..2349c26 --- /dev/null +++ b/framework/app/app/view/home.tpl @@ -0,0 +1 @@ +hi, {{ name }} \ No newline at end of file diff --git a/framework/app/package.json b/framework/app/package.json new file mode 100644 index 0000000..567ed60 --- /dev/null +++ b/framework/app/package.json @@ -0,0 +1,13 @@ +{ + "name": "framework-example", + "version": "1.0.0", + "devDependencies": { + "egg-bin": "^1.0.0" + }, + "scripts": { + "dev": "egg-bin dev" + }, + "egg": { + "framework": "yadan" + } +} diff --git a/framework/yadan/app/extend/application.js b/framework/yadan/app/extend/application.js new file mode 100644 index 0000000..0e903a8 --- /dev/null +++ b/framework/yadan/app/extend/application.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + +}; diff --git a/framework/yadan/app/extend/context.js b/framework/yadan/app/extend/context.js new file mode 100644 index 0000000..0e903a8 --- /dev/null +++ b/framework/yadan/app/extend/context.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + +}; diff --git a/framework/yadan/app/service/test.js b/framework/yadan/app/service/test.js new file mode 100644 index 0000000..38c1a40 --- /dev/null +++ b/framework/yadan/app/service/test.js @@ -0,0 +1,17 @@ +'use strict'; + +module.exports = app => ( + /** + * Test Service + */ + class Test extends app.Service { + constructor(ctx) { + super(ctx); + this.config = this.app.config.test; + } + + * get(id) { + return { id, name: this.config.key }; + } + } +); diff --git a/framework/yadan/config/config.default.js b/framework/yadan/config/config.default.js new file mode 100644 index 0000000..4c13a58 --- /dev/null +++ b/framework/yadan/config/config.default.js @@ -0,0 +1,16 @@ +'use strict'; + +module.exports = appInfo => { + const config = {}; + + /** + * some description + * @member Config#test + * @property {String} key - some description + */ + config.test = { + key: appInfo.name + '_123456', + }; + + return config; +}; diff --git a/framework/yadan/config/plugin.js b/framework/yadan/config/plugin.js new file mode 100644 index 0000000..96a68c6 --- /dev/null +++ b/framework/yadan/config/plugin.js @@ -0,0 +1,8 @@ +'use strict'; + +// add you build-in plugin here, example: +exports.view = { + enable: true, + package: 'egg-view-nunjucks', +}; + diff --git a/framework/yadan/index.js b/framework/yadan/index.js new file mode 100644 index 0000000..51c7daa --- /dev/null +++ b/framework/yadan/index.js @@ -0,0 +1,12 @@ +'use strict'; + +const Application = require('./lib/application'); +const Agent = require('./lib/agent'); +const egg = require('egg'); + +// clone egg API +Object.assign(exports, egg); + +// override Application and Agent +exports.Application = Application; +exports.Agent = Agent; diff --git a/framework/yadan/lib/agent.js b/framework/yadan/lib/agent.js new file mode 100644 index 0000000..bd5bd5b --- /dev/null +++ b/framework/yadan/lib/agent.js @@ -0,0 +1,13 @@ +'use strict'; + +const path = require('path'); +const egg = require('egg'); +const EGG_PATH = Symbol.for('egg#eggPath'); + +class YadanAgent extends egg.Agent { + get [EGG_PATH]() { + return path.dirname(__dirname); + } +} + +module.exports = YadanAgent; diff --git a/framework/yadan/lib/application.js b/framework/yadan/lib/application.js new file mode 100644 index 0000000..c6f9a46 --- /dev/null +++ b/framework/yadan/lib/application.js @@ -0,0 +1,13 @@ +'use strict'; + +const path = require('path'); +const egg = require('egg'); +const EGG_PATH = Symbol.for('egg#eggPath'); + +class YadanApplication extends egg.Application { + get [EGG_PATH]() { + return path.dirname(__dirname); + } +} + +module.exports = YadanApplication; diff --git a/framework/yadan/package.json b/framework/yadan/package.json new file mode 100644 index 0000000..ddfdc99 --- /dev/null +++ b/framework/yadan/package.json @@ -0,0 +1,19 @@ +{ + "name": "yadan", + "version": "1.0.0", + "dependencies": { + "egg": "^0.7.0", + "egg-view-nunjucks": "1.0.0" + }, + "devDependencies": { + "egg-bin": "^1.10.0", + "egg-mock": "^2.0.0", + "supertest": "^2.0.1" + }, + "engines": { + "node": ">=6.0.0" + }, + "scripts": { + "test": "egg-bin test" + } +} diff --git a/framework/yadan/test/lib/framework.test.js b/framework/yadan/test/lib/framework.test.js new file mode 100644 index 0000000..a1c13e4 --- /dev/null +++ b/framework/yadan/test/lib/framework.test.js @@ -0,0 +1,39 @@ +'use strict'; + +const path = require('path'); +const assert = require('assert'); +const request = require('supertest'); +const mm = require('egg-mock'); + +describe('test/lib/framework.test.js', () => { + let app; + before(() => { + app = mm.app({ + baseDir: path.join(__dirname, '../../../app'), + customEgg: true, + }); + return app.ready(); + }); + after(() => app.close()); + afterEach(mm.restore); + + it('should GET /', () => { + return request(app.callback()) + .get('/') + .expect('hi, framework-example_123456') + .expect(200); + }); + + it('should load config', () => { + assert(app.config.test.key === 'framework-example_123456'); + }); + + it('should load service', function* () { + const ctx = app.mockContext(); + const data = yield ctx.service.test.get(123); + assert.deepEqual(data, { + id: 123, + name: 'framework-example_123456', + }); + }); +}); diff --git a/hackernews/package.json b/hackernews/package.json index 808d83e..3f26837 100644 --- a/hackernews/package.json +++ b/hackernews/package.json @@ -5,7 +5,7 @@ "private": true, "dependencies": { "egg": "^0.7.0", - "egg-view-nunjucks": "^0.6.0", + "egg-view-nunjucks": "^1.0.0", "moment": "^2.17.1" }, "devDependencies": { From 5aa491ac1d8e8949e0b1e1e385bb4188a2bdd2c0 Mon Sep 17 00:00:00 2001 From: popomore Date: Mon, 16 Jan 2017 21:05:04 +0800 Subject: [PATCH 2/2] fix: add application test --- framework/app/package.json | 7 +++++-- .../app/test/app/controller/home.test.js | 20 +++++++++++++++++++ framework/yadan/package.json | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 framework/app/test/app/controller/home.test.js diff --git a/framework/app/package.json b/framework/app/package.json index 567ed60..f9f9d68 100644 --- a/framework/app/package.json +++ b/framework/app/package.json @@ -2,10 +2,13 @@ "name": "framework-example", "version": "1.0.0", "devDependencies": { - "egg-bin": "^1.0.0" + "egg-bin": "^2.0.0", + "egg-mock": "^2.0.0", + "supertest": "^2.0.1" }, "scripts": { - "dev": "egg-bin dev" + "dev": "egg-bin dev", + "test": "egg-bin test" }, "egg": { "framework": "yadan" diff --git a/framework/app/test/app/controller/home.test.js b/framework/app/test/app/controller/home.test.js new file mode 100644 index 0000000..28d830a --- /dev/null +++ b/framework/app/test/app/controller/home.test.js @@ -0,0 +1,20 @@ +'use strict'; + +const mock = require('egg-mock'); +const request = require('supertest'); + +describe('test/app/controller/home.test.js', () => { + + let app; + before(() => { + app = mock.app(); + return app.ready(); + }); + + it('should GET /', () => { + return request(app.callback()) + .get('/') + .expect(200) + .expect('hi, framework-example_123456'); + }); +}); diff --git a/framework/yadan/package.json b/framework/yadan/package.json index ddfdc99..7b99672 100644 --- a/framework/yadan/package.json +++ b/framework/yadan/package.json @@ -6,7 +6,7 @@ "egg-view-nunjucks": "1.0.0" }, "devDependencies": { - "egg-bin": "^1.10.0", + "egg-bin": "^2.0.0", "egg-mock": "^2.0.0", "supertest": "^2.0.1" },