Skip to content

Commit

Permalink
update the test with ejs-v3 🧪 ..
Browse files Browse the repository at this point in the history
  • Loading branch information
3imed-jaberi committed Jul 13, 2021
1 parent 4040752 commit 468410f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 65 deletions.
83 changes: 42 additions & 41 deletions test/koa-ejs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,119 +4,120 @@
* MIT Licensed
*/

'use strict';
'use strict'

/**
* Module dependencies.
*/

const render = require('..');
const request = require('supertest');
const Koa = require('koa');
const render = require('..')
const request = require('supertest')
const Koa = require('koa')

describe('test/koa-ejs.test.js', function () {
describe('init()', function () {
const app = new Koa();
const app = new Koa()
it('should throw error if no root', function () {
(function () {
render(app);
render(app)
}).should.throw('settings.root required');

(function () {
render(app, {});
}).should.throw('settings.root required');
});
render(app, {})
}).should.throw('settings.root required')
})

it('should init ok', function () {
render(app, {
root: __dirname,
open: '{{',
close: '}}'
});
app.context.render.should.be.Function;
});
});
})
// eslint-disable-next-line no-unused-expressions
app.context.render.should.be.Function
})
})

describe('server', function () {
it('should render page ok', function (done) {
const app = require('../example/app');
const app = require('../example/app')
request(app)
.get('/')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/<title>koa ejs<\/title>/)
.expect(/Dead Horse/)
.expect(/Jack/, done);
});
.expect(/Imed Jaberi/, done)
})
it('should render page ok with async functions', function (done) {
const app = new Koa();
const app = new Koa()
render(app, {
root: 'example/view',
viewExt: 'html',
layout: false,
async: true
});
})

app.use(async function (ctx) {
await ctx.render('async', {
async sayHello (name) {
return `Hello, ${name}`
}
});
});
})
})
request(app.callback())
.get('/')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/Hello, Jack/, done);
});
.expect(/Hello, Jack/, done)
})
it('should render page ok with custom open/close', function (done) {
const app = new Koa();
const app = new Koa()
render(app, {
root: 'example/view',
layout: 'template.oc',
viewExt: 'html',
delimiter: '?'
});
})

app.use(async function (ctx) {
await ctx.render('user.oc', {
user: { name: 'Zed Gu' }
});
});
})
})
request(app.callback())
.get('/')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/Zed Gu/, done);
});
.expect(/Zed Gu/, done)
})
it('should render page ok with `viewExt` option supporting `include` directive', function (done) {
const app = new Koa();
const app = new Koa()
render(app, {
root: 'example/view',
layout: 'template',
viewExt: 'html',
cache: false
});
})

app.use(function (ctx, next) {
ctx.state = ctx.state || {};
ctx.state.ip = ctx.ip;
return next();
});
ctx.state = ctx.state || {}
ctx.state.ip = ctx.ip
return next()
})

app.use(async function (ctx) {
const users = [{ name: 'Dead Horse' }, { name: 'Runrioter Wung' }];
const users = [{ name: 'Dead Horse' }, { name: 'Runrioter Wung' }]
await ctx.render('content.noext', {
users
});
});
})
})
request(app.callback())
.get('/')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/Dead Horse/)
.expect(/Runrioter Wung/, done);
});
});
});
.expect(/Runrioter Wung/, done)
})
})
})
48 changes: 24 additions & 24 deletions test/write-response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,68 @@
* MIT Licensed
*/

'use strict';
'use strict'

/**
* Module dependencies.
*/

const render = require('..');
const request = require('supertest');
const Koa = require('koa');
const render = require('..')
const request = require('supertest')
const Koa = require('koa')

describe('test/write-response.test.js', function () {
describe('writeResp option', function () {
it('should return html with default configuration and writeResp option = false', function (done) {
const app = new Koa();
const app = new Koa()
render(app, {
root: 'example/view',
layout: 'template.oc',
viewExt: 'html',
delimiter: '?'
});
})

app.use(async function (ctx, next) {
const html = await ctx.render('user.oc', {
user: { name: 'Zed Gu' },
writeResp: false
});
})

ctx.type = 'html';
ctx.body = html;
});
ctx.type = 'html'
ctx.body = html
})

request(app.callback())
.get('/')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/Zed Gu/, done);
});
.expect(/Zed Gu/, done)
})

it('should return html with configuration writeResp = false', function (done) {
const app = new Koa();
const app = new Koa()
render(app, {
root: 'example/view',
layout: 'template.oc',
viewExt: 'html',
delimiter: '?',
writeResp: false
});
})

app.use(async function (ctx) {
const html = await ctx.render('user.oc', {
user: { name: 'Zed Gu' }
});
const html = await ctx.render('user.oc', {
user: { name: 'Zed Gu' }
})

ctx.type = 'html';
ctx.body = html;
});
ctx.type = 'html'
ctx.body = html
})

request(app.callback())
.get('/')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect(/Zed Gu/, done);
});
});
});
.expect(/Zed Gu/, done)
})
})
})

0 comments on commit 468410f

Please sign in to comment.