123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- var koa = require('koa');
- var hbs = require('..');
- var assert = require('assert');
- var request = require('supertest');
- var testApp = require('./app');
- describe('without required options', function () {
- it('should throw an error when viewPath is not set', function () {
- assert.throws(function () { hbs.create().middleware({}); });
- });
- });
- describe('rendering', function() {
- var app;
- it('should render into the response body', function (done) {
- app = testApp.create({
- viewPath: __dirname + '/app/assets'
- });
- request(app.listen())
- .get('/')
- .expect(200)
- .end(function (err, content) {
- if(err) return done(err);
- assert.ok(/<title>test<\/title>/.test(content.text));
- assert.ok(/html/.test(content.text));
- done();
- });
- });
- describe('with an empty template', function () {
- it('should render a blank page', function (done) {
- app = testApp.create({
- viewPath: __dirname + '/app/assets'
- });
- request(app.listen())
- .get('/empty')
- .expect(200)
- .end(function (err, page) {
- if(err) return done(err);
- assert.deepEqual(page.text, '');
- done();
- });
- });
- });
- describe('with partials', function () {
- it('should render into the response body', function (done) {
- app = testApp.create({
- viewPath: __dirname + '/app/assets',
- partialsPath: __dirname + '/app/assets/partials'
- });
- request(app.listen())
- .get('/partials')
- .expect(200)
- .end(function (err, content) {
- if(err) return done(err);
- assert.ok(/google\.com/.test(content.text));
- done();
- });
- });
- it('should work also for nested partials', function (done) {
- request(app.listen())
- .get('/nestedPartials')
- .expect(200)
- .end(function (err, content) {
- assert.ok(/NESTED/.test(content.text));
- done();
- });
- });
- });
- describe('when using layouts', function () {
- var app;
- before(function () {
- // Create app which specifies layouts
- app = testApp.create({
- viewPath: __dirname + '/app/assets',
- partialsPath: __dirname + '/app/assets/partials',
- layoutsPath: __dirname + '/app/assets/layouts',
- defaultLayout: 'default'
- });
- });
- describe('with the default layout', function () {
- it('should insert rendered content', function (done) {
- request(app.listen())
- .get('/layout')
- .expect(200)
- .end(function (err, content) {
- if(err) return done(err);
- assert.ok(/DEFAULT LAYOUT/.test(content.text));
- assert.ok(/DEFAULT CONTENT/.test(content.text));
- done();
- });
- });
- it('should support alternative layouts', function (done) {
- request(app.listen())
- .get('/altLayout')
- .expect(200)
- .end(function (err, content) {
- if(err) return done(err);
- assert.ok(/ALTERNATIVE LAYOUT/.test(content.text));
- assert.ok(/ALTERNATIVE CONTENT/.test(content.text));
- done();
- });
- });
- });
- describe('with block content', function() {
- it('should show default without content for', function (done) {
- request(app.listen())
- .get('/blockNoReplace')
- .expect(200)
- .end(function (err, content) {
- assert.ok(/DEFAULT BLOCK CONTENT/.test(content.text));
- assert.ok(/NO BLOCK/.test(content.text));
- done();
- });
- });
- it('should replace block content with contentFor', function (done) {
- request(app.listen())
- .get('/block')
- .expect(200)
- .end(function (err, content) {
- assert.ok(/CONTENT FOR SIDEBAR/.test(content.text));
- assert.ok(/CONTENT IN THE BODY/.test(content.text));
- done();
- });
- });
- });
- });
- describe('when using locals', function () {
- var app;
- before(function () {
- // Create app which specifies layouts
- app = testApp.create({
- viewPath: __dirname + '/app/assets',
- partialsPath: __dirname + '/app/assets/partials',
- layoutsPath: __dirname + '/app/assets/layouts',
- locals: {
- title: 'Foo'
- }
- });
- });
- it('should not overflow the call stack when recursive', function (done) {
- request(app.listen())
- .get('/localsRecursive')
- .expect(200)
- .end(function (err, content) {
- if(err) {
- return done(err);
- }
- done();
- });
- });
- it('should render "Foo"', function (done) {
- request(app.listen())
- .get('/locals')
- .expect(200)
- .end(function (err, content) {
- assert.ok(/Foo/.test(content.text));
- done();
- });
- });
- it('should render "Bar"', function (done) {
- request(app.listen())
- .get('/localsOverride')
- .expect(200)
- .end(function (err, content) {
- assert.ok(/Bar/.test(content.text));
- done();
- });
- });
- });
- });
- describe('var conflict', function () {
- var app = koa();
- app.use(hbs.middleware({
- viewPath: __dirname + '/app/assets'
- }));
- app.use(function * () {
- if (this.url === '/first') {
- yield this.render('locals', {
- title: 'hbs'
- });
- return;
- }
- if (this.url === '/second') {
- yield this.render('locals', {
- name: 'hbs'
- });
- return;
- }
- });
- var server;
- before(function () {
- server = app.listen();
- });
- it('should render title', function (done) {
- request(server)
- .get('/first')
- .expect(200)
- .end(function (err, content) {
- assert.equal(content.text, '<h1>hbs</h1>');
- done();
- });
- });
- it('should not have title', function (done) {
- request(server)
- .get('/second')
- .expect(200)
- .end(function (err, content) {
- assert.equal(content.text, '<h1></h1>');
- done();
- });
- });
- });
|