Skip to content

Commit

Permalink
Merge pull request #64 from HeZai/f_add_defaultCtrlData
Browse files Browse the repository at this point in the history
add: 增加 views 中间件 test
  • Loading branch information
xiongwilee committed May 13, 2019
2 parents 1e99c37 + fb76522 commit 9917d3b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ context属性 | 类型 | 说明
context属性 | 类型 | 中间件 | 说明
---------- | ---- | ----- | ------------------
`this.bindDefault` | `function` | router | 公共控制器,相当于`require('app/*/controller/defaultCtrl.js')`
`this.defaultCtrlData` | `object` | views | 模板渲染的全局变量,类似于 Koa `this.state`
`this.request.body` | `object` | body | post参数,可以直接在this.request.body中获取到post参数
`this.render` | `function` | views | 模板引擎渲染方法,请参看: 模板引擎- Template engine
`this.mongo` | `function` | mongo | 数据库操作方法,请参看: 数据库 - Database
Expand Down
3 changes: 3 additions & 0 deletions middleware/views/example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ app.use(views(app, {
}))

app.use(async(ctx, next) => {
ctx.defaultCtrlData = {
defaultData: 'this is defaultCtrlData'
}
await ctx.render('test.html', {
data: 'hello world!',
data1: { test1: 'test1' }
Expand Down
2 changes: 2 additions & 0 deletions middleware/views/example/views/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<h2>直接输出:</h2> {{data}}
<hr>

<h2>模板渲染全局变量:</h2>{{defaultData}}

<h2>include:</h2> {% include './common.html' ignore missing %}
<hr>

Expand Down
38 changes: 38 additions & 0 deletions test/views.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**!
* gracejs - test/views.test.js
* Copyright(c) 2019
* MIT Licensed
*
* Authors:
* hezai
*/

'use strict';

var koa = require('koa');
var app = require('../middleware/views/example/app');
var request = require('supertest');
var mm = require('mm');

describe('test/views.test.js', function() {
afterEach(function() {});

describe('set DefaultCtrlData', function() {
it('页面渲染', function(done) {
request(app)
.get('/')
.expect(200, done);
});

it('设置模板渲染全局变量', function(done) {
request(app)
.get('/')
.expect(200)
.end(function(err, res) {
if(/this is defaultCtrlData/.test(res.text)) {
done();
}
});
});
});
});

0 comments on commit 9917d3b

Please sign in to comment.